Package org.apache.tapestry5

## Interface Validator<C,​T>

- All Known Implementing Classes:
    `AbstractValidator`, `Checked`, `Email`, `Max`, `MaxLength`, `Min`, `MinLength`, `None`, `Regexp`, `Required`, `Unchecked`

      public interface Validator<C,​T>

  Used by a `Field` to enforce a constraint related to a form submission. Validators themselves are stateless singletons. Validators are usually encapsulated inside a `FieldValidator`.
  See Also:
    `FieldValidationSupport`, `FieldValidatorDefaultSource`

- - ### Method Summary

    All Methods Instance Methods Abstract Methods
    Modifier and Type | Method | Description
    --- | --- | ---
    `Class<C>` | `getConstraintType()` | Returns the type of constraint value used with this validator.
    `String` | `getMessageKey()` | Returns the message key, within the validation messages, normally used by this validator.
    `Class<T>` | `getValueType()` | Returns the value type associated with this validator.
    `boolean` | `isRequired()` | Returns true if the validator should be invoked for null or blank (empty string) values.
    `void` | `render​(Field field, C constraintValue, MessageFormatter formatter, MarkupWriter writer, FormSupport formSupport)` | Hook used by components to allow the validator to contribute additional attributes or (more often) client-side JavaScript (via the `FormSupport.addValidation(Field, String, String, Object)`).
    `void` | `validate​(Field field, C constraintValue, MessageFormatter formatter, T value)` | Invoked after the client-submitted value has been `translated` to check that the value conforms to expectations (often, in terms of minimum or maximum value).

- - ### Method Detail

    - #### getConstraintType

          Class<C> getConstraintType()

      Returns the type of constraint value used with this validator. Constraint values are used to parameterize a validator, for example a "maxLength" validator will have a constraint value of type int (the maximum length allowed). For constraints that do not have a constraint value, this method returns null.

    - #### getValueType

          Class<T> getValueType()

      Returns the value type associated with this validator. `validate(Field, Object, MessageFormatter, Object)` will only be invoked when the value is assignable to the validator's value type.

    - #### getMessageKey

          String getMessageKey()

      Returns the message key, within the validation messages, normally used by this validator. This is used to provide the `MessageFormatter` passed to `validate(Field, Object, MessageFormatter, Object)` (unless overridden).
      Returns:
        a message key

    - #### validate

          void validate​(Field field,
                        C constraintValue,
                        MessageFormatter formatter,
                        T value)
                 throws ValidationException

      Invoked after the client-submitted value has been `translated` to check that the value conforms to expectations (often, in terms of minimum or maximum value). If and only if the value is approved by all Validators is the value applied by the field.
      Parameters:
        `field` - the field for which a client submitted value is being validated
        `constraintValue` - the value used to constrain
        `formatter` - Validation messages, in the appropriate locale
        `value` - the translated value supplied by the user
      Throws:
        `ValidationException` - if the value violates the constraint

    - #### isRequired

          boolean isRequired()

      Returns true if the validator should be invoked for null or blank (empty string) values. This is generally false, but is true for validators that enforce that a non-blank value is required. This is the basis of the `Field.isRequired()` property.

    - #### render

          void render​(Field field,
                      C constraintValue,
                      MessageFormatter formatter,
                      MarkupWriter writer,
                      FormSupport formSupport)

      Hook used by components to allow the validator to contribute additional attributes or (more often) client-side JavaScript (via the `FormSupport.addValidation(Field, String, String, Object)`).
      Parameters:
        `field` - the field which is currently being rendered
        `constraintValue` - the value used to constrain input
        `formatter` - validation message, in the appropriate locale
        `writer` - markup writer, allowing additional attributes to be written into the active element
        `formSupport` - used to add JavaScript