Magnolia 5.7 reached extended end of life on May 31, 2022. Support for this branch is limited, see End-of-life policy. Please note that to cover the extra maintenance effort, this EEoL period is a paid extension in the life of the branch. Customers who opt for the extended maintenance will need a new license key to run future versions of Magnolia 5.7. If you have any questions or to subscribe to the extended maintenance, please get in touch with your local contact at Magnolia.
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.
Common validator properties
Simple validator definition:
form: tabs: - name: tabUser label: User info fields: - name: emailAddress fieldType: text 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 | |
email | |
class | info.magnolia.ui.form.validator.definition.EmailValidatorDefinition |
errorMessage | Enter a valid email address |
fieldType | text |
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 FieldValidatorFactory . Validator definitions specify a default factory class, meaning that unless you want to use a custom implementation you don't need to configure a factory class. |
| 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 |
| optional for SafeHtmlValidatorDefinition (Magnolia 5.7.21+) Custom list of html |
| optional for SafeHtmlValidatorDefinition (Magnolia 5.7.21+) Custom list of allowedAttributes: abbr: tag: a attributes: [ title ] Only |
| optional for SafeHtmlValidatorDefinition (Magnolia 5.7.21+) Custom list of allowedAttributes: a: tag: a attributes: [ href ] allowedProtocols: a: tag: a attribute: href protocols: [ https, mailto ] In this case |
List of validator definition classes
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 |
info.magnolia.ui.form.validator.definition.SvgUploadValidatorDefinition | Validates SVG file uploads against malicious content. |
info.magnolia.ui.form.validator.definition.SafeHtmlValidatorDefinition | Validates text against malicious HTML. (Magnolia 5.7.21+) |
Custom regular expressions
The easiest custom validator is a regular expression. Use the RegexpValidatorDefinition 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 fieldType: text 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})?$ |
fieldType | text |
label | Zip code |
Custom validators
To write your own validator class:
- Create a validator class that performs the actual validation.
- Create a validator factory class that extends AbstractFieldValidatorFactory . Implement the
createValidator
method. - Create a validator definition class that extends ConfiguredFieldValidatorDefinition . In the definition class, set the factory class.
Example: Checking that a username is unique.
- UniqueUserNameValidator
- UniqueUserNameValidatorFactory
- UniqueUserNameValidatorDefinition