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.

  1. 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.
  2. 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. 
  3. 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.


Recent Discussions on this Topic

Making a textarea, TinyMCE

I have a number of textarea's that I would like to convert to WYSIWYG editors. Can anyone tell me how to do this the C5 way? and anything I may need to strip when saving back to the db?, I have taken a quick look in the content block but I think tha…

paypal buttons

when I paste my buy now button in my web page it becomes faded and oversized what am I doing wrong? stick5

validations

I am using form helper class to make a form for block admin interface. I want to implement javascript(client side validations) for the fields.I looked at the class but i didn't find any function for validation. So can someone point me where should I…

Bizarre behavior in checkbox form helper

OK, I've learned a lot in the last 24 hours, and things are generally going quite well. However, I've inserted a call to the form helper checkbox utility, and I'm seeing the exact reverse of what I expected. This is in an external form block. I'm loopi…

form->hidden()???

The other Form methods make sense because they are named for the data type that they are associated with. However, although "hidden" is an input type, it is not a data type. When you create a hidden input field, what type of data format is this field used…