Class WFormModel

Direct Known Subclasses:

public class WFormModel extends WObject
A basic model class for forms.

This implements field data and validation handling for (simple) form-based views. It provides a standard way for views to perform field validation, and react to validation results.

All fields are uniquely identified using a string literal (which is the Field type). For each field, its value, the visibility, whether the field is read-only, and its current validation status is managed by the model. In addition, you will typically specialize the class to customize the validation and application logic.

Although it can be setup to use WValidator objects for individual fields, also other validation where more entered information needs to be considered simultaneously can be implemented.

A model is typically used by a View which renders the fields configured in the model, updates the model values, invokes and reflects the validation status.

Example (a bit contrived since you will usually not use the model directly):

 String NameField = "name";
 String TelField = "telephone";

 WFormModel model = new WFormModel();
 model.addField(NameField, "Enter your name");
 model.addField(TelField, "Phone number");

 model.setValue(NameField, "John Doe");

 if (model.validate()) {
 } else {
 WValidator.Result rname = model.getValidation(NameField);
 if (rname.getState() != WValidator.State.Valid) {
 System.err.println("Invalid name: " + rname.getMessage());

  • Constructor Details

    • WFormModel

      public WFormModel()

      Creates a new form model.

  • Method Details

    • addField

      public void addField(String field, CharSequence info)
      Adds a field.

      The field is added to the model, with an optional short informational message that can be used by views to provide a hint on the value that needs to be entered. The message is set as the validation message as long as the field has not yet been validated.

      If the field was already in the model, its data is reset.

    • addField

      public final void addField(String field)
      Adds a field.

      Calls addField(field, WString.Empty)

    • removeField

      public void removeField(String field)
      Removes a field.

      The field is removed from the model.

    • getFields

      public List<String> getFields()
      Returns the fields.

      This returns the fields currently configured in the model (added with addField() or for which a value or property has been set).

    • reset

      public void reset()
      Resets the model.

      The default implementation clears the value of all fields, and resets the validation state to not validated.

    • validate

      public boolean validate()
      Validates the current input.

      The default implementation calls validateField() for each field and returns true if all fields validated.

      See Also:
    • isValid

      public boolean isValid()
      Returns the current overall validation state.

      This checks the getValidation() of all fields, and returns true if all all fields have been validated and are valid.

      See Also:
    • setVisible

      public void setVisible(String field, boolean visible)
      Sets whether a field is visible.

      Fields are visible by default. An invisible field will be ignored during validation (i.e. will be considered as valid).

      See Also:
    • isVisible

      public boolean isVisible(String field)
      Returns whether a field is visible.

      In some cases not all fields of the model need to be shown. This may depend on values input for certain fields, and thus change dynamically. You may specialize this method to indicate that a certain field should be invisible.

      The default implementation returns the value set by setVisible().

    • setReadOnly

      public void setReadOnly(String field, boolean readOnly)
      Sets whether a field is read-only.

      Fields are read-write by default.

      See Also:
    • isReadOnly

      public boolean isReadOnly(String field)
      Returns whether a field is read only.

      The default implementation returns the value set by setReadOnly()

    • label

      public WString label(String field)
      Returns a field label.

      The default implementation returns the WString::tr(field)

    • setValue

      public void setValue(String field, Object value)
      Sets the field value.

      See Also:
    • getValue

      public Object getValue(String field)
      Returns the field value.

      See Also:
    • valueText

      public String valueText(String field)
      Returns the field value text.

      See Also:
    • setValidator

      public void setValidator(String field, WValidator validator)
      Sets a validator.
    • getValidator

      public WValidator getValidator(String field)
      Returns a validator.

      Returns the validator for the field.

    • validateField

      public boolean validateField(String field)
      Validates a field.

      The default implementation uses the validator configured for the field to validate the field contents, or if no validator has been configured assumes that the field is valid.

      You will typically customize this method for more complex validation cases.

      See Also:
    • setValidated

      public void setValidated(String field, boolean validated)
      Sets whether a field has been validated.

      This is usually not used directly, but invoked by setValidation()

      A field is initially (or after reset()), not validated.

    • isValidated

      public boolean isValidated(String field)
      Returns whether the field has been validated yet.

      This is initially false, and set to true by setValidation().

      See Also:
    • getValidation

      public WValidator.Result getValidation(String field)
      Returns the result of a validation.

      See Also:
    • setValidation

      public void setValidation(String field, WValidator.Result result)
      Sets the validation result for a field.

      This will also set the field as validated.

      See Also: