Load this example in your browser to see the following output:
While the above output does reflect the interface changes, it does not throw any light on the interesting concept of "grouping" Web form elements. To learn more about the latter, you will need to analyze the code behind the scenes. Take a look at the following code snippet:
// Creates a group of text inputs $obj_first_name = &HTML_QuickForm::createElement('text', '', '', array('size' => 30, 'maxlength' => 30)); $obj_last_name = &HTML_QuickForm::createElement('text', '', '', array('size' => 30, 'maxlength' => 30)); $obj_registration_form->addGroup(array($obj_first_name, $obj_last_name), 'txtFullName', 'Full Name:', ' ');
In the first example, I provided a single textbox for the "Full Name" field whereas most Web forms provide two text boxes, one each for the "First Name" and "Last Name" fields. This anomaly needs to be corrected. This is where the ability of the HTML_QuickForm() object to group <FORM> elements and treat them a single entity is useful.
The "grouping" of the "First Name" and "Last Name" fields is one possible example. Another common example is a Web form field that accepts a credit card number. The user can be requested to enter it in groups of four numbers as embossed on the credit card itself.
Coming back to the script, I have invoked the createElement() static method in order to create an instance of the HTML_QuickForm_element() object. The parameters of this static method mimic those of the addElement() method and the nature of element object created depends on the "keyword" value passed to the method.
Once I have defined all the elements of a particular "group," I invoke the addGroup() method of the core HTML_QuickForm() object and pass an array, containing the element instances, along with a "name" and a "label" for the "grouped" control.
The fourth parameter represents the separator used between the elements to be grouped - I have passed a non-breaking space entity character in order to display the two text fields on the same line.