Skip to content

Basics of FormBuilder

Andreas Kollaros edited this page Jun 19, 2014 · 1 revision

#Basics of FormBuilder

Creating forms from classes

Define a class to render as html form.

<?php

class Topic
{
	public $title;

    public $content;

    public $approved;

	public $category;
}

Create the instance of class and optional set some values. These values will be rendered to the final html form.

$topic = new Topic();

$topic->title = 'My topic';

Create an instance of Form and inject the instance of class.

use Larium\FormBuilder\Form;

$form = new Form($topic);

Rendering forms

echo $form->label('Title', 'title');
echo $form->text('title');

echo $form->label('Content', 'content');
echo $form->textarea('content');

echo $form->label('Approved', 'approved');
echo $form->checkbox('approved');

echo $form->label('Category', 'category');
$options = ['Category 1', 'Category 2', 'Category 3'];
echo $form->select('category', $options);

HTML ouptut from above example.

<label for="topic_title">Title</label>
<input id="topic_title" type="text" name="topic[title]" value="My topic" />

<label for="topic_content">Content</label>
<textarea name="topic[content]" id="topic_content" rows="10" cols="30"></textarea>

<label for="topic_approved">Approved</label>
<input id="_topic_approved" type="hidden" name="topic[approved]" value="0" />
<input id="topic_approved" type="checkbox" name="topic[approved]" value="1" />

<label for="topic_category">Category</label>
<select name="topic[category]" id="topic_category">
    <option value="" selected="selected"></option>
    <option value="0">Category 1</option>
    <option value="1">Category 2</option>
    <option value="2">Category 3</option>
</select>
Clone this wiki locally