1

I've currently got a regular expression set up for validating my passwords:

/^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*\W).{6,}$/

This checks for at least 1 uppercase and lowercase character, 1 digit, 1 special character/non-word character and minimum of 6 characters.

I'm now porting all of my validation rules over to Respect Validation. I've got most of it set up nicely but having difficulty with password validation.

I'm aware of the regex method:

Validator::regex('/^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*\W).{6,}$/')->setName('Password');

But this returns an error message like this:

Password must validate against "/^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*\W).{6,}$/"

I'm wanting to chain methods together so each rule is set in the validation chain. So if it's a digit you are missing it returns something like "Password must contain at least 1 digit", if you're missing an uppercase character it returns "Password must contain at least 1 uppercase character" and so on.

I thought it might be done using the allOff method, whereby you can set individual rules that will pass globally if all inner rules pass.

As a test, I tried to detect if the string contains a digit and an alpha character like this:

Validator::allOf(Validator::digit(), Validator::alpha())->setName('Password');

The results:

$password = 'test'; // Password must contain only digits (0-9)
$password = '1234'; // Password must contain only letters (a-z)
$password = 'test1234' // Password must contain only digits (0-9)

Looking at it now, it makes perfect sense why I got those errors, but I can't see how else to do this without the regex method. Is what I'm after possible is it just best to use the regex method in this case, I've gone through the rule list a few times but I've probably missed the one I'm after as there is quite a few.

Any help is appreciated.

no.
  • 2,241
  • 3
  • 25
  • 40
  • Unrelated to [tag:respect-validation], but you might find [Reference - Password Validation](https://stackoverflow.com/q/48345922/3600709) useful. – ctwheels Mar 27 '19 at 15:23

0 Answers0