DEPRECATION WARNING

This documentation is not using the current rendering mechanism and is probably outdated. The extension maintainer should switch to the new system. Details on how to use the rendering mechanism can be found here.

Form validator

A form validator is called after the submission of the form: it allows managing all its specific validation rules, and any additional process which could depend on its data.

The goal of FormZ is to automatize a maximum of processes, especially the validation ones. However, it is likely that complex forms require more advanced mechanism, which fit their specific needs. In this case, it is possible to very easily hook into the core of the validation process, and customize its behaviour by using the functions listed below.

Tip

If you do not need any additional process for your form, you can use the default validator provided by FormZ: \Romm\Formz\Validation\Validator\Form\DefaultFormValidator.

API

A form validator gives you access to the following variables/functions:


Form instance

Property

protected $form;

Type

Romm\Formz\Form\FormInterface

Description

In this variable is stored the form instance which is sent when the user launched the submission. You will find all the submitted data.

Validation result

Property

protected $result;

Type

TYPO3\CMS\Extbase\Error\Result

Description

In this variable is stored the validation result. You can interact with it according to your needs: especially adding/removing errors.

This variable is returned to the controller at the end of the validation, which means if the result contains at least one error, the form will be considered as invalid.

Pre-validation process

Fonction

protected function beforeValidationProcess()
{
   // ...
}

Return

/

Description

This function is called just before the launch of the form's fields validation process. You may override it to configure your own behaviours: for instance the (de)activation of fields depending on your own criteria.

During-validation process

Fonction

protected function *field*Validated()
{
   // ...
}

Return

/

Description

Every time the validation of a field ends, a function containing the name of the field is called. The function begins with the name of the field in lowerCamelCase, and ends with Validated (note the upper case V).

Example for the field firstName, the name of the function will be firstNameValidated(); if this function does exist in the class, it will be called, and you can then execute whatever you want.

Post-validation process

Fonction

protected function afterValidationProcess()
{
   // ...
}

Return

/

Description

This function is called just after the fields validation. Override it to implement you own specific behaviours.

Note that you can still use $this->result.


Form validation example

You can find below an example of a form validator.

<?php
namespace MyVendor\MyExtension\Validation\Validator\Form;

use Romm\Formz\Validation\Validator\Form\AbstractFormValidator;
use MyVendor\MyExtension\Utility\SimulationUtility;
use MyVendor\MyExtension\Form\SimulationForm

class ExampleFormValidator extends AbstractFormValidator {

    /**
     * @var SimulationForm
     */
    protected $form;

    /**
     * If there was no error in the form submission, the simulation process
     * runs. If the simulation result contains errors, we cancel the form
     * validation.
     */
    protected function afterValidationProcess()
    {
        if (false === $this->result->hasErrors()) {
            $simulation = SimulationUtility::simulate($this->form);

            if (null === $simulation) {
                $error = new Error('Simulation error!', 1454682865)
                $this->result->addError($error);
            } else {
                $this->form->setSimulationResult($simulation);
            }
        }
    }
}