We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

MaxMinValidation problem

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?

edited May '14

Did you call method validation() in you controller?

if ($image->validation() == false)  {
    foreach ($image->getMessages() as $message)  {
        $this->flashSession->warning((string)$message);
    }
} else {
    # successfully 
}

If it works, then I think you need this: https://docs.phalcon.io/en/latest/reference/models.html#disabling-enabling-features

I had the same problem and I took the same solution, creating the validator inside a 'validators' directory and registered in the bootstrap like models and controllers, but this is not explained...

Also there is no need to call the validation() method in the controller if you use the 'create()' method of the model, validation will automaticaly run.

I also had a problem with the MaxMinValidator because it is explained (I've understand it) like it should be a 'range' validator (where valid values are between min and max) but in reality it is a 'not-in-range' validator, where not valid value are the ones between the range.

For example if min is 10 and max is 100, using its condition and following values

with value = 50 10 <= 50 && 50 <= 100 true && true

condition is true so validation is false, we will get the error.


with value 0 10 <= 0 && 0 <= 100 false && true

condition is false so validation is true, no error


with value 200 10 <= 200 && 200 <= 100 true && false

condition is false so validation is true, no error

Correct me if I mistake.

Cheers

edited Nov '14

Hi. I think You can use this validation rules for validation min and max

Phalcon\Validation\Validator\Between

https://docs.phalcon.io/en/latest/api/Phalcon_Validation_Validator_Between.html

I helped You ?

edited Nov '14

if $min = 10, $max = 100, $value = 50, as You said.

if ($min <= $value && $value <= $max) {
        $this->appendMessage(
            "The field doesn't have the right range of values",
            $field,
            "MaxMinValidator"
        );
        return false;
    }

this

if (10 <= 50 && 50 <=100)

(10 <= 50 && 50 <=100) == TRUE but method return false

with message

 "The field doesn't have the right range of values",

But i don't understand, 50 is correct value, why you returned false with error message?

Or I don't understand all :(

50 - is right range of values, or not ?

Hi vitaliykoziy, thank you for your answer, I'll try to make clear what my point is:

currently I am learning PhalconPHP, so in the documentation I've found this part where validation is explained with this MaxMinValidation example. What I thought at a first glance is "ok, this is an example of a range validator", but trying the code, what I've found is that if I pass a number that is out of the range (less than the min value or more than the max) the MaxMinValidation is not giving any error, instead if the number is within the specified range (so between min and max) it is giving the error.

Then doing some search I've found this discussion with the user 'mixuuu' that did my same assumptions, "why validation passes when the size is over max?" Because this MaxMinValidation it is not a "Between" validator, I only think that the name of this validator is just confusing and also how they are explained.

So no other issue at this point, thanks for reading this :)

Cheers