Interface DynamicField<F extends DynamicField<F>>

All Superinterfaces:
Comparable<F>, Component<F>, Copyable<F>, Field<F>, FormComponent<F>, Serializable
All Known Implementing Classes:
DefaultDynamicField

public interface DynamicField<F extends DynamicField<F>> extends Field<F>

Represents the metadata for a FieldTypes.DYNAMIC field. A dynamic field is useful for situations where you have a property that might be managed with different metadata depending on the form state.

A dynamic field can be used to switch between one field type and another, for example:

 Fields.dynamic()
         .configureFields(fields -> {
             // match when "type" property is "TEXT_AREA"
             fields.add(Fields.textArea()
                     .label("Text Area")
                     .conditional(Conditional.when("type").equalTo(FieldTypes.TEXT_AREA)));

             // match when "type" property is "HTML"
             fields.add(Fields.html()
                     .label("HTML")
                     .conditional(Conditional.when("type").equalTo(FieldTypes.HTML)));

             // match when none of the others match, i.e. the default
             fields.add(Fields.string()
                     .label("String"));

             return fields;
         });
 

A dynamic field can be used to switch read-only status for a field, for example:

 Fields.dynamic()
         .configureFields(fields -> {
             // match when "mutable" property is true
             fields.add(Fields.string()
                     .label("Text (immutable)")
                     .readOnly()
                     .conditional(Conditional.when("mutable").equalTo(true)));

             // match when none of the others match, i.e. the default
             fields.add(Fields.string()
                     .label("Text (mutable)"));

             return fields;
         });
 

A dynamic field can be configured to clear the existing value when a new field is matched, for example:

 Fields.dynamic()
         .clearValueOnMatch() // configure the field to clear the value on match
         .configureFields(fields -> {
             // match when "type" is COLOR
             fields.add(Fields.select()
                     .label("Colors")
                     .multi()
                     .options(ColorOptionEnum.toOptions())
                     .conditional(Conditional.when("type").equalTo("COLOR")));

             // match when "type" is SIZE
             fields.add(Fields.select()
                     .label("Sizes")
                     .multi()
                     .options(SizeOptionEnum.toOptions())
                     .conditional(Conditional.when("type").equalTo("SIZE")));

             // match when none of the others match, i.e. the default
             fields.add(Fields.stringArray()
                     .label("Values"));

             return fields;
         });
 
Author:
Nick Crum (ncrum)
  • Method Details

    • name

      default F name(String name)
      Specified by:
      name in interface Field<F extends DynamicField<F>>
    • configureFields

      default F configureFields(UnaryOperator<List<Field<?>>> fn)
      Configure the list of fields this component will match against in deciding which field to render. This provides a List of Field which can be modified to configure the list of components. The list returned will be sorted according to the order of the members. Each member will be mapped over to the dynamic field's components map using DynamicField.Keys.getFieldKey(int) as the key.
      Parameters:
      fn - the function to configure the list
      Returns:
      this
    • clearValueOnMatch

      default F clearValueOnMatch()
      Configure this dynamic field to clear the existing value when a new field is matched. This will result in the property the field targets being cleared out and then defaulted to a new value if the matched field has a default value.
      Returns:
      this
    • notClearValueOnMatch

      default F notClearValueOnMatch()
      Configure this dynamic field to not clear the existing value when a new field is matched. This is the default behavior of dynamic fields, and the result is that the existing property value is preserved even when new fields are matched.
      Returns:
      this