Page 524 - Beginning PHP 5.3
P. 524

Part III: Using PHP in Practice
                 Next, the three select fields for the month, day, and year are added to the form. Such fields are quite
                 tedious to generate, even when using a scripting language like PHP. Fortunately, HTML_QuickForm
                includes a date element type that automatically generates the three select fields for you.

                 First the script creates an array of options for the date fields. format specifies both the format of each of
                the month, day, and year fields, as well as their order in the form. In this case, “MdY” specifies the month
                 as a three-letter abbreviation, followed by the day with a leading zero, followed by the four-digit year.
                 The range of years shown in the third select menu is set with minYear and maxYear; in this case the
                script sets the lowest year to 1902 and the highest year to the current year:

                    $options = array( format => “MdY”, “minYear” => 1902, “maxYear” => date(“Y”) );

                    You can find a full list of formatting characters for the format option in the HTML_QuickForm
                    documentation at http://pear.php.net/package/HTML_QuickForm/docs/latest/HTML_
                    QuickForm/HTML_QuickForm_date.html#methodHTML_QuickForm_date.
                    Due to the limits of 32-bit operating systems, timestamp handling on most current machines is usually
                    limited to dates and times between Friday, Dec 13 1901 20:45:54 GMT and Tuesday, Jan 19 2038
                    03:14:07 GMT.

                 Next it’s simply a case of adding the dateOfBirth element, as well as an element for the Calculate
                button:

                    $form->addElement( “date”, “dateOfBirth”, “Your date of birth”, $options );
                    $form->addElement( “submit”, “calculateButton”, “Calculate” );

                 The dateOfBirth field is validated with two rules. The first is a simple rule to check that the month,
                 day, and year fields have been filled in. Rather than using the usual addRule() method, the script uses
                 addGroupRule(), which checks that all three fields within the dateOfBirth element contain data. The
                 second rule checks that the date specified by the three fields is in fact a valid date. It does this by using a
                 callback function (described in a moment):

                    $form->addGroupRule( “dateOfBirth”, “Please enter your date of birth”,
                    “required” );
                    $form->addRule( “dateOfBirth”, “Please enter a valid date”, “callback”,
                    “checkDateOfBirth” );
                 The next section of code checks to see whether the form has already been submitted. If so, and the
                 supplied form data is valid, the form is processed by calling the calculateDays() function:

                    if ( $form->isSubmitted() and $form->validate() ) {
                      $form->process( “calculateDays” );
                    }

                 Regardless of whether the form was processed, it is now output to the browser using the HTML_
                 QuickForm_Renderer_Tableless renderer:

                    $renderer = new HTML_QuickForm_Renderer_Tableless();
                    $form->accept( $renderer );
                    echo $renderer->toHtml();




              486





                                                                                                      9/21/09   9:15:34 AM
          c16.indd   486
          c16.indd   486                                                                              9/21/09   9:15:34 AM
   519   520   521   522   523   524   525   526   527   528   529