I'm trying to do so my Image models size can have a min and a max value, I followed the documentation (I had no idea where to put the custom validator) so I created an app/validators
directory and added 'validatorsDir' => __DIR__ . '/../../app/validators/',
to config/config.php
and
# app/config/loader.php
<?php
$loader = new \Phalcon\Loader();
$loader->registerDirs(
array(
$config->application->controllersDir,
$config->application->modelsDir,
$config->application->validatorsDir
)
)->register();
And heres the validator class:
# app/validators/MaxMinValidator.php
<?php
use Phalcon\Mvc\Model\Validator,
Phalcon\Mvc\Model\ValidatorInterface;
class MaxMinValidator extends Validator implements ValidatorInterface
{
public function validate($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 finally heres the model:
<?php
class Image extends \Phalcon\Mvc\Model
{
/**
*
* @var integer
*/
public $size;
public function initialize() {
$this->belongsTo('post_id', 'Post', 'id');
}
public function validation()
{
$this->validate(new MaxMinValidator(
array(
"field" => "size",
"min" => 1,
"max" => 10
)
));
if ($this->validationHasFailed() == true) {
return false;
}
}
}
However it still passes validation when the size is over max?