Validators ensure that field input is entered in the 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 in a chain, one after the other.

This field validator definition is part of the Magnolia 6 UI framework. The fully qualified class name is info.magnolia.ui.field.FieldValidatorDefinition.

If you work with the Magnolia 5 UI framework, see Field validators for Magnolia 5 UI instead.

Example definition

email:
  label: Email
  $type: textField
  required: true
  validators:
    email:
      $type: emailValidator
      errorMessage: Enter a valid email address 

Validator properties

validators

required

List of field validator definition items. Any value must be a subtype of info.magnolia.ui.field.FieldValidatorDefinition.

<validator name>

required

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

class

required (unless $type is used)

Validator definition class. Use the fully qualified class name. See Validator types for possible values.

You can also create your own validator definition class.

$type

You can use this as a shortcut for class if the definition class is annotated with info.magnolia.ui.field.ValidatorType. The proper value is defined by the annotation.

Example class annotation
@ValidatorType("emailValidator")
public class EmailValidatorDefinition extends ConfiguredFieldValidatorDefinition {
...
}

To use the $type property in YAML, see Example definition.

pattern

required (used only in regex validation)

Regular expression pattern when using regexpValidator.

factoryClass

optional

Fully qualified name of the Java class that creates the validator. The class must implement info.magnolia.ui.field.FieldValidatorFactory. Validator definitions specify a default factory class. This means that unless you want to use a custom implementation, you do not need to configure a factory class.

errorMessage

optional

Error message displayed on invalid input. The value can be literal or retrieved from the message bundle with a key. Use alphanumeric characters in literal values.

Custom validators

To write your own validator definition class:

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

Validator types

$typeclass and description

emailValidator

info.magnolia.ui.field.EmailValidatorDefinition

Validates an email address. Delegates to Vaadin EmailValidator.

nodeNameValidator

info.magnolia.ui.editor.validator.NodeNameValidatorDefinition

Validates a JCR node name as unique.

regexpValidator

info.magnolia.ui.field.RegexpValidatorDefinition

Validates a regular expression defined in the pattern property.

#trackbackRdf ($trackbackUtils.getContentIdentifier($page) $page.title $trackbackUtils.getPingUrl($page))
  • No labels