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.

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 and jQuery Validation.

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 for more information.

Server-side validation

Alphanumeric validator

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

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 which is “YYYY-MM-DDT##:##:##+##:##”, for example “2005-08-15T15:52:01+00:00”.

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 date string for the above mentioned fields is expected to be formatted according to the W3C standard which is “YYYY-MM-DDT##:##:##+##:##”, for example “2005-08-15T15:52:01+00:00”.

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. The standard allows international characters and the multiple appearance of the @ sign.

Empty validator

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

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.

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+-].

Integer number validator

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

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() 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.

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.

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 vlaue to accept.

MimeType validator

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

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.

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 /^[-a-z0-9]+\.[a-z]{2,6}$/.

Translation of validation messages

To learn more about this topic, please continue here.

Custom validator implementations

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

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:

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

Override the default value within your form definition:

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 TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator. Read more about this topic in the Extbase docs.