Class WFormModel
- Direct Known Subclasses:
FormBaseModel
public class WFormModel extends WObject
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());
}
...
}
-
Nested Class Summary
Nested classes/interfaces inherited from class eu.webtoolkit.jwt.WObject
WObject.FormData
-
Constructor Summary
Constructors Constructor Description WFormModel()
Constructor.WFormModel(WObject parent)
Constructor. -
Method Summary
Modifier and Type Method Description void
addField(java.lang.String field)
Adds a field.void
addField(java.lang.String field, java.lang.CharSequence info)
Adds a field.java.util.List<java.lang.String>
getFields()
Returns the fields.WValidator.Result
getValidation(java.lang.String field)
Returns the result of a validation.WValidator
getValidator(java.lang.String field)
Returns a validator.java.lang.Object
getValue(java.lang.String field)
Returns the field value.boolean
isReadOnly(java.lang.String field)
Returns whether a field is read only.boolean
isValid()
Returns the current overall validation state.boolean
isValidated(java.lang.String field)
Returns whether the field has been validated yet.boolean
isVisible(java.lang.String field)
Returns whether a field is visible.WString
label(java.lang.String field)
Returns a field label.void
removeField(java.lang.String field)
Removes a field.void
reset()
Resets the model.void
setReadOnly(java.lang.String field, boolean readOnly)
Sets whether a field is read-only.void
setValidated(java.lang.String field, boolean validated)
Sets whether a field has been validated.void
setValidation(java.lang.String field, WValidator.Result result)
Sets the validation result for a field.void
setValidator(java.lang.String field, WValidator validator)
Sets a validator.void
setValue(java.lang.String field, java.lang.Object value)
Sets the field value.void
setVisible(java.lang.String field, boolean visible)
Sets whether a field is visible.boolean
validate()
Validates the current input.boolean
validateField(java.lang.String field)
Validates a field.java.lang.String
valueText(java.lang.String field)
Returns the field value text.Methods inherited from class eu.webtoolkit.jwt.WObject
addChild, getId, getObjectName, remove, setFormData, setObjectName, tr
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Constructor Details
-
WFormModel
Constructor.Creates a new form model.
-
WFormModel
public WFormModel()Constructor.Calls
this((WObject)null)
-
-
Method Details
-
addField
public void addField(java.lang.String field, java.lang.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(java.lang.String field)Adds a field. -
removeField
public void removeField(java.lang.String field)Removes a field.The
field
is removed from the model. -
getFields
public java.util.List<java.lang.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 returnstrue
if all fields validated.- See Also:
validateField(String field)
-
isValid
public boolean isValid()Returns the current overall validation state.This checks the
getValidation()
of all fields, and returnstrue
if all all fields have been validated and are valid.- See Also:
validate()
-
setVisible
public void setVisible(java.lang.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(String field)
-
isVisible
public boolean isVisible(java.lang.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(java.lang.String field, boolean readOnly)Sets whether a field is read-only.Fields are read-write by default.
- See Also:
isReadOnly(String field)
-
isReadOnly
public boolean isReadOnly(java.lang.String field)Returns whether a field is read only.The default implementation returns the value set by
setReadOnly()
-
label
Returns a field label.The default implementation returns the WString::tr(field)
-
setValue
public void setValue(java.lang.String field, java.lang.Object value)Sets the field value.- See Also:
getValue(String field)
,valueText(String field)
-
getValue
public java.lang.Object getValue(java.lang.String field)Returns the field value. -
valueText
public java.lang.String valueText(java.lang.String field)Returns the field value text.- See Also:
getValue(String field)
-
setValidator
Sets a validator.If the validator has no ownership yet, the form model will take ownership.
-
getValidator
Returns a validator.Returns the validator for the field.
-
validateField
public boolean validateField(java.lang.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:
validate()
-
setValidated
public void setValidated(java.lang.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(java.lang.String field)Returns whether the field has been validated yet.This is initially
false
, and set totrue
bysetValidation()
. -
getValidation
Returns the result of a validation.- See Also:
validateField(String field)
-
setValidation
Sets the validation result for a field.This will also set the field as validated.
-