eZ components - UserInput ~~~~~~~~~~~~~~~~~~~~~~~~~ .. contents:: Table of Contents Introduction ============ This component makes secure handling of input data easier. With a user defined form definition the component analyses, filters and returns GET and POST data. The component will not render forms to HTML from its definition, but only handles incoming data. The filtering is done through PHP's filter_ extension and the component supports all filters and flags that this extension supports. Class overview ============== ezcInputForm Is the class that validates the definition, processes the form data and provides functionality for accessing the submitted data. ezcInputFormDefinitionElement The container class that wraps around the definition for each form element. It contains whether the field is optional or required, the filter name and optional flags for the filter. Basic Usage =========== The example in this section is cut in multiple parts to allow easier explanation. .. include:: tutorial_example_01_def.php :literal: In the lines above we prepare a definition array that defines our form. A definition array consists of an associative array where the key is the input field name and the value an object of the ezcInputFormDefinitionElement class. The first parameter to the constructor is either ezcInputFormDefinitionElement::REQUIRED for fields that *have* to be submitted (although they can be empty) or ezcInputFormDefinitionElement::OPTIONAL for optional fields. The second parameter is the filter to use for this input field. The filters are defined in PHP's filter_ extension, and can also be retrieved by the PHP function input_filters_list(). The third optional parameter contains flags to the filter. Those are documented in the `filter documentation`_. In the definition above we define four input fields, which are all required. Two of them are strings (firstName and lastName), one is an integer (age) and the last one an e-mail address (email). .. include:: tutorial_example_01_init.php :literal: Here we just initialize the variables that are used to show the current value and whether invalid data was submitted to the form. This is later used to render the form. .. include:: tutorial_example_01_process.php :literal: In line 2 we check whether there was GET data submitted to this script. Besides the ezcInputForm::hasGetData() method to verify if there is GET data available there is another method, ezcInputForm::hasPostData(), which does the similar thing but then for POST data. Upon instantiation of the ezcInputForm object in line 4 the component will parse the input data and makes the input fields available through the object. In case one of the required input variables did not exist in the input data this instantiation will throw an ezcInputFormFieldNotFoundException exception. In lines 6 to 20 we loop over all elements from the definition and check (line 10) whether the field has valid data. When there is valid data available we retrieve the value from the $form object through a property (line 12) and in case the data for a field was invalid we fetch the raw data with the ezcInputForm::getUnsafeRawData() function, encode that with htmlspecialchars_ and set the parameter with the name "property\_" to the encoded raw data. We also record in the "warning\_" variable if the field has invalid data. .. include:: tutorial_example_01_form.php :literal: The last part of this example renders the form. If previous data was submitted it will be shown as default value in the input fields. If the data for one of the fields was invalid it will show that next to the field as well. More Information ================ The filters and its parameters are documented are documented in the `filter documentation`_. .. _filter: http://pecl.php.net/filter .. _`filter documentation`: http://files.derickrethans.nl/filter_extension.html .. _htmlspecialchars: http://php.net/htmlspecialchars .. Local Variables: mode: rst fill-column: 79 End: vim: et syn=rst tw=79