php - Custom Validation with Phalcon Model -
i trying implement custom validation models. using included validators phalcon library, when try implement mine, doesn't work. here code:
<?php use phalcon\mvc\model\validator; use phalcon\mvc\model\validatorinterface; use phalcon\mvc\entityinterface; class maxminvalidator extends validator implements validatorinterface { public function validate(entityinterface $model) { $field = $this->getoption('field'); $min = $this->getoption('min'); $max = $this->getoption('max'); $value = $model->$field; if ($min <= $value && $value <= $max) { $this->appendmessage( "the field doesn't have right range of values", $field, "maxminvalidator" ); return false; } return true; } }
i trying use in model this:
public function validation() { $this->validate(new maxminvalidator( array( "field" => "email", "min" => 10, "max" => 100 ) )); }
the class correctly loaded, doesn't execute validation function , code stops being executed after trying validate. note: exact same code found here.
1.check validator class loaded properly. (create function , write die("sth");) if calling function causes app die loaded class properly.
2.check phalcon version if under 2.0.5 should modify validator code to:
<?php use phalcon\mvc\model\validator; use phalcon\mvc\model\validatorinterface; use phalcon\mvc\entityinterface; class maxminvalidator extends validator implements validatorinterface { public function validate(\phalcon\mvc\modelinterface $model) { $field = $this->getoption('field'); $min = $this->getoption('min'); $max = $this->getoption('max'); $value = $model->$field; if ($min <= $value && $value <= $max) { $this->appendmessage( "the field doesn't have right range of values", $field, "maxminvalidator" ); return false; } return true; } }
and if phalcon version 2.0.5 , class loaded u can't see validation error message add method model class:
public function getmessages() { $messages = array(); foreach (parent::getmessages() $message) { switch ($message->gettype()) { case 'invalidcreateattempt': $messages[] = 'the record cannot created because exists'; break; case 'invalidupdateattempt': $messages[] = 'the record cannot updated because exists'; break; case 'presenceof': $messages[] = 'the field ' . $message->getfield() . ' mandatory'; break; case 'maxminvalidator': $messages[] = 'the field ' . $message->getfield() . ' not in range'; break; } } return $messages; }
Comments
Post a Comment