I am trying to implement custom validation for my models. I am currently using successfully the included validators from Phalcon library, but when I try to implement mine, it doesn't work. Here is my 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 the right range of values",
$field,
"MaxMinValidator"
);
return false;
}
return true;
}
}
I am trying to use it in my model like this:
public function validation()
{
$this->validate(new MaxMinValidator(
array(
"field" => "Email",
"min" => 10,
"max" => 100
)
));
}
The class is correctly loaded, but doesn't execute the validation function and my code stops being executed after trying to validate. Note: This is the exact same code as found here .
1.Check that your validator class is loaded properly. (create a function and write die("sth");) if calling that function causes your app to die you loaded the class properly.
2.Check your phalcon version if it under 2.0.5 you should modify your 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 the right range of values",
$field,
"MaxMinValidator"
);
return false;
}
return true;
}
}
and if your phalcon version is 2.0.5 and your class is loaded but u can't see the validation error message add this method to your model class:
public function getMessages() {
$messages = array();
foreach (parent::getMessages() as $message) {
switch ($message->getType()) {
case 'InvalidCreateAttempt':
$messages[] = 'The record cannot be created because it already exists';
break;
case 'InvalidUpdateAttempt':
$messages[] = 'The record cannot be updated because it already exists';
break;
case 'PresenceOf':
$messages[] = 'The field ' . $message->getField() . ' is mandatory';
break;
case 'MaxMinValidator':
$messages[] = 'The field ' . $message->getField() . ' is not in range';
break;
}
}
return $messages;
}
did you check the whole function?
public function validation()
{
$this->validate(new MaxMinValidator(
array(
"field" => "Email",
"min" => 10,
"max" => 100
)
));
// This line:
return $this->validationHasFailed() != true;
}
I think that you missed running validationHasFailed
method.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.