Page 503 - Beginning PHP 5.3
P. 503
Chapter 15: Making Your Job Easier with PEAR
digits”, “alphanumeric” );
$form->addRule( “password2”, “Please retype your password”, “required” );
$form->addRule( “password2”, “The password can contain only letters and
digits”, “alphanumeric” );
$form->addRule( array( “password1”, “password2” ), “Please make sure you
enter your password correctly in both password fields.”, “compare” );
$form->addRule( “emailAddress”, “Please enter an email address”,
“required” );
$form->addRule( “emailAddress”, “Please enter a valid email address”,
“email” );
$form->addRule( “firstName”, “Please enter your first name”, “required” );
$form->addRule( “firstName”, “The First Name field can contain only
letters, digits, spaces, apostrophes, and hyphens”, “regex”,
“/^[ \’\-a-zA-Z0-9]+$/” );
$form->addRule( “lastName”, “Please enter your last name”, “required” );
$form->addRule( “lastName”, “The Last Name field can contain only letters,
digits, spaces, apostrophes, and hyphens”, “regex”,
“/^[ \’\-a-zA-Z0-9]+$/” );
$form->addRule( “gender”, “Please select your gender”, “required” );
$form->addRule( “gender”, “The Gender field can contain only ‘m’ or ‘f’”,
“regex”, “/^[mf]$/” );
$member = new Member( array() );
$form->addRule( “favoriteGenre”, “The Favorite Genre field can contain
only allowed genre values”, “regex”, “/^(“ . implode( “|”, array_keys
( $member->getGenres() ) ) . “)$/” );
$form->addRule( “otherInterests”, “The Other Interests field can contain
only letters, digits, spaces, apostrophes, commas, periods, and hyphens”,
“regex”, “/^[ \’\,\.\-a-zA-Z0-9]+$/” );
$form->addRule( “username”, “A member with that username already exists
in the database. Please choose another username.”, “callback”,
“checkDuplicateUsername” );
$form->addRule( “emailAddress”, “A member with that email address already
exists in the database. Please choose another email address, or contact the
webmaster to retrieve your password.”, “callback”,
“checkDuplicateEmailAddress” );
}
function checkDuplicateUsername( $value ) {
return !(boolean) Member::getByUsername( $value );
}
function checkDuplicateEmailAddress( $value ) {
return !(boolean) Member::getByEmailAddress( $value );
}
function processForm( $values ) {
$values[“password”] = $values[“password1”];
$values[“joinDate”] = date( “Y-m-d” );
$member = new Member( $values );
465
9/21/09 9:14:56 AM
c15.indd 465
c15.indd 465 9/21/09 9:14:56 AM