Validators
The form framework ships a set of server-side validators (derived from Extbase validators) which you can use in form elements. Some validators can only be used for certain elements, e.g. the "Date range validator" can only be used for "Date" elements. Some form elements (like "Email") come with validators.
You can define your own validation error messages using the validation
property. These error messages can also be set in the form editor.
Client-side validation
In the form framework, HTML 5-based frontend validation can be added to form elements, but JavaScript validation is not included. The TYPO3 core have no plans to add this functionality at the current time. However, you can add it yourself if required. Examples of reliable and well-maintained projects are Parsley and jQuery Validation.
Localization of client side validation
Display of validation messages is browser-specific and not generated by TYPO3 so these messages cannot easily be changed. However, you can use JavaScript to change validation messages. See Stack Overflow for more information.
Server-side validation
Alphanumeric validator (Alphanumeric)
The "Alphanumeric validator"
checks for alphanumeric strings. Alphanumeric is defined as a combination of
alphabetic and numeric characters [A-.
Number of submitted values validator (Count)
The "Number of submitted values validator" checks if a value contains a specific number of elements. The validator has two options:
- Minimum [
options.]: The minimum count to accept.minimum - Maximum [
options.]: The maximum count to accept.maximum
New in version 14.2 Feature: #105708 - Multiple file upload for EXT:form elements
Use the
Count
validator with
File and
Image elements to limit the number of uploaded files. The
element property
multiple
must be enabled.
Date range validator (DateRange )
The "Date range validator" checks if a value is a valid DateTime object and within a specified date range. The range can be defined by providing a minimum and/or maximum date. The validator has two options:
- Format [
options.]: The format of the absolute minimum and maximum dates. Default: [format Y-].m- d - Minimum date [
options.]: The minimum date. Use an absolute date in the formatminimum Y-or a relative date expression.m- d - Maximum date [
options.]: The maximum date. Use an absolute date in the formatmaximum Y-or a relative date expression.m- d
Absolute dates use the format 'Y-m-d', which represents the RFC 3339 'full-date' format.
New in version 14.2 Feature: #106681 - Support relative date formats in DateRange validator
Before, the options
minimum
and
maximum
only accepted
absolute dates.
Relative date expressions follow the syntax of the PHP function
strtotime(), for example today,
tomorrow, -18 years, or +1 month. They are evaluated when the form is
validated. You can mix absolute and relative dates.
The following example accepts only dates of birth at least 18 years in the past:
type: Date
identifier: date-of-birth
label: 'Date of birth'
validators:
-
identifier: DateRange
options:
minimum: '1900-01-01'
maximum: '-18 years'
The HTML attributes min and max of the rendered date input can also
contain relative date expressions. They are resolved to absolute Y-
dates when the form is rendered.
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 and maximum date can be configured through the validator options.
Date/time validator (DateTime )
The "Date/time validator"
checks if a value is a valid DateTime object. The date string is
expected to be formatted according to the W3C standard
which is YYYY-, for example 2005-.
Email validator (EmailAddress )
The "Email validator"
checks if a value is a valid email address. The format of a valid email
address is defined in RFC 3696.
This standard allows international characters and multiple
@ signs.
File size validator (FileSize )
The "File size validator" validates the size of a file resource. The validator has two options:
- Minimum [
options.]: The minimum file size. Use the formatminimum <size>B. For example:|K |M |G 10Mis 10 Megabytes. - Maximum [
options.]: The maximum file size. Use the formatmaximum <size>B. For example:|K |M |G 10Mis 10 Megabytes.
Use the format <size>B for file size, for example, 10M
is 10 megabytes. Note: the maximum file size also depends on the php.
settings of your environment.
Floating-point number validator (Float)
The "Floating-point number validator"
checks if a value is of type float or a string matching the regular
expression [0-.
Integer number validator (Integer)
The "Integer number validator" checks if a value is a valid integer.
Empty validator (NotEmpty )
The "Empty validator" checks if a value is not empty (i.e. equal to NULL, empty string, empty array or empty object).
Number validator (Number)
The "Number validator" checks if a value is a number.
Number range validator (NumberRange )
The "Number range validator" checks if a value is a number in a specified range. The validator has two options:
- Minimum [
options.]: The minimum value.minimum - Maximum [
options.]: The maximum value.maximum
Regular expression validator (RegularExpression )
The "Regular expression validator" checks if a value matches a specified regular expression. Delimiters or modifiers are not supported. The validator has one option:
- Regular expression [
options.]: The regular expression to use for validation, used as given.regular Expression
As an example, a user submits a domain name and the submitted value should only
contain the second and the top level domain, i.e. "typo3.org" instead of
"https://typo3.org". The regular expression for this would be /^.
String length validator (StringLength )
The "String length validator" checks if a value is a valid string and its length is within a specified range. The validator has two options:
- Minimum [
options.]: The minimum length of a valid string.minimum - Maximum [
options.]: The maximum length of a valid string.maximum
Non-XML text validator (Text)
The "Non-XML text validator" checks if a value is a valid piece of text (containing no XML tags). This basically means that tags are stripped out. 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. If you want to be sure of that, use a customized regular expression or filter on output.
Translation of validation messages
To learn more about this topic, see here.
Custom validator implementations
Validators belong to configuration prototypes in a validators.
Set the implementation property of the prototype to your
own validator classes.
prototypes:
standard:
validatorsDefinition:
Custom:
implementationClassName: 'VENDOR\MySitePackage\Domain\Validation\CustomValidator'
Add options to your validator and provide a default value your:
prototypes:
standard:
validatorsDefinition:
Custom:
implementationClassName: 'VENDOR\MySitePackage\Domain\Validation\CustomValidator'
options:
yourCustomOption: 'Jurian'
You can override the default value in 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 uses Extbase validators. That said,
your own validators should extend
\TYPO3\.
Read more in "TYPO3 Explained":
Writing a custom Extbase validator.
Validating multi-value fields
New in version 14.2 Feature: #105708 - Multiple file upload for EXT:form elements
Some form elements submit several values as an
Object, for example a
File element with
multiple
enabled. By default, a
validator receives the whole
Object. Use this for
validators that check the collection as a whole, such as the
Count
validator.
If a validator checks single items, implement the marker interface
Object.
The validator is then called once for each item. The validators
Mime and
File work this way.
use TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator;
use TYPO3\CMS\Form\Mvc\Validation\ObjectStorageElementValidatorInterface;
final class CustomFileValidator extends AbstractValidator implements
ObjectStorageElementValidatorInterface
{
public function isValid(mixed $value): void
{
// $value is a single item of the ObjectStorage,
// for example a FileReference
}
}
For values that are not an
Object, the interface
has no effect.