Therefore, you shouldn??™t use filters to modify data that
the users will later depend upon unless you notify users of the modification. For example, if usernames
and passwords are case-sensitive, a filter should not change the case of these items without notifying
the users.
Using Rules
While filters offer an implicit method for tidying up user data before processing
continues, sometimes you want to expressly restrict the user from inserting certain
forms of data, preventing the form from being processed until certain constraints are
met. For instance, when asking the user for his name, you??™ll want to prevent numerical
digits from being passed in. Therefore, while Jason Gilmore and Bob Bryla are
valid names, JasonGilmore1 and B0b Bryla are not. But you can??™t just filter out the digits,
because you can??™t be sure of what the user intended to type. Therefore, the mistake
must be flagged and the user notified of the problem. This is where rules come in.
Rules can be instituted to impose strict restrictions on the contents of a string, and
HTML_QuickForm comes packaged with several of the more commonplace rules ready
for use. Table 13-1 summarizes what??™s at your disposal. If none of these rules meets
your needs, you can instead use a callback (also listed in Table 13-1) to create your
own function.
Table 13-1. Common Predefined Validation Rules
Rule Description Usage
alphanumeric Value can only contain letters
and numbers
callback Value must pass through userdefined
function
Name of function
compare Value is compared with another
field value
eq, neq, gt, gte, lt, lte
email Value must be a valid e-mail address Boolean (whether to
perform domain
verification with
checkdnsrr())
lettersonly Value must contain only letters
maxlength Value cannot exceed N characters Integer value
360 CHAPTER 13 ?– FORMS
As an example, to define a rule requiring the user to enter a ZIP code, you would
use this:
$form->addRule('zipcode', 'Please enter a zipcode', 'required', null, 'client');
All of the input parameters should be self-explanatory, except the concluding null
and client designations.
Pages:
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436