If this tutorial is helpful to you, you can donate some money to the tutorial author via Paypal.

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\FLOW3\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.
Layh.Events\Validation\Validator\PasswordValidator.php 

<?php
namespace Layh\Events\Validation\Validator;
 
/*                                                              * 
 * This script belongs to the FLOW3 package "Layh.Events".    */
 
use TYPO3\FLOW3\Annotations as FLOW3;
 
/**
 * Password validator for the Layh.Events package
 * Checks the password if it has a minimum length and compares password-1
 * with password-2
 *
 */
class PasswordValidator extends \TYPO3\FLOW3\Validation\Validator\AbstractValidator {
 
    /**
     * Check if $value is valid. If it is not valid, needs to add an error
     * to Result.
     *
     * @param array $value
     * @return void
     */
    protected function isValid($value) {
         
            // get the option for the validation
        $minimumLength = $this->options['minimumLength'];
 
            // check for empty password
        if ($value[0] === '') {
            $this->addError('The password is to weak.', 1221560718);
        }
            // check for password length
        if(strlen($value[0]) < $minimumLength) {
            $this->addError('The password is to short, minimum lenght is ' . $minimumLength, 1221560718);
        }
 
            // check that the passwords are the same
        if(strcmp($value[0], $value[1]) != 0) {
            $this->addError('The password do not match!', 1221560718);
        }
    }
 
}

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.
Layh.Events\Controller\LoginController.php 

/**
 * saveAction
 * save the new registration
 *
 * @param string $name
 * @validate $name StringLength(minimum=3, maximum=10)
 *
 * @param array $password
 * @validate $password \Layh\Events\Validation\Validator\PasswordValidator(minimumLength = 3)
 *
 * @return void
 */
public function createAction($name, $password) {
 
        // set a default role for now
    $defaultRole = 'Visitor';
 
        // create a account with password an add it to the accountRepository
    $account = $this->accountFactory->createAccountWithPassword($name, $password[0], array($defaultRole));
    $this->accountRepository->add($account);
 
        // add a message and redirect to the login form
    $this->flashMessageContainer->add('Account created. Please login.');
    $this->redirect('index');
}

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.
Layh.Events/Resources/Private/Templates/Login/Register.html 

<f:form action="create" controller="Login" method="post" name="loginform">
    <label>User:</label>
    <f:form.textbox name="name"/>
    <br/>
    <label>Pass:</label>
    <f:form.password name="password[0]"/>
    <br/>
    <label>Pass repeat:</label>
    <f:form.password name="password[1]"/>
    <br/>
    <f:form.submit value="Register"/>
</f:form>

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\FLOW3\MVC\Controller\ActionController and overwrite it if you want to have more detailed error messages.


Was this tutorial helpful? So, you can donate some money to the tutorial author via Paypal.