Developing HTML Forms in PHP using QuickForm
Setup QuickForm
The first step in using the package would be to include it on our code. We need this package for our script to work, so we will make use of the require_once() function to include it.
require_once('HTML/QuickForm.php');
The following step is to instantiate a QuickForm object. In the constructor, we need to declare the name of the form. Additionally, the constructor can receive a number of optional arguments. More information on the constructor, can be found on the pear web site.
$form = new HTML_QuickForm(‘myform’);
Form Elements
We can add form elements by calling the addElement() function. The order in which we add elements to our form, defines the order in which the elements will be displayed on the browser.
The function receives a number of arguments. The first argument defines the type of the element. The standard HTML form types are: text, button, checkbox, hidden, submit, reset, radio, file, image, password, select and textarea. Furthermore, QuickForm supports a number of other types like date, static, header, html, link, advcheckbox and hiddenselect.
The second argument is the unique name that should be used for the element. The third argument represents the label of the element (the text that will be displayed next to the field on the browser).
Some of the element types can accept additional arguments. For example, an element of type ‘select’ can receive a fourth argument which is an array of values that would populate the items list.
Validation & Filter Rules
QuickForm has a variety of validation capabilities. These ensure that the values submitted for specific form elements meet certain predefined requirements (for example that the field is mandatory).
QuickForm makes it easy to execute these validation rules not only on the server once a form has been submitted but also via automatically-generated JavaScript in the browser before the fields are submitted.
We can specify element rules by calling the addRule() function. This function receives three arguments. The first argument is the name of the fields that the rule should be applied to. The second argument is the text that should be displayed when the rule fails. The last argument is the type of the rule that should be applied on the field. The supported rules are:
- required: Mandatory field. The form cannot be submitted if a value is not entered.
- maxlength: Sets the maximum length of the accepted value. This rule expects an argument for the maximum length.
- minlength: Sets the maximum length of the accepted value. This rule expects an argument for the minimum length.
- rangelength: The value needs to be between a minimum and maximum value (both limits needs to be specified as arguments).
- email: Accepts only email addresses.
- regex: The entered value needs to match a regular expression (specified as an argument).
- lettersonly: The field accepts only letters.
- alphanumeric: The entered value cannot be a number.
- numeric: The value can only be a number.
- nopunctuation: Punctuation is not allowed in the field value. The following characters are considered invalid: ( ) . / * ^ ? # ! @ $ % + = , ” ‘ > < ~ [ ] { }.
- nonzero: 0 is not an accepted value
- callback: Calls a function that would perform the validation.
- compare: Compares two fields to make sure that they hold the same value. Very useful when we need to confirm a password or an email address.
The default option for the validation checks is on the server, once the form is submitted. However, QuickForm allows us to perform the checks on the client. We can achieve that by adding the string ‘client’ as a final argument on the function (if instead we use the string ‘server’ then the validation is performed on the server which is the default option). This will force QuickForm to generate JavaScript functions that will be added automatically on the generated HTML code and perform the validation checks on the client when the form is submitted.















Very concise. Thanks, I’ll be using this info!
[...] Developing HTML Forms in PHP using QuickForm (dountsis.com, en) [...]