Magnolia 5.3 reached end of life on June 30, 2017. This branch is no longer supported, see End-of-life policy.

Page tree
Skip to end of metadata
Go to start of metadata

Validators ensure that field input is entered in correct format and length. For example, an email address should adhere to an established syntax john.smith@magnolia-cms.com.

Default validators

ValidatorDescription
Regular expression

Validates a regular expression given in the pattern property.

Class: info.magnolia.ui.form.validator.definition.RegexpValidatorDefinition

Email

Valid email address. Delegates to a Vaadin EmailValidator .

Class:  info.magnolia.ui.form.validator.definition.EmailValidatorDefinition

Validating a field

Here is an example of validating an email address.

Node nameValue

 email

 

 validators

 

 email

 

 class

info.magnolia.ui.form.validator.definition.EmailValidatorDefinition

 errorMessage

validation.message.non.valid.email

Properties:

  • class : Validator definition class. Use the fully-qualified class name. Mandatory.

  • factoryClass : Fully qualified name of the Java class that creates the validator. The class must implement the  FieldValidatorFactory  interface. Optional.

  • errorMessage : Text displayed to the user on invalid input. Text string or message bundle key. Optional.
  • i18nBasename Message bundle for localized messages. This property can be set at dialog, form, tab or field level. Optional.
  • pattern : Regular expression pattern when using the  info.magnolia.ui.form.validator.definition.RegexpValidatorDefinition class.

You can add multiple validators to a field. The validators are executed like a chain, one after the other.

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.

Custom regular expressions

The easiest custom validator is a regular expression. Use the info.magnolia.ui.form.validator.definition.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).

Node nameValue

 zip

 

 validators

 

 zipCode

 

 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})?$

Custom validators

To write your own validator class:

  1. Create a validator class that performs the actual validation. 
  2. Create a validator factory class that extends  AbstractFieldValidatorFactory . Implement the createValidator method.
  3. Create a validator definition class that extends  ConfiguredFieldValidatorDefinition . In the definition class, set the factory class.

Example: Checking that a username is unique.

Checking for null values

Best practice

Define default values for fields or check for null values. This ensures that you can submit the form. For example, if you define an optional radio button field and don't provide a default value, an error occurs when a user submits the form by mail. The email processor does not check for null value in the template. It is good practice to check for null values of any variables you call.

You can check for null values and provide a default value in a Freemarker script like this:

field: ${field!"value not provided"}