Interface ValidatingField<E extends HasValue.ValueChangeEvent<V>,V>

Type Parameters:
E - value change event type
V - internal binder bean type
All Superinterfaces:
HasValue<E,V>, Serializable
All Known Implementing Classes:
BinderCustomField, FieldBuilderCustomField, NullableField

public interface ValidatingField<E extends HasValue.ValueChangeEvent<V>,V> extends HasValue<E,V>
Implemented by fields that are themselves capable of validating field values.

The main purpose of doing things this way is modularity: it allows a field to define its own validation logic, rather than relying on some external component to do so. Of course, this only works for validation checks that can be done by the field itself and are therefore independent of the containing context (e.g., other fields).

This interface would typically be implemented by complex fields composed from sub-fields for which some overall validation is required. For example, imagine a date range field composed of two LocalDate fields representing the start and end date: an overall validation would require that the start date be prior to the end date.

However, for any ValidatingField validation to take effect, some mechanism must register the validation with an appropriate Binder. This happens automatically for fields created by a FieldBuilder, but can also be done manually via addValidationTo().

See Also:
  • Method Details

    • validate

      ValidationResult validate(V value, ValueContext context)
      Validate this instance.
      Parameters:
      value - field value to validate
      context - the value context for validation
      Returns:
      the validation result
    • addValidationTo

      default <BEAN, TARGET extends V> Binder.BindingBuilder<BEAN,TARGET> addValidationTo(Binder.BindingBuilder<BEAN,TARGET> builder)
      Add this instance as a Validator to the given binding.
      Type Parameters:
      BEAN - binder bean type
      TARGET - binding target type
      Parameters:
      builder - binding builder
      Returns:
      updated binding builder
      Throws:
      IllegalArgumentException - if builder is null