A validator is used to validate user input according to pre-defined rules.
A validator may have a split implementation to provide both validation at
the client-side (which gives instant feed-back to the user while
editing), and server-side validation (to be sure that the client was not
tampered with). The feed-back given by (client-side and server-side)
validation is reflected in the style class of the form field: a style
Wt-invalid is set for a field that is invalid.
The WValidator only checks that mandatory fields are not empty. Validated input can have one of the following states:
If these validators are not suitable, you can inherit from this class,
and provide a suitable implementation to
validate() which does
the server-side validation. If you want to provide client-side validation
for your own validator, you may also reimplement
In entirely the same way, a date validator can be used to
validate date input with a
WDatePicker. You can limit the date range of the
A more convenient way to perform validation is to implement a form model (a specialisation of WFormModel) and a complementary template-based view (a specialisation of WTemplateFormView). In this way you can validate the values entered at the server-side.
The form model 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.
Each field has a string literal assigned to it. The string literal
uniquely identifies the field. 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, you could also create a
validator to simultaneously validate interdependent fields.
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. See Forms for more details on implementing forms.
Here is an example with the same behavior as the previous example. It
also uses the same XML template. The contents of the variable name
'age-info' in the template depends on the outcome of the method
validate() of the model.