Form Generation Library


Prefixes and Suffixes

The prefix and suffix settings in the configuration file allow you to easily format your form output. Please note that the behaviour for fieldsets is a little different from all other elements.

Config File

$config['group_prefix'] = '';
$config['group_suffix'] = '';
$config['element_prefix'] = '';
$config['element_suffix'] = '';
$config['label_prefix'] = '';
$config['label_suffix'] = '';
$config['field_prefix'] = '';
$config['field_suffix'] = '';

Controller

// let's create a set of radiobuttons for example
$radios[] = array('m', 'male');
$radios[] = array('f', 'female');

$this->form->radiogroup('gender', $radios, 'Your gender', 'm');

Prefixes and suffixes can always be overwritten via $config['defaults'] or by supplying them with the element's attributes.

Examples

group_prefix / group_suffix

These values will be wrapped around all checkgroups and radiogroups.// config file
$config['group_prefix'] = '<div>';
$config['group_suffix'] = '</div>';

// output
<div>
<label class="left">Your gender</label>
<input type="radio" name="gender[]" value="m" checked="checked" class="check" id="cc7c2" /><label for="cc7c2" class="check">male</label>
<input type="radio" name="gender[]" value="f" class="check" id="db279" /><label for="db279" class="check">female</label>
</div>

element_prefix / element_suffix

These values will be wrapped around all elements (outside labels and fields). // config file
$config['element_prefix'] = '<p>';
$config['element_suffix'] = '</p>';

// output
<label class="left">Your gender</label>
<p><input type="radio" name="gender[]" value="m" checked="checked" class="check" id="cc7c2" /><label for="cc7c2" class="check">male</label></p>
<p><input type="radio" name="gender[]" value="f" class="check" id="db279" /><label for="db279" class="check">female</label></p>

label_prefix / label_suffix

These values will be wrapped around all labels. // config file
$config['label_prefix'] = '';
$config['label_suffix'] = '<br />';

// output
<label class="left">Your gender</label><br />
<input type="radio" name="gender[]" value="m" checked="checked" class="check" id="cc7c2" /><label for="cc7c2" class="check">male</label><br />
<input type="radio" name="gender[]" value="f" class="check" id="db279" /><label for="db279" class="check">female</label><br />

field_prefix / field_suffix

These values will be wrapped around all element fields.// config file
$config['field_prefix'] = '<br />';
$config['field_suffix'] = '';

// output
<label class="left">Your gender</label>
<br /><input type="radio" name="gender[]" value="m" checked="checked" class="check" id="cc7c2" /><label for="cc7c2" class="check">male</label>
<br /><input type="radio" name="gender[]" value="f" class="check" id="db279" /><label for="db279" class="check">female</label>

General Usage

The following form will show how the prefixes and suffixes will be applied in general:

<form>
{element_prefix}
<fieldset>
{label_prefix}<legend>Fieldset</legend>{label_suffix}
{field_prefix}

{element_prefix}
{label_prefix}<label>Input</label>{label_suffix}        // with label position = 'before' (default)
{field_prefix}<input type="" />{field_suffix}
{element_suffix}

{element_prefix}
{label_prefix}<label>Textarea</label>{label_suffix}
{field_prefix}<textarea></textarea>{field_suffix}
{element_suffix}

{group_prefix}

{element_prefix}
{field_prefix}<input type="radio" />{field_suffix}
{label_prefix}<label>Radio #1</label>{label_suffix}         // with label position = 'after' for radio
{element_suffix}

{element_prefix}
{field_prefix}<input type="radio" />{field_suffix}
{label_prefix}<label>Radio #2</label>{label_suffix}
{element_suffix}

{group_suffix}

{field_suffix}
</fieldset>
{element_suffix}

</form>

Note: Fieldset's element_prefix and element_suffix are wrapped outside of the <fieldset></fieldset> tags whereas field_prefix and field_suffix are wrapped inside (between </legend> and </fieldset>). Also label_prefix and label_suffix are wrapped around the <legend></legend> tags.