Class DataForm.Field

  • All Implemented Interfaces:
    Comparable<DataForm.Field>
    Enclosing class:
    DataForm

    public static final class DataForm.Field
    extends Object
    implements Comparable<DataForm.Field>
    A data form field.

    Usage

    Creating a field

    Since a field can have multiple different properties like type, value, label, description, required, options, etc. it uses the builder pattern to construct an (immutable) instance of a field. If the field type is omitted it's inferred from the value, as you see in the following examples.
    
     // <field type="boolean" var="test"><value>1</value></field>
     DataForm.Field field = DataForm.Field.builder()
         .name("test")
         .value(true)
         .build();
    
     // <field type="jid-single" var="test"><value>domain</value></field>
     DataForm.Field.builder()
         .name("test")
         .value(Jid.of("domain"))
         .build();
     

    Creating a field with options

    
     // <field type="list-single" var="test"><option><value>option</value></option></field>
     DataForm.Field field = DataForm.Field.builder()
         .name("test")
         .type(DataForm.Field.Type.LIST_SINGLE)
         .options(Collections.singleton(new DataForm.Option("option")))
         .build();
     

    Retrieving values from a field

    
     // Interprets the field value as integer, e.g. <value>123</value>
     Integer intValue = field.getValueAsInteger();
    
     // Interprets the field value as boolean, e.g. <value>1</value>
     boolean boolValue = field.getValueAsBoolean();
     
    This class is immutable.
    See Also:
    3.2 The Field Element
    • Method Detail

      • builder

        public static DataForm.Field.Builder builder()
        Creates the builder to build a data form field.
        Returns:
        The builder.
      • getType

        public final DataForm.Field.Type getType()
        Gets the field type.
        Returns:
        The field type.
      • getName

        public final String getName()
        Gets a unique identifier for the field in the context of the form.
        Returns:
        The var attribute.
      • getVar

        @Deprecated(forRemoval=true)
        public final String getVar()
        Deprecated, for removal: This API element is subject to removal in a future version.
        Gets a unique identifier for the field in the context of the form.
        Returns:
        The var attribute.
      • getLabel

        public final String getLabel()
        Gets the field label.
        Returns:
        The label.
      • getValue

        public final String getValue()
        Gets the value of the field.
        Returns:
        The value.
      • getValueAsBoolean

        public final boolean getValueAsBoolean()
        Gets the value as boolean.
        Returns:
        The value as boolean.
      • getValueAsInteger

        public final Integer getValueAsInteger()
        Returns the first value as integer.
        Returns:
        The integer or null, if the values are empty.
      • getValueAsInstant

        public final Instant getValueAsInstant()
        Returns the first value as date.
        Returns:
        The date or null, if the values are empty.
      • getValueAsJid

        public final Jid getValueAsJid()
        Returns the first value as JID, e.g. for the DataForm.Field.Type.JID_SINGLE field type.
        Returns:
        The JID or null, if the values are empty.
      • getMedia

        public final Media getMedia()
        Gets the media element.
        Returns:
        The media element.
      • getDescription

        public final String getDescription()
        Gets a natural-language description of the field, intended for presentation in a user-agent (e.g., as a "tool-tip", help button, or explanatory text provided near the field).
        Returns:
        The description.
      • isRequired

        public final boolean isRequired()
        If the field as required in order for the form to be considered valid.
        Returns:
        True, if the field is required.
      • hashCode

        public final int hashCode()
        Overrides:
        hashCode in class Object