|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objecteu.webtoolkit.jwt.WObject
eu.webtoolkit.jwt.WFormModel
public class WFormModel
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 Summary | |
|---|---|
WFormModel()
Constructor. |
|
WFormModel(WObject parent)
Constructor. |
|
| Method Summary | |
|---|---|
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, setObjectName, tr |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public WFormModel(WObject parent)
Creates a new form model.
public WFormModel()
Calls this((WObject)null)
| Method Detail |
|---|
public void addField(java.lang.String field,
java.lang.CharSequence info)
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.
public final void addField(java.lang.String field)
Calls addField(field,
WString.Empty)
public void removeField(java.lang.String field)
The field is removed from the model.
public java.util.List<java.lang.String> getFields()
This returns the fields currently configured in the model (added with
addField()
or for which a value or property has been set).
public void reset()
The default implementation clears the value of all fields, and resets the validation state to not validated.
public boolean validate()
The default implementation calls
validateField() for each
field and returns true if all fields validated.
validateField(String field)public boolean isValid()
This checks the getValidation() of all fields, and returns true if all all
fields have been validated and are valid.
validate()
public void setVisible(java.lang.String field,
boolean visible)
Fields are visible by default. An invisible field will be ignored during validation (i.e. will be considered as valid).
isVisible(String field)public boolean isVisible(java.lang.String field)
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().
public void setReadOnly(java.lang.String field,
boolean readOnly)
Fields are read-write by default.
isReadOnly(String field)public boolean isReadOnly(java.lang.String field)
The default implementation returns the value set by
setReadOnly()
public WString label(java.lang.String field)
The default implementation returns the WString::tr(field)
public void setValue(java.lang.String field,
java.lang.Object value)
getValue(String field),
valueText(String field)public java.lang.Object getValue(java.lang.String field)
valueText(String field),
setValue(String field, Object value)public java.lang.String valueText(java.lang.String field)
getValue(String field)
public void setValidator(java.lang.String field,
WValidator validator)
If the validator has no ownership yet, the form model will take ownership.
public WValidator getValidator(java.lang.String field)
Returns the validator for the field.
public boolean validateField(java.lang.String 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.
validate()
public void setValidated(java.lang.String field,
boolean validated)
This is usually not used directly, but invoked by
setValidation()
A field is initially (or after reset()), not
validated.
public boolean isValidated(java.lang.String field)
This is initially false, and set to true by
setValidation().
setValidated(String field, boolean validated)public WValidator.Result getValidation(java.lang.String field)
validateField(String field)
public void setValidation(java.lang.String field,
WValidator.Result result)
This will also set the field as validated.
getValidation(String field),
isValidated(String field)
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||