Magnolia 5.6 reached end of life on June 25, 2020. This branch is no longer supported, see End-of-life policy.
Validators ensure that field input is entered in correct format and length. For example, you can validate that an email address adheres to a syntax such as first.last@company.com
. You can add multiple validators to a field. The validators are executed like a chain, one after the other.
Simple validator definition:
form: tabs: - name: tabUser label: User info fields: - name: emailAddress class: info.magnolia.ui.form.field.definition.TextFieldDefinition label: Email validators: - name: email class: info.magnolia.ui.form.validator.definition.EmailValidatorDefinition errorMessage: Enter a valid email address
Node name | Value |
---|---|
form |
|
tabs |
|
tabUser |
|
fields |
|
emailAddress |
|
validators |
|
| |
class | info.magnolia.ui.form.validator.definition.EmailValidatorDefinition |
errorMessage | Enter a valid email address |
class | info.magnolia.ui.form.field.definition.TextFieldDefinition |
label |
Properties:
<field name> | Name of field. |
| required Contains the validator definition. |
| required Arbitrary node name. Use a name that describes the validator type. |
| required Validator definition class. Use the fully-qualified class name. |
| optional Fully qualified name of the Java class that creates the validator. The class must implement |
| optional Text displayed to the user on invalid input. Text string or message bundle key. |
| optional Message bundle for localized messages. This property can be set at dialog, form, tab or field level. |
| required for regex validation Regular expression pattern when using the |
info.magnolia.ui.form.validator.definition.RegexpValidatorDefinition | Validates a regular expression given in the |
info.magnolia.ui.form.validator.definition.EmailValidatorDefinition | Validates an email address. Delegates to a Vaadin |
The easiest custom validator is a regular expression. Use the class and define your own regular expression in the
pattern
property.
Here is an example of validating a ZIP code (U.S. postal code).
form: tabs: - name: tabUser label: User info fields: - name: zipCode class: info.magnolia.ui.form.field.definition.TextFieldDefinition label: Zip code validators: - name: zip class: info.magnolia.ui.form.validator.definition.RegexpValidatorDefinition pattern: ^\d{5}(-\d{4})?$ errorMessage: Enter a valid U.S. ZIP code using the format 12345 or 12345-1234
Node name | Value |
---|---|
form |
|
tabs |
|
tabUser |
|
fields |
|
zipCode |
|
validators |
|
zip |
|
class | info.magnolia.ui.form.validator.definition.RegexpValidatorDefinition |
errorMessage | Enter a valid U.S. ZIP code using the format 12345 or 12345-1234 |
pattern | ^\d{5}(-\d{4})?$ |
class | info.magnolia.ui.form.field.definition.TextFieldDefinition |
label | Zip code |
To write your own validator class:
createValidator
method.Example: Checking that a username is unique.