Skip to content

Sample form

Steve "Uru" West edited this page Aug 5, 2013 · 2 revisions
<?php

require('../vendor/autoload.php');

use Fuel\Fieldset\Input;

$form = new Fuel\Fieldset\Form;

// Create a radio group

$name = new Input\Text('username');
$form[] = $name->setLabel('User name');

$select = Input\Select::fromArray([
	'name' => 'contact',
	'_content' => [
		1 => 'Always',
		0 => 'Never',
		2 => 'Some of the time',
		'Random Optgroup!' => [
			'a' => 'One',
			'b' => 'Two',
			'c' => 'Three',
		],
	],
]);

$select->setLabel('Can we contact you with info?');

$form[] = $select;

$radio = Input\RadioGroup::fromArray([
	'name' => 'freebies',
	'_content' => [
		0 => 'No',
		1 => 'Yes',
	],
]);
$radio->setLabel('Free stuff?');
$form[] = $radio;

$checkbox = Input\CheckboxGroup::fromArray([
	'name' => 'animals',
	'_content' => [
		'd' => 'Dogs',
		'c' => 'Cats',
		'r' => 'Rabits',
	],
]);

$checkbox->setLabel('Which animals?');

$form[] = $checkbox;

$fieldset = new Fuel\Fieldset\Fieldset();

$textarea = new Input\Textarea('area');
$fieldset[] = $textarea;


$form[] = $fieldset;

$submit = new Fuel\Fieldset\Input\Submit('submit', array(), 'GO!');
$submit->setLabel('');
$form[] = $submit;

$input = new Fuel\Fieldset\Data\Input;
$form->repopulate($input);

// Output stuff here

echo '<pre>';
print_r($input->input());
echo '</pre>';

$render = new Fuel\Fieldset\Render\BasicRender;
echo $render->render($form);

Will produce a form that looks something like this:

<form>
    User name <input name="username" type="text"/><br>
    Can we contact you with info?
    <select name="contact">
        <option value="1">Always</option>
        <option value="0">Never</option>
        <option value="2">Some of the time</option>
        <optgroup name="" label="Random Optgroup!">
            <option value="a">One</option>
            <option value="b">Two</option>
            <option value="c">Three</option>
        </optgroup>
    </select><br>

    Free stuff?<br>
    No <input name="freebies" value="0" type="radio"/><br>
    Yes <input name="freebies" value="1" type="radio"/><br>
    Which animals?<br>Dogs <input name="animals[]" value="d" type="checkbox" checked="1"/><br>
    Cats <input name="animals[]" value="c" type="checkbox"/><br>
    Rabits <input name="animals[]" value="r" type="checkbox"/><br>
    <fieldset name="">
        area <textarea name="area" value="Area">Area</textarea>
    </fieldset>
    <input name="submit" value="GO!" type="submit"/><br>
</form>
Clone this wiki locally