concrete5's form helper makes it easy to add form widgets to your single pages and custom blocks, and several advantages to writing full HTML tags.
- Data persistence on POST: when submitting the core form helpers, their data will remain as originally entered, without having to worry about checking $_REQUEST variables. This is even the case for more complex form elements like radio buttons and checkbox lists.
- Attractive styling. Form elements in the block add and edit dialogs will better adhere to concrete5 design guidelines when they are output using the core form helpers.
- Do more with less. Once you learn the syntax, writing forms using these helpers can save you a lot of time and code.
Loading the Helper
You can load the concrete5 form helper using the following simple code
$form = Loader::helper('form');
The $form variable now contains an instance of the FormHelper class.
Arguments
While each form helper function ultimately displays a different HTML tag, most take common arguments. Here is their explanation:
- $name
Name of the field. Oftentimes will set the ID of the field to the same value. - $value
Initial field value. Typically optional. - $tagAttributes
An associative array of additional attributes to be added to the input element. (e.g. array('style' => 'width: 100%')). - $additionalClasses
A string of additional classes to be added to the class attribute on the tag.
Optional Arguments
Note: In most cases, only the $name of the field is a required function argument. Other items are optional. Additionally, if the $value is left blank, the $tagAttributes field can usually be specified as the second argument to the helper.
Methods
$form->text($name, $value, $tagAttributes)
Creates a text input element.
print $form->text('firstName', "Andrew", array('style' => 'width: 100%', 'tabindex' => 2));
$form->submit($name, $value, $tagAttributes, $additionalClasses)
Outputs a submit button.
$form->label($name, $value)
Creates a label for a form element. The $name parameter must match the $name of another form element.
$form->hidden($name, $value)
Creates a hidden form element.
$form->password($name, $value, $tagAttribute)
Outputs a password field.
$form->checkbox($name, $value, $isChecked, $tagAttributes)
Outputs a checkbox. The $isChecked boolean controls whether the checkbox is initially checked or not.
$form->textarea($name, $value, $tagAttributes)
Outputs a textarea form element.
$form->radio($name, $buttonValue, $value, $tagAttributes)
Outputs a radio button. The $buttonValue argument controls the value of this particular radio button. The second $value argument contains the initial value we wish to check, in a series of radio buttons.
$form->select($name, $options, $value, $tagAttributes)
Outputs a select menu. The $options argument is a PHP array. The keys of the array will be used as the select option values, while the array values will be displayed in the menu. The $value parameter controls the initially checked select element.
print $form->select('favoriteFruit', array('p' => 'Pears', 'a' => 'Apples', 'o' => 'Oranges'), 'a');
In this example, the select menu will be output, with the second option selected.