Page 495 - Beginning PHP 5.3
P. 495

Chapter 15: Making Your Job Easier with PEAR

                             Method                         Description
                                             ,
                               addRule( $ element             Adds a validation rule to the form element with name   $ element  .
                                    ,
                                           ,
                           $ message  $ type  $ format
                                                    ,
                                                          $ message  is the error message to display next to the form

                           $ validation  $ reset ,        field if the entered data is invalid, and   $ type  is the rule type
                                       ,

                           $ force  )                     to use (for example,    “ required ”   to check that the field
                                                          contains data, or    “ alphanumeric ”   to check that the data in
                                                          the field is alphanumeric).   $ format  is required by some rules

                                                          (for example, the    “ regex ”   type expects a regular expression
                                                          as the   $ format  argument).


                                                          $ validation  can be   “ server ”  or   “ client .   Forms are
                                                                                             ”

                                                          usually validated in the PHP script (that is, server - side). By
                                                          specifying    “ client ”   here,  HTML_QuickForm  also includes
                                                          JavaScript in the form for additional client - side validation.
                                                            $ reset  works in tandem with the   “ client ”  $ validation

                                                          setting; if set to   true , the form element is reset to its original
                                                          value if there was an error. (The default is   false .)
                                                            Finally,   $ force  forces the validation rule to be applied even if

                                                          the element in question doesn ’ t exist in the form. The default
                                                          is   false ; set to  true  to force validation.
                                                          All arguments are optional except   $ element   $ message ,

                                                                                             ,

                                                          and  $ type  (and  $ format  if required by the rule type).


                                applyFilter( $ element        Applies a filter to an element ’ s data. The filter is a callback
                                                 ,
                           $ filter  )                    function.   $ element  is the element to filter, and  $ filter  is


                                                          the callback name. For example, to trim whitespace from a

                                                          form field called    “ username ”,   you could use  $form - >
                                                          applyFilter(  “ username ” ,  “ trim ”  ) . To run all fields
                                                          through a filter, use the special element name    “ __ALL__ ”  .
                                                          You can also write your own filter callback functions.
                               isSubmitted()                Returns  true  if the form has been sent back to the script by the user,
                                                          or   false  if this is the first time the form is being displayed. (Only
                                                          works if you created the form with   $ trackSubmit  set to  true .)

                           validate()                       Runs all validation rules on the submitted form data, returning
                                                            true  if the form is valid and  false  otherwise. In addition, error
                                                          messages are automatically inserted into the form, next to the
                                                          invalid form fields.
                                              ,
                               process( $ callback            Processes the submitted form by passing the form data to the
                           $ mergeFiles  )                function called   $ callback . You need to create this function

                                                          yourself, and it should expect an associative array containing
                                                          the submitted form fields and values. Any uploaded files are
                                                          also passed to the callback function by default (turn this
                                                          feature off by setting   $ mergeFiles  to  false ).

                                toHtml()                      Returns the HTML markup to display the form. Call this
                                                          method once you ’ ve created your form to retrieve the HTML
                                                          for inserting into the Web page.
                                                                                                         457


          c15.indd   457                                                                              9/21/09   9:14:52 AM
          c15.indd   457
                                                                                                      9/21/09   9:14:52 AM
   490   491   492   493   494   495   496   497   498   499   500