Class Choice

Direct Known Subclasses:
Checkbox, Radio

public abstract class Choice extends Field
Abstracts an HTML choice type such as choicebox or radio. Does not abstract a HTML select element for which the Select class should be used. Concrete subclasses represent a particular choice element such as Checkbox and Radio
  • Method Details

    • getType

      public abstract Field.Type getType()
      Description copied from class: Field
      Subclasses should return an appropriate Field.Type. This type is rendered as part of <input type= ...
      Specified by:
      getType in class Field
    • getValue

      public String getValue(FormData fd)
      Returns the current value of this field. This can be:
    • null: if no value is currently set which can happen if this field was not selected when the parent form was submitted. Browsers send nothing at all if choice type fields are not selected in an HTML form.
    • the value attribute of this field (if this choice field was created/displayed with a value attribute) or the string "on" (which is sent by browsers if there is no specific value attribute for this choice field). Note, the default value should be treated as case insensitive, since browsers can send on, ON etc.
    • setValue

      public void setValue(FormData fd, String value)
      Sets the selected or non-selected state for this choice in the specified form data. Selected choices are returned by the browser as on, ON, oN etc., the value returned is not important as long as something is returned. Therefore any non-null value set by this method will have the effect of selecting this choice when it is rendered. A null value wil unselect this choice.
      Parameters:
      fd - a non-null form data object
      value - any non-null value
    • setValue

      public void setValue(FormData fd, boolean selected)
      Convenience method that sets this choice to be selected/non-selected.
      Parameters:
      fd - a non-null form data object
      selected - true to select this choice, false otherwise
    • setValue

      public void setValue(String value, boolean selected)
      Sets the value for this choice.
    • getIntValue

      public int getIntValue(FormData fd)
      Convenience method that returns the value of this field as a Integer.
      Throws:
      NumberFormatException - if the value could not be returned as in integer.
    • getShortValue

      public short getShortValue(FormData fd)
      Convenience method that returns the value of this field as a Short.
      Throws:
      NumberFormatException - if the value could not be returned as a short.
    • getBooleanValue

      public boolean getBooleanValue(FormData fd)
      Convenience method that returns the value of this field as a boolean. The returned value will be true if the submitted value is "true" or "on" (both case insensitive), else false
    • setValueFromSubmit

      public void setValueFromSubmit(FormData fd, jakarta.servlet.http.HttpServletRequest req) throws SubmitHackedException
      Description copied from class: Field
      This method sets the value of this field from the parameters obtained from the specified request. The name of the parameter to obtain this value will typically be the name of this field itself.
      Specified by:
      setValueFromSubmit in class Field
      Parameters:
      fd - the form data object to store the value in
      Throws:
      SubmitHackedException
    • renderImpl

      public void renderImpl(FormData fd, Writer writer) throws IOException
      Specified by:
      renderImpl in class Field
      Throws:
      IOException
    • isFilled

      public boolean isFilled(FormData fd)
      Description copied from class: Field
      Returns true if this field was isFilled out or selected by the user, false otherwise.

      Note: Some fields like selects will never be empty since non-multiple select fields always send their default selected value. [although select/with/multiple can be empty since the browser sends (much like radio buttoms) nothing at all when no option is selected].

      Specified by:
      isFilled in class Field
      Returns:
      true if this field is selected, false otherwise
    • setSelected

      public void setSelected(boolean select)
      Sets the initial selection status for this field.
      Parameters:
      select - true if this field should be selected false otherwise.
    • toString

      public String toString()
      Overrides:
      toString in class Field