.. include:: /Includes.rst.txt


.. _concepts-validators:

Validators
==========

The form framework ships a set of server-side validators, which implement
Extbase validators. Be aware that not all of the existing validators are
available for each form element, e.g. the "Date range validator" is
only available for the "Date" element. Furthermore, some form elements
(like "Email") already contain reasonable validators.

With the help of the form element property ``validationErrorMessages``
you can define custom validation error messages. The message can also
be set within the form editor.


.. _concepts-validators-client-side-validation:

Client-side validation
----------------------

If a form element is configured accordingly, the form framework adds
HTML 5 based frontend validation. Nevertheless, there is no JavaScript
validation included by default. We as the TYPO3 core have no plans to
opt for a specific solution. This has to be integrated manually.
Reliable and maintained projects are `Parsley <https://github.com/guillaumepotier/Parsley.js>`_
and `jQuery Validation <https://github.com/jquery-validation/jquery-validation>`_.


.. _concepts-validators-localization-client-side-validations:

Localization of client side validations
"""""""""""""""""""""""""""""""""""""""

The displayed validation message is a browser specific text. The output
is not generated by TYPO3 and therefore you cannot change it easily.
Nevertheless, there is a JavaScript solution for changing the validation
message. See `Stack Overflow <http://stackoverflow.com/questions/5272433/html5-form-required-attribute-set-custom-validation-message>`_
for more information.


.. _concepts-validators-server-side-validation:

Server-side validation
----------------------


.. _concepts-validators-alphanumeric:

Alphanumeric validator
""""""""""""""""""""""

The 'Alphanumeric validator' checks for alphanumeric strings.
Alphanumeric is defined as a combination of alphabetic and numeric
characters [A-Z + 0-9].


.. _concepts-validators-date_time:

Date/ time validator
""""""""""""""""""""

The 'Date/ time validator' checks if the given value is a valid DateTime
object. The date string is expected to be formatted according to the `W3C
standard <http://www.w3.org/TR/NOTE-datetime.html>`_ which is
"YYYY-MM-DDT##:##:##+##:##", for example "2005-08-15T15:52:01+00:00".


.. _concepts-validators-date_range:

Date range validator
""""""""""""""""""""

The 'Date range validator' checks if the given value is a valid DateTime
object and in-between a specified date range. The range can be defined by
providing a minimum and/ or maximum date. The validator has 2 options:

- Minimum date: The minimum date.
- Maximum date: The maximum date.

The properties ``minimum`` and ``maximum`` must have the format 'Y-m-d' which
represents the RFC 3339 'full-date' format.

Read more: https://www.w3.org/TR/2011/WD-html-markup-20110405/input.date.html

The input must be a DateTime object. This input can be tested against a minimum
date and a maximum date. The minimum date and the maximum date are strings. The minimum
date and the maximum date can be configured through the validator options.


.. _concepts-validators-email:

Email validator
"""""""""""""""

The 'Email validator' checks if the given value is a valid email address.
The format of a valid email address is defined in `RFC 3696 <https://tools.ietf.org/html/rfc3696>`_.
The standard allows international characters and the multiple appearance
of the @ sign.


.. _concepts-validators-empty:

Empty validator
"""""""""""""""

The 'Empty validator' checks if the given value is not empty (NULL,
empty string, empty array or empty object).


.. _concepts-validators-filesize:

File size validator
"""""""""""""""""""

The 'File size validator' validates a file resource regarding its file
size. The validator has 2 options:

- Minimum: The minimum file size to accept.
- Maximum: The maximum file size to accept.

Use the format <size>B|K|M|G when entering file sizes. For example: 10M
means 10 megabytes. Please keep in mind that the maximum file size also
depends on the php.ini settings of your environment.


.. _concepts-validators-floating_point:

Floating-point number validator
"""""""""""""""""""""""""""""""

The 'Floating-point number validator' checks if the given value is of
type float or a string matching the regular expression [0-9.e+-].


.. _concepts-validators-integer:

Integer number validator
""""""""""""""""""""""""

The 'Integer number validator' checks if the given value is a valid
integer.


.. _concepts-validators-text:

Non-XML text validator
""""""""""""""""""""""

The 'Non-XML text validator' checks if the given value is a valid text
(contains no XML tags). This basically means, that tags are stripped.
In this special case quotes are not encoded (see `filter_var()
<https://php.net/filter_var>`_ for more information.

Be aware that the value of this check entirely depends on the output
context. The validated text is not expected to be secure in every
circumstance, if you want to be sure of that, use a customized regular
expression or filter on output.


.. _concepts-validators-count:

Number of submitted values validator
""""""""""""""""""""""""""""""""""""

The 'Number of submitted values validator' checks if the given value
contains the specified amount of elements. The validator has 2 options:

- Minimum: The minimum number of submitted values.
- Maximum: The maximum number of submitted values.


.. _concepts-validators-number_range:

Number range validator
""""""""""""""""""""""

The 'Number range validator' checks if the given value is a number in
the specified range. The validator has 2 options:

- Minimum: The minimum value to accept.
- Maximum: The maximum value to accept.


.. _concepts-validators-mimetype:

MimeType validator
""""""""""""""""""

The 'MimeType validator' checks if the given value is a FileReference
of the configured type (one of the */* IANA media types).


.. _concepts-validators-string_length:

String length validator
"""""""""""""""""""""""

The 'String length validator' checks if the given value is a valid string
and its length is in the specified range. The validator has 2 options:

- Minimum: The minimum length of the string.
- Maximum: The maximum length of the string.


.. _concepts-validators-regular_expressions:

Regular expression validator
""""""""""""""""""""""""""""

The 'Regular expression validator' checks if the given value matches
the specified regular expression. Delimiters or modifiers are not
supported. The validator has 1 option:

- Regular expression: The regular expression to use for validation.

Imagine the following example. You want the user to provide a domain
name. The submitted value shall only contain the second and the top
level domain, e.g. "typo3.org" instead of "https://typo3.org". The
regular expression for this use case would be
:code:`/^[-a-z0-9]+\.[a-z]{2,6}$/`.


.. _concepts-validators-validation-message-translation:

Translation of validation messages
----------------------------------

To learn more about this topic, please continue :ref:`here<concepts-frontendrendering-translation-validationerrors>`.


.. _concepts-validators-customvalidatorimplementations:

Custom validator implementations
--------------------------------

Validators belong to a certain ``prototype`` and are defined within the
``validatorsDefinition``. The property ``implementationClassName`` is used
for the validator implementation.

.. code-block:: yaml

   TYPO3:
     CMS:
       Form:
         prototypes:
           standard:
             validatorsDefinition:
               Custom:
                 implementationClassName: 'VENDOR\MySitePackage\Domain\Validation\CustomValidator'

You can provide options for your validator using the property ``options``.
Those will be used as default values which can be overridden within a
specific ``form definition``.

Define the default value of the option ``yourCustomOption``:

.. code-block:: yaml

   TYPO3:
     CMS:
       Form:
         prototypes:
           standard:
             validatorsDefinition:
               Custom:
                 implementationClassName: 'VENDOR\MySitePackage\Domain\Validation\CustomValidator'
                 options:
                   yourCustomOption: 'Jurian'

Override the default value within your ``form definition``:

.. code-block:: yaml

   identifier: sample-form
   label: 'Simple Contact Form'
   prototype: standard
   type: Form

   renderables:
     -
       identifier: subject
       label: 'Name'
       type: Text
       validators:
         -
           identifier: Custom
           options:
             yourCustomOption: 'Mathias'

As mentioned above EXT:form implements Extbase validators. That said,
your own validators should extend :php:`\TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator`.
Read more about this topic in "TYPO3 Explained":
:ref:`t3coreapi:extbase_domain_validator`.
