Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

equals rule is broken #257

Open
GrishinSergey opened this issue Jul 13, 2018 · 3 comments
Open

equals rule is broken #257

GrishinSergey opened this issue Jul 13, 2018 · 3 comments
Assignees

Comments

@GrishinSergey
Copy link

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
Copy link
Collaborator

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
Copy link
Author

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
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants