Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Status
colourRed
titleDeprecated

Note

This field validator definition has been deprecated since Magnolia 6.0. It is part of the Magnolia 5 UI framework.

For the updated implementation, see Field validators for Magnolia 6 UI instead.

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.

Table of Contents

Common validator properties

Simple validator definition:

Localtab Group
Localtab
activetrue
titleYAML file
Code Block
languagejs
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
Localtab
titleJCR node
Advanced Tables - Table Plus
enableHeadingAttributesfalse
enableSortingfalse
classm5-configuration-tree
enableHighlightingfalse
Node nameValue

Mgnl n
form


Mgnl n
tabs


Mgnl n
tabUser


Mgnl n
fields


Mgnl n
emailAddress


Mgnl n
validators


Mgnl n
email


Mgnl p
class

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

Mgnl p
errorMessage

Enter a valid email address

Mgnl p
fieldType

text

Mgnl p
label

Email

Properties:

<field name>Name of field.

validators

required

Contains the validator definition.

<validator name>

required

Arbitrary node name. Use a name that describes the validator type.

class

required

Validator definition class. Use the fully-qualified class name.

factoryClass

optional

Fully qualified name of the Java class that creates the validator. The class must implement

Javadoc resource link
classNameinfo.magnolia.ui.form.validator.factory.FieldValidatorFactory
renderTypeasynchronous
. 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.

errorMessage

optional

Text displayed to the user on invalid input. Text string or message bundle key.

i18nBasename

optional

Message bundle for localized messages. This property can be set at dialog, form, tab or field level.

pattern

required for regex validation

Regular expression pattern when using the  info.magnolia.ui.form.validator.definition.RegexpValidatorDefinition class

List of validator definition classes

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

Validates a regular expression given in the pattern property.

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

Validates an email address. Delegates to a Vaadin EmailValidator.

Custom regular expressions

The easiest custom validator is a regular expression. Use the 

Javadoc resource link
classNameinfo.magnolia.ui.form.validator.definition.RegexpValidatorDefinition
renderTypeasynchronous
class and define your own regular expression in the pattern property.

Here is an example of validating a ZIP code (U.S. postal code).

Localtab Group
Localtab
activetrue
titleYAML file
Code Block
languagejs
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
Localtab
titleJCR node
Localtab
titleJCR node
Advanced Tables - Table Plus
enableHeadingAttributesfalse
enableSortingfalse
classm5-configuration-tree
enableHighlightingfalse
Node nameValue

Mgnl n
form


Mgnl n
tabs


Mgnl n
tabUser


Mgnl n
fields


Mgnl n
zipCode


Mgnl n
validators


Mgnl n
zip


Mgnl p
class

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

Mgnl p
errorMessage

Enter a valid U.S. ZIP code using the format 12345 or 12345-1234

Mgnl p
pattern

^\d{5}(-\d{4})?$

Mgnl p
fieldType

text

Mgnl p
label

Zip code

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 
    Javadoc resource link
    classNameinfo.magnolia.ui.form.validator.factory.AbstractFieldValidatorFactory
    renderTypeasynchronous
    . Implement the createValidator method.
  3. Create a validator definition class that extends 
    Javadoc resource link
    classNameinfo.magnolia.ui.form.validator.definition.ConfiguredFieldValidatorDefinition
    renderTypeasynchronous
    . In the definition class, set the factory class.

Example: Checking that a username is unique.

  • Javadoc resource link
    classNameinfo.magnolia.security.app.dialog.field.validator.UniqueUserNameValidator
    renderTypeasynchronous
  • Javadoc resource link
    classNameinfo.magnolia.security.app.dialog.field.validator.UniqueUserNameValidatorFactory
    renderTypeasynchronous
  • Javadoc resource link
    classNameinfo.magnolia.security.app.dialog.field.validator.UniqueUserNameValidatorDefinition
    renderTypeasynchronous

Checking for null values

Info
iconfalse
titleBest practice
Excerpt

Define default values for fields or check for null values. This ensures that you can submit the form. For example, if you define an option group field and do not provide a default value, an error will occur when a user submits the form by email. The email processor does not check for null values 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:

Code Block
languagehtml/xml
field: ${field!"value not provided"}