problem or a bug in Phalcon\Validation\Validator\Confirmation

Hi all!

I have a registration form with two fields mobile and mobile2. The second field is only to confirm that the user has not made any typo.

My form file:


//begining of the file
use Phalcon\Validation\Validator\Confirmation as ConfirmationValidator;

//leter somewhare in an initialisation() method:

$mobile2->addValidator(new ConfirmationValidator(array(
  'message' => "Numbers does not match",
  'with' => 'mobile'
))); 

Everything works fine except one situation.

The validator does NOT see an error when the values differ in + sign at the beginnig, e.g.:

mobile has +420777888999 value

mobile2 has 420777888999 value

Does anyone have similar problem to me? Is it a bug or this is how it should work?



1.7k

The ConfirmationValidator uses the != comparison operator to test if the values differ.

I think both values are tested agains each other as int instead of string. Not sure why this happens though. Maybe there is a entity bound to the form and getMobile() returns a int? In which case +420777888999 == 420777888999 and the validation passes.

A solution might be to implement a stricter ConfirmationValidator yourself.



18.0k

Thanks waaghals!

There is no entity bound to the form. Only $_POST values.

But this might be the case. As far as I know, in PHP:

"+48" == "48" gives TRUE

"+48" === "48" gives FALSE

"+48" == +48 gives TRUE

"+48" === +48 gives FALSE



18.0k

I have made this, what do you think?


<?php

use Phalcon\Validation\Validator,
    Phalcon\Validation\ValidatorInterface,
    Phalcon\Validation\Validator\Confirmation as ConfirmationValidator,
    Phalcon\Validation\Message;

class StrictConfirmation extends ConfirmationValidator implements ValidatorInterface {

    public function validate($validator,$attribute) {

        //obtain the name of the field 
        $with = $this->getOption("with");

        //obtain "with" field value - first value to be compared
        $with_value = $validator->getValue($with);

        //obtain atribute value - second value to be compared
        $value = $validator->getValue($attribute);

        //check if the values match
        if($with_value === $value){
            //value is valid
            return true;//end of validation
        }

        //try to obtain message defined in a validator
        $message = $this->getOption('message');

        //if the message is not defined takes a standard one
        if (!isset($message) OR !$message) {
            $message = "Values do not match each other";
        }

        //don't know if the last parameter of the Message class constructor is correct and make sense
        $validator->appendMessage(new Message($message, $attribute, 'StrictConfirmation'));

        //end of function
        return false;
    }

}

When producing a Messages, what should be the last parameter of Message class constructor? I have typed StrictConfirmation.



1.7k

Looks good!, Message() third paramater is a string value for the type, so I think StrictConfirmation is indeed correct.