Table of Contents

WACT Form Validation

WACT Form Validation in WACT is accomplished by adding validation rules to a form controller. Generating output based on invalid form input is accomplished by using the ErrorSummary tag and a view that registers validation errors with a given form, typically a FormView.

Generally, client side form validation performs the validation rules in the executing client before the data is sent to the server. Conversely, server side form validation checks the data immediately as it is received by the server, and before any additonal processing is allowed to occur.

FormComponent provides a powerful API for dealing with errors, allowing errors to be displayed alongside form fields where the error occurred. See the WACT Examples Listing for more examples.

Example

This example demonstrates a very simple use case of form validation. If validation doesn’t succeed, errors in red text will appear above the form controls.

signup.php

Dispatching controller, rule registration, and FormView.

$f = new FormController;
$f->addChild ('signup', new ButtonController (new StaticDelegate ('MailSignup', 'doSignup')));
 
/** these register rules to be checked upon form submission */
$f->addRule (new RequiredRule ('emailaddress'));
$f->addRule (new EmailRule    ('emailaddress'));
$f->addRule (new RequiredRule ('mailtype'));
$f->addRule (new MemberRule   ('mailtype', array_flip (array ('text', 'html')))); 
 
$f->setDefaultView (new FormView ('/signupform.tpl.html'));

signupform.tpl.html

Template with form ErrorSummary tag.

...
<form method="POST" runat="server">
 
<errorsummary id="Summary">
<ul>
<list:item><li style="color: red;">{$ErrorMessage}</li></list:item>
</ul>
</errorsummary>
 
<label for="emailaddress">Email address</label>:
<input type="text" id="emailaddress" name="emailaddress"><br>
 
<label for="text">Plain text</label>:
<input id="text" name="mailtype" type="radio" value="text" checked><br>
 
<label for="html">HTML/formatted</label>:
<input id="html" name="mailtype" type="radio" value="html"><br>
 
<input type="submit" name="signup" value="Sign up">
</form>
...

Validation Rules

Please see the WACT Validation Rules catalog for a list of validation rules.

Additional Information