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.