Password validator

This part is a simple example how you can write your own validator. In this case we want to write a validator that checks for the password strenght in our registration.

Implementing PasswordValidator.php

Adding your own validator is not really hard. First we create the validator itself. In this case we call it PasswordValidator.php and the class has the same name. The class has to extend \TYPO3\Flow\Validation\Validator\AbstractValidator. The only required function is the function isValid with the parameter $value. This is the parameter that will be validated.

The type of the $value is completely free. In this case the type is an array containing the password and the re-typed password. If you take a look at the code you will also see that we work with options. This options can be set in the annotations but we will see later how this works.

Changing the controller

To activate the validation it is almost the same as adding a normal validation. The main difference is that we have to add the complete class identifier in the annotation. The part in brackets behind the class name are the options we see in the code snippet above and are used to define the minimum length of our password. We can access this options like seen above with $this->options[‘minimumLength’].

Compared to our previously created createAction we have to change the parameter since we can only pass one parameter to the validator so we need an array here containing both passwords.

Changing the html form

In the part of the tutorial where we created the registration and the login we used the parameters pass and pass2. Since the createAction requires an array now we have to change this two parameters to password[0] and password[1].

Try it now. It should work and you should get an error message depending on your error.

Password strength

Yes I know, this is not a real strong password now but feel free to add some fancy regex stuff to check for numbers, signs or whatever you want.

Additional if you want some nice error messages displayed, take a look at the errorAction inside the \TYPO3\Flow\Mvc\Controller\ActionController and overwrite it if you want to have more detailed error messages.



Leave a Reply

Your email address will not be published. Required fields are marked *