|
Form Processing in Freeform Freeform Framework offers a standard API for processing forms. It includes automatic form field validation and default value setting, detecting the submission of the form, accessing and altering field values. The HTML package that is present in every Freeform installation, has all the tools to display forms, fields and warning messages in a very comfort way. Each form in Freeform application is represented by the Form object. Note that unlike other MVC frameworks, this class does not extend the Action class. This is a major step forward as it allows you to use multiple forms on the same page, as well as to reuse the same form on different pages. Moreover, extending the Form you can create standalone forms that will contain a predefined set of fields and can be reused by other actions. The main characteristic of the form is the location of the action that will be used to process it. Compared to the action attribute of the generic HTML form, this sets the action class that will be executed when the form is submitted. The action will instantiate the same form and call one or a few methods of the form object to detect its submitted/valid state. Based on that the action can decide whether to process the data or to redisplay the page in case of erroneous input or to redirect to some other page. The form consists of one or more input field objects that extend the InputField. Currently there are five of them:
Besides input fields, the form can also contain parameters - non-editable elements that carry additional information about the entity being edited (i.e., in the HTML documents they are included as the hidden fields). You can use this feature to pass along the ID of the record you are editing, for example. Upon submission, the form will detect all incorrectly filled fields and reset their values to null. It will also set its validity state to false if any of the fields failed validation. Most frequently you will use forms that are created and processed by a single action. This very well fits into the MVC paradigm of "one controller (action) for each functionality". In such cases you will create the form by the action, and this action will be processing the form. Below is an example of such action that is used to log in a user:
This example could utilize this sort of template (here we assume we use the HTML package):
As you can see from the template it will display the warning message if the credentials are wrong in first place. Then it will display form, possibly displaying warnings before each field stating that the field was incorrectly filled. The action uses very simple form handling mechanism: it is the sole handler of the form, that's why we didn't use separate form class. It also lets the same page display warnings (you might want to redirect to a help page if anything goes wrong with the user input). In case of successful submission it will redirect to another action (this is the most common behaviour). If this is the first time the form is displayed, the userName input field will contain the text '(please enter user name here)'. On subsequent erroneous input it will be empty and the warning message will be displayed. If the validation was successful, the action will check if the credentials are valid. If so, you will be taken to another page; otherwise you will be prompted with the message 'You have supplied wrong credentials' and have to reenter data. |
|