vlucas / valitron

Valitron is a simple, elegant, stand-alone validation library with NO dependencies
BSD 3-Clause "New" or "Revised" License
1.58k stars 250 forks source link

equals rule is broken #257

Open GrishinSergey opened 6 years ago

GrishinSergey commented 6 years ago

Hello, I use this library for validation registration form. But, when I try to check two fields - password and confirmPassword to equality with "equals" rule, it works incorrectly. Firslty, I saw the same Notises and Warnings:

Notice: Undefined offset: 0 in /var/www/auth.lesson/vendor/vlucas/valitron/src/Valitron/Validator.php on line 159

Warning: vsprintf(): Too few arguments in /var/www/auth.lesson/vendor/vlucas/valitron/src/Valitron/Validator.php on line 714

Notice: Undefined offset: 0 in /var/www/auth.lesson/vendor/vlucas/valitron/src/Valitron/Validator.php on line 159

Warning: vsprintf(): Too few arguments in /var/www/auth.lesson/vendor/vlucas/valitron/src/Valitron/Validator.php on line 714

Than, I check result of validation and it's false for set of data: password=11111111 confirmPassword=11111111

Other rules returs true. An error only with "euals"

My full validation code

$this->validator
            ->rule("required", [self::EMAIL_KEY_NAME, self::PASSWORD_KEY_NAME, self::CONFIRM_KEY_NAME])
            ->rule("equals", [self::PASSWORD_KEY_NAME, self::CONFIRM_KEY_NAME])
            ->rule("email", [self::EMAIL_KEY_NAME])
            ->rule("lengthMin", [self::PASSWORD_KEY_NAME], self::PASSWORD_MIN_LENGTH)
            ->validate();

PASSWORD_MIN_LENGTH = 8 (so, there can't been problems with this)

willemwollebrants commented 6 years ago

The problem is a combination of a poor error message and a (small) mistake in your code. The good news is: the mistake is easy to fix.

I'm taking only the part that is causing the problem:

$this->validator->rule("equals", [self::PASSWORD_KEY_NAME, self::CONFIRM_KEY_NAME]);

This line tells our validator 'check that both the password and the confirm field have the same value as an undefined (null) field. That's of course not supposed to happen, which is why the error message doesn't get generated correctly as you can see by the vsprintf()-related errors.

What you want to do is:

$this->validator->rule("equals", self::PASSWORD_KEY_NAME, self::CONFIRM_KEY_NAME);

Which is 'check that the value of the password field equals that of the confirm field'.

TODO: throw an exception for wrong parameter count

GrishinSergey commented 6 years ago

Thank you for help. Yes, will be cool, if library throws an exception, when params not correctly passed? BTW, how about set types as standart PHP7?

willemwollebrants commented 6 years ago

PHP7 types etc will be for the 2.x version, whenever I get round do that. Might be a while still, I'm afraid :/