Annotation Interface AbstractFieldBuilder.EnabledBy

Enclosing class:
AbstractFieldBuilder<S extends AbstractFieldBuilder<S,T>,T>

@Retention(RUNTIME) @Target(METHOD) @Documented public static @interface AbstractFieldBuilder.EnabledBy
Causes the generated field to be automatically enabled or disabled based on the value of some other controlling field(s).

The target field will be enabled depending on whether the controlling field(s) have value(s) equal to their empty values. Often the controlling field is a single Checkbox (whose empty value is false), but any type of controlling field is supported.

When multiple controlling fields are specified, by default an AND condition applies: all of the controlling fields must have non-empty values for the target field to be enabled. You can change this to an OR condition by setting requireAll() to false.

Whenever the target field is disabled, by default it is also reset to its empty value. To have it keep its previous value, set resetOnDisable() to false.

If any named property doesn't exist in the Binder, or the target field's Component doesn't implement HasEnabled, then an exception is thrown.

Example:


 public class GroceryItem {

     // Is this item perishable?
     @FieldBuilder.Checkbox(label = "Perishable food item")
     public boolean isPerishable() { ... }

     // This field is only used for perishable items
     @FieldBuilder.DatePicker
     @FieldBuilder.EnabledBy("perishable")
     @FieldBuilder.FormLayout("Expiration Date:")
     public LocalDate getExpirationDate() { ... }
 }
 
See Also:
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    boolean
    Enable the target field only when all controlling fields have a non-default value (AND condition).
    boolean
    Whether the value of this field should be automatically reset to its empty value when this field is disabled by its controlling field.
    The name of the other properties in the form that should control enablement.
  • Element Details

    • value

      String[] value
      The name of the other properties in the form that should control enablement.
      Returns:
      controlling property name(s)
      Default:
      {}
    • resetOnDisable

      boolean resetOnDisable
      Whether the value of this field should be automatically reset to its empty value when this field is disabled by its controlling field.

      Note: it's not currently possible in Vaadin to listen for changes to a field's enabled status (see issue #14334). Therefore, if you change this to false, and the field X you're configuring is itself the enabling field for some third field Y through another instance of this annotation, then it's possible to get in a state where this field X is disabled (though non-empty), and therefore Y is still enabled.

      To avoid that possibility, leave this set to true or set value() to the transitive closure of such dependencies.

      Returns:
      whether to reset when disabled
      Default:
      true
    • requireAll

      boolean requireAll
      Enable the target field only when all controlling fields have a non-default value (AND condition).

      If set to false, then the target field is enabled when any of the controlling fields have a non-default value (OR condition).

      Returns:
      true for AND conditions, false for OR condition
      Default:
      true