Form Decorator

Trott edited this page Apr 6, 2012 · 13 revisions
Clone this wiki locally

Form Decorator (MWF 1.3)

Basic Usage

Creating a short form.

echo Site_Decorator::form('action' => 'save.php')
    ->set_title('Form Title')

set_padded() sets the form to be padded. set_title() sets the text for the form title. add_submit() adds a primary submit button.

Form elements

Input Types

add_input($input_decorator) adds an input form element.

$input_decorator is, predictably, an Input Site Decorator. A simple text Input Site Decorator can be created like this:

Site_Decorator::input('text-1', 'Name');

The first parameter is the id for the resulting element. The second parameter is the label. Attributes for the resulting HTML element can be specified as an array passed as an optional third parameter.

The default input type is a text field, but you can specify other input types using the type_* methods.

Site_Decorator::input('textarea-20', 'Textarea')

Site_Decorator::input('checkbox-10', 'Checkbox')

Site_Decorator::input('color-10', 'Color')

Site_Decorator::input('search-10', 'Search')

Site_Decorator::input('tel-10', 'Telephone')

Site_Decorator::input('url-10', 'URL')

Site_Decorator::input('email-10', 'Email')

Some useful methods to apply to Input Site Decorators include:

  • required() marks input field as required
  • disable() disables the input field
  • set_placeholder($placeholder_text) puts placeholder text in the input field
  • invalid($error_text) highlights the input and label field as an error field with error text displayed

Checkbox Groups and Radio Groups

Input groups (specifically, radio groups and checkbox groups) are handled like this:

echo Site_Decorator::form()
    ->add_checkbox_group('checkbox-group-1', 'Label for Checkbox Group', array(
        Site_Decorator::input('checkbox-1', 'One')->set_param('value', 1),
        Site_Decorator::input('checkbox-2', 'Two')->set_param('value', 2),
        Site_Decorator::input('checkbox-3', 'Three')->set_param('value', 3)
    ->add_radio_group('radio-group-1', 'Label for Radio Group', array(
        Site_Decorator::input('radio-1', 'One')->set_param('value', 1),
        Site_Decorator::input('radio-2', 'Two')->set_param('value', 2),
        Site_Decorator::input('radio-3', 'Three')->set_param('value', 3)


Select (dropdowns and lists) can be generated like this:

$select_input = Site_Decorator::input('select-group-1', 'Label for Select')
    ->add_option('', 'Select...')
    ->add_option(1, 'One')
    ->add_option(2, 'Two')
    ->add_option(3, 'Three');

$select_input_multiple = Site_Decorator::input('select-group-2', 'Label for Multiple Select')
    ->add_option('hustle', 'The Hustle')
    ->add_option('mashedp', 'Mashed Potato')
    ->add_option('twist', 'The Twist')
    ->add_option('bunnyhop', 'Bunny Hop');

$select_input_sized = Site_Decorator::input('select-group-3', 'Label for Sized Select')
    ->add_option(1, 'Planet of the Apes')
    ->add_option(2, 'Beneath the Planet of the Apes')
    ->add_option(3, 'Escape from the Planet of the Apes')
    ->add_option(4, 'Conquest of the Planet of the Apes')
    ->add_option(5, 'Battle for the Planet of the Apes')
    ->add_option(6, 'Planet of the Apes (Tim Burton Reboot)')
    ->add_option(7, 'Rise of the Planet of the Apes');

echo Site_Decorator::form()
    ->set_title('Select Form')

multiple() can be used to make it possible to enter more than one item. This is often a better choice than a checkbox group. For one thing, using multiple() and required() on a select input is the only way, without resorting to JavaScript, to make an input that allows a user to choose as many items as they want but requiring that they choose at least one.

Number and Range

HTML5 input types number and range can be generated like this:

$number_input = Site_Decorator::input('number-10', 'Number')
    ->set_param('min', 0)
    ->set_param('max', 10)
    ->set_param('step', 2)

$range_input = Site_Decorator::input('range-10', 'Range')
    ->set_param('min', 0)
    ->set_param('max', 100)
    ->set_param('step', 10)

echo Site_Decorator::form()

Date/Time Input

$now = new DateTime('now');
$five_years_ago = new DateTime("5 years ago");
$five_years_from_now = new DateTime("5 years");
$date_input = Site_Decorator::input('date-10', 'Date')
    ->set_param('min', $five_years_ago->format('Y-m-d'))
    ->set_param('max', $five_years_from_now->format('Y-m-d'))
$month_input = Site_Decorator::input('month-10', 'Month')
    ->set_param('min', $five_years_ago->format('Y-m'))
    ->set_param('max', $five_years_from_now->format('Y-m'))
$week_input = Site_Decorator::input('week-10', 'Week')
    ->set_param('min', $five_years_ago->format('Y-\WW'))
    ->set_param('max', $five_years_from_now->format('Y-\WW'))

$datetime_local_input = Site_Decorator::input('datetime-10', 'Datetime Local')
    ->set_param('min', $five_years_ago->format('Y-m-d\TH:i'))
    ->set_param('max', $five_years_from_now->format('Y-m-d\TH:i'))

$time_input = Site_Decorator::input('time-10', 'Time')
    ->set_param('min', $five_years_ago->format('H:i'))
    ->set_param('max', $five_years_from_now->format('H:i'))

echo Site_Decorator::form()

For browsers that do not implement the date, time, and datetime-local input types, you can override the default behavior of using a simple text box by including the polyfill library with add_js_handler_library():

echo Site_Decorator::head()
    ->set_title('Forms With Polyfills')
    ->add_js_handler_library('full_libs', 'formsPolyfills')

This will cause a calendar to appear when date or datetime-local input types are selected. For time and (and the time portion of datetime-local), buttons will allow the time to be incremented or decremented.


echo Site_Decorator::form()
    ->add_input(Site_Decorator::input()->type_button()->primary()->set_param('value', 'Primary Button'))
    ->add_input(Site_Decorator::input()->type_button()->secondary()->set_param('value', 'Secondary Button'))
    ->add_input(Site_Decorator::input()->type_button()->set_param('value', 'Neutral Button'))
    ->add_link_button_primary('Primary Link', '')
    ->add_link_button_secondary('Secondary Link', '')
    ->add_link_button_neutral('Neutral Link', '')
    ->add_input(Site_Decorator::input()->type_submit()->set_param('value', 'I Am The, I mean, Submit Button')


Adds a subtitle to the form.



Adds a paragraph to the form