Page 506 - Beginning PHP 5.3
P. 506

Part III: Using PHP in Practice
                field names and labels, and the script specifies text (” Male” and “ Female“) to appear after each
                button, as well as values for the fields (“m” and “f“). The two buttons are then added to an array:

                      $genderOptions = array();
                      $genderOptions[] = HTML_QuickForm::createElement( “radio”, null, null, “
                    Male”, “m” );
                      $genderOptions[] = HTML_QuickForm::createElement( “radio”, null, null, “
                    Female”, “f” );

                 This array of buttons is then used to create an element group with a name of “gender” and a label of
                 “Your gender”. HTML_QuickForm then sets each radio button’s field name to the group name of
                 “gender“:
                      $form->addGroup( $genderOptions, “gender”, “Your gender”, “ “ );

                 To create the favoriteGenre select field, the script calls the Member::getGenres() method to get
                the associative array of genre names and values to pass to addElement(). The script needs to create
                a temporary $member object in order to call Member::getGenres(), because getGenres() isn’t a static
                 method:

                      $member = new Member( array() );
                      $form->addElement( “select”, “favoriteGenre”, “What’s your favorite
                    genre?”, $member->getGenres() );

                 The addRules() function uses the $form object’s addRule() method to add validation rules to most of
                the fields in the form. All required fields are checked against the required rule, and the alphanumeric
                 rule is used on the username and both password fields to make sure they contain only letters and/or
                 digits. In addition, the compare rule is used to check that both password fields contain the same value:

                      $form->addRule( array( “password1”, “password2” ), “Please make sure you
                    enter your password correctly in both password fields.”, “compare” );

                 The emailAddress field is, of course, checked using the email rule. The remaining fields —
                 firstName, lastName, gender, favoriteGenre, and otherInterests — are checked against various
                regular expressions using the regex rule.

                 Of interest is the code for the favoriteGenre field. This creates a temporary $member object and calls
                 its getGenres() method to retrieve the associative array of allowed genres. It then extracts the genre
                 values with PHP’s array_keys() function (which returns an indexed array containing all the keys in a
                given array), and uses the implode() function to turn the resulting array into a string of |-separated
                 alternative values for plugging into the regular expression:

                      $member = new Member( array() );
                      $form->addRule( “favoriteGenre”, “The Favorite Genre field can contain only
                    allowed genre values”, “regex”, “/^(“ . implode( “|”, array_keys( $member-
                    >getGenres() ) ) . “)$/” );









              468





                                                                                                      9/21/09   9:14:57 AM
          c15.indd   468                                                                              9/21/09   9:14:57 AM
          c15.indd   468
   501   502   503   504   505   506   507   508   509   510   511