Additional Options

MV allows to perform additional data process inside core methods. For this you need to reload the function from the core to models.

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 % in the end.";

        return parent :: validate();
    }
}
?>

Get/set fields properties of the Model

From a model object we can get the property of it’s 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 add 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";
}