3.10.3. Form Helpers

All helpers automatically apply appropriate escaping. This means you can use {{= ... }} to output them. If you use {{h ... }} et al., you will end up double-escaping the output.

You can also address the helpers as methods on $this in PHP template code.

3.10.3.1. Form Tag

Open a form like so:

{{= form ([                         // (array) attributes
    'method' => 'post',
    'action' => '/hello',
]) }}
<form method="post" action="/hello" enctype="multipart/form-data">

You can close a form just using </form>.

3.10.3.2. Input Tags

3.10.3.2.1. checkboxField

You can use a checkboxField as a generic input field helper, but you will have to set the checked attribute yourself to mark it as checked or not.

Alternatively, if you specify the pseudo-attribute _options, greater functionality becomes available:

  • The _options specify one or more checkboxes as part of the field, with their value when checked, and their corresponding label.

  • If the _options have more than one element, then field name will be appended automatically with [] to make it an array.

  • The value attribute will be matched against the _options and the correct checkboxes will be checked for you.

  • The _default pseudo-attribute, when present, will produce a hidden input field for the value when no checkboxes are checked.

{{= checkboxField ([                // (array) attributes
    'name' => 'flags',
    'value' => 'bar',
    '_default' => '',
    '_options' => [
        'foo' => 'Foo Flag',
        'bar' => 'Bar Flag',
        'baz' => 'Baz Flag',
    ]
]) }}
<input type="hidden" name="flags" value="" />
<label><input type="checkbox" name="flags[]" value="foo" /> Foo Flag</label>
<label><input type="checkbox" name="flags[]" value="bar" checked /> Bar Flag</label>
<label><input type="checkbox" name="flags[]" value="baz" /> Baz Flag</label>

3.10.3.2.2. colorField

{{= colorField ([                   // (array) attributes
    'name' => 'foo',
    'value' => 'bar',
]) }}
<input type="color" name="foo" value="bar" />

3.10.3.2.3. dateField

{{= dateField ([                    // (array) attributes
    'name' => 'foo',
    'value' => 'bar',
]) }}
<input type="date" name="foo" value="bar" />

3.10.3.2.4. datetimeField

{{= datetimeField ([                // (array) attributes
    'name' => 'foo',
    'value' => 'bar',
]) }}
<input type="datetime" name="foo" value="bar" />

3.10.3.2.5. datetimeLocalField

{{= datetimeLocalField ([           // (array) attributes
    'name' => 'foo',
    'value' => 'bar',
]) }}
<input type="datetime-local" name="foo" value="bar" />

3.10.3.2.6. emailField

{{= emailField ([                   // (array) attributes
    'name' => 'foo',
    'value' => 'bar',
]) }}
<input type="email" name="foo" value="bar" />

3.10.3.2.7. fileField

{{= fileField ([                    // (array) attributes
    'name' => 'foo',
    'value' => 'bar',
]) }}
<input type="file" name="foo" value="bar" />

3.10.3.2.8. hiddenField

{{= hiddenField ([                  // (array) attributes
    'name' => 'foo',
    'value' => 'bar',
]) }}
<input type="hidden" name="foo" value="bar" />

3.10.3.2.9. inputField

A generic input field; specify the type needed.

{{= inputField ([                  // (array) attributes
    'type' => 'text',
    'name' => 'foo',
    'value' => 'bar',
    // ...
]) }}
<input type="text" name="foo" value="bar" />

3.10.3.2.10. monthField

{{= monthField ([                   // (array) attributes
    'name' => 'foo',
    'value' => 'bar',
]) }}
<input type="month" name="foo" value="bar" />

3.10.3.2.11. numberField

{{= numberField ([                  // (array) attributes
    'type' => 'number',
    'name' => 'foo',
    'value' => 'bar',
]) }}
<input type="number" name="foo" value="bar" />

3.10.3.2.12. passwordField

{{= passwordField ([                // (array) attributes
    'name' => 'foo',
    'value' => 'bar',
]) }}
<input type="password" name="foo" value="bar" />

3.10.3.2.13. radioField

You can use a radioField as a generic input field helper, but you will have to set the checked attribute yourself to mark it as checked or not.

Alternatively, if you specify the pseudo-attribute _options, greater functionality becomes available:

  • The _options specify one or more radio buttons as part of the field, with their value when checked, and their corresponding label.

  • The value attribute will be matched against the _options and the correct checkboxes will be checked for you.

{{= radioField ([                   // (array) attributes
    'name' => 'foo',
    'value' => 'baz',
    '_options' => [
        'bar' => 'Bar Label',
        'baz' => 'Baz Label,
        'dib' => 'Dib Label',
    ),
]) }}
<label><input type="radio" name="foo" value="bar" /> Bar Label</label>
<label><input type="radio" name="foo" value="baz" checked /> Baz Label</label>
<label><input type="radio" name="foo" value="dib" /> Dib Label</label>

3.10.3.2.14. rangeField

{{= rangeField ([                   // (array) attributes
    'name' => 'foo',
    'value' => 'bar',
]) }}
<input type="range" name="foo" value="bar" />

3.10.3.2.15. searchField

{{= searchField ([                  // (array) attributes
    'name' => 'foo',
    'value' => 'bar',
]) }}
<input type="search" name="foo" value="bar" />

3.10.3.2.16. select

Use the pseudo-attribute _options to describe the <option> tags.

The attribute placeholder is honored as a placeholder label when no option is selected. The pseudo-attribute _default specifies the value of the placeholder.

Using the attribute 'multiple' => true will set up a multiple select, and automatically add [] to the name if it is not already there.

{{= select ([                       // (array) attributes
    'name' => 'foo',
    'value' => 'dib',
    'placeholder' => 'Please pick one',
    '_default' => '',
    '_options' => [
        'bar' => 'Bar Label',
        'baz' => 'Baz Label',
        'dib' => 'Dib Label',
        'zim' => 'Zim Label',
    ],
]) }}
<select name="foo">
    <option value="" disabled>Please pick one</option>
    <option value="bar">Bar Label</option>
    <option value="baz">Baz Label</option>
    <option value="dib" selected>Dib Label</option>
    <option value="zim">Zim Label</option>
</select>

The helper also supports option groups. If an _options array value is itself an array, the key for that element will be used as an <optgroup> label and the array of values will be options under that group.

{{= select ([
    'name' => 'foo',
    'value' => 'bar',
    '_options' => [
        'Group A' => [
            'bar' => 'Bar Label',
            'baz' => 'Baz Label',
        ],
        'Group B' => [
            'dib' => 'Dib Label',
            'zim' => 'Zim Label',
        ],
    ],
]) }}
<select name="foo">
    <optgroup label="Group A">
        <option value="bar">Bar Label</option>
        <option value="baz">Baz Label</option>
    </optgroup>
    <optgroup label="Group B">
        <option value="dib" selected>Dib Label</option>
        <option value="zim">Zim Label</option>
    </optgroup>
</select>

3.10.3.2.17. telField

{{= telField([                      // (array) attributes
    'name' => 'foo',
    'value' => 'bar',
]) }}
<input type="tel" name="foo" value="bar" />

3.10.3.2.18. textField

{{= textField ([                    // (array) attributes
    'name' => 'foo',
    'value' => 'bar',
]) }}
<input type="text" name="foo" value="bar" />

3.10.3.2.19. textarea

{{= textarea ([                     // (array) attributes
    'name' => 'foo',
    'value' => 'bar',
]) }}
<textarea name="foo">bar</textarea>

3.10.3.2.20. timeField

{{= timeField ([                    // (array) attributes
    'name' => 'foo',
    'value' => 'bar',
]) }}
<input type="time" name="foo" value="bar" />

3.10.3.2.21. urlField

{{= urlField ([                      // (array) attributes
    'name' => 'foo',
    'value' => 'bar',
]) }}
<input type="url" name="foo" value="bar" />

3.10.3.2.22. weekField

{{= weekField ([                    // (array) attributes
    'name' => 'foo',
    'value' => 'bar',
]) }}
<input type="week" name="foo" value="bar" />

3.10.3.3. Button Tags

Helpers for various button tags.

3.10.3.3.1. button

{{= button ([                       // (array) atttributes
    'name' => 'foo',
    'value' => 'bar',
]) }}
<input type="button" name="foo" value="bar" />

3.10.3.3.2. imageButton

{{= imageButton ([                  // (array) atttributes
    'name' => 'foo',
    'src' => '/images/map.png',
]) }}
<input type="image" name="foo" src="/images/map.png" />

3.10.3.3.3. submitButton

{{= submitButton ([                 // (array) atttributes
    'name' => 'foo',
    'value' => 'bar',
]) }}
<input type="submit" name="foo" value="bar" />

3.10.3.3.4. resetButton

{{= resetButton ([                  // (array) attributes
    'name' => 'foo',
    'value' => 'bar',
]) }}
<input type="reset" name="foo" value="bar" />

3.10.3.4. Label Tag

A helper for <label> tags.

{{= label (
    'Label For Field',              // (string) label text
    [                               // (array) optional attributes
        'for' => 'field'
    ]
) }}
<label for="field">Label For Field</label>