Learning about Validation
In this example, we extend our calculator application to include Validation.
As we mentioned earlier, Qforms will go through a validation process just before it executes
any Server-based actions, if needed. If the Control that triggers the ServerAction has its
CausesValidation property set to "true", then before executing the ServerAction, the Form will
go through every visible control in the entire Form and call
Validate(). Only after ensuring
that every control is valid, will the Form go ahead and execute the assigned ServerAction.
Otherwise, every Control that had its
Validate() fail will have its ValidationError property
set with the appropriate error message.
What the validation checks for is dependent on the control you are using. In general,
QControls that have their
Required property set to "true" will check to ensure that data
was at least entered or selected. Some controls have additional rules. For example, we'll use
QIntegerTextBox here to have Qforms ensure that the data entered in our two textboxes are
valid integers.
So we will utilize the Qforms validation in our application by doing the following:
- Set our btnCalculate button's CausesValidation property to true
- Use QIntegerTextBox classes
- For those textboxes, we will use RenderWithError() instead of Render() in the HTML
template code. This is because Render() only renders the control, itself, with no
other markers or placeholders. RenderWithError() will be sure to render any error/warning
messages for that control if needed.
- Lastly, we will add our first "business rule": ensure that the user does not divide by 0.
This rule will be implemented as an if statement in the Form_Validate method.
For more advanced users, note that
CausesValidation can also be set to
QCausesValidation::SiblingsAndChildren
or
QCausesValidation::SiblingsOnly. This functionality is geared for developers who are creating more
complex
QForms with child controls (either dynamically created, via custom composite controls, custom
QPanels, etc.),
and allows for more finely-tuned direction as to specify a specific subset of controls that should be validated, instead
of validating against all controls on the form.
SiblingsAndChildren specifies to validate all sibling controls and their children of the control that is triggering
the action, while
SiblingsOnly specifies to validate the triggering control's siblings, only.