Additional options
MV allows to perform additional data processing inside core methods. To do this you need to reload the code function inside the model class.
Additional data validation in the model
Let’s assume we need to insert data in format of "12%". We need to validate entered data to match a template of regular expression.
<? class Discount extends Model { protected $name = "Discounts"; protected $name_field = "amount"; protected $model_elements = array( array("Activation", "bool", "active", array("on_create" => true)), array("Discount amount, %", "char", "amount", array("required" => true, "help_text" => "Samples: 10%, 17%, 30%")), array("Amount, from", "int", "summ_from", array("required" => true)), array("Amount, to", "int", "summ_to", array("zero_allowed" => false)) ); public function validate() { $amount = $this -> elements["amount"] -> getValue(); if($amount && !preg_match("/^[1-9]d?%$/", $amount)) $this -> errors[] = "Amount of discount should be an integer with % at the end."; return parent :: validate(); } } ?>
Get and set fields properties of the model
From a model object we can get the property of its fields, and also to set new properties for these fields.
- getFieldProperty($field, $property) - get $property value of $field
- setFieldProperty($field, $property, $value) - set value equal to the $property $value of the $field
$value = $mv -> discount -> getFieldProperty("summ_to", "zero_allowed"); if( ... ) $mv -> discount -> setFieldProperty("summ_to", "zero_allowed", true);
Transforming values in model table in admin panel
It is possible to change the values of fields in main model table in admin panel. For this porpose special method should be added into model class.
- processAdminModelTableFields($name, $row) - where $name - name of field, $row - array of all fields values of record including id
//Add "miles per hour" for speed values public function processAdminModelTableFields($name, $row) { if($name = "speed" && $row[$name]) return $row[$name]." mph"; }
Previous section
Simple model management