Page 498 - Beginning PHP 5.3
P. 498
Part III: Using PHP in Practice
Finally, two buttons are created: a submit button and a reset button. So that these two buttons appear
side by side, they are placed into an element group, separated by a non - breaking space:
$buttons = array();
$buttons[] = HTML_QuickForm::createElement( “submit”, “submitButton”, “Send
Details” );
$buttons[] = HTML_QuickForm::createElement( “reset”, “resetButton”, “Reset
Form” );
$form- > addGroup( $buttons, null, null, “ & nbsp;” );
Now that the $form object has been created and populated, the script checks if the form has been
submitted. If it has, a thank - you message is displayed; otherwise the form is displayed by calling the
toHtml() method and outputting the result:
if ( $form- > isSubmitted() ) {
echo “ < p > Thanks for your details! < /p > ”;
} else {
echo $form- > toHtml();
}
Using Validation Rules
HTML_QuickForm comes with a number of built - in validation rule types, or you can create your own.
Here ’ s a list of the built - in rule types that you can use with the addRule() method (described in
“ Working with HTML_QuickForm ” earlier in the chapter):
Rule Type Value of $format Argument Description
required N/A The value must not be empty.
maxlength $ max (integer) The value ’ s string length must not exceed
$ max characters.
minlength $ min (integer) The value ’ s string length must be at least
$ min characters.
rangelength array( $ min , $ max ) The value ’ s string length must be between
(integers) $ min and $ max characters.
regex $ regex (string) The value must match the regular
expression $ regex .
email $ domainCheck (Boolean, The value must be a valid email address.
default: false) Set $ domainCheck to true to verify the
email domain with the PHP checkdnsrr()
function.
lettersonly N/A The value must contain only letters.
alphanumeric N/A The value must contain only letters and/or
numbers.
460
9/21/09 9:14:54 AM
c15.indd 460
c15.indd 460 9/21/09 9:14:54 AM