Form Builder Example

Initial Setup

Imagine a posts table like the following:

+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| id          | int(11)      | NO   | PRI | NULL    | auto_increment |
| title       | varchar(255) | NO   |     | NULL    |                |
| post        | text         | NO   |     | NULL    |                |
| userid      | varchar(8)   | NO   | MUL | NULL    |                |
| created_at  | datetime     | YES  |     | NULL    |                |
| updated_at  | datetime     | YES  |     | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+
<?php
use \Vespula\Form\Form;
use \Vespula\Form\Builder\Builder as FormBuilder;

$pdo = new \PDO('mysql:host=localhost;dbname=test', 'username', '********');

$form = new Form();
$form->autoLf();
$builder = new FormBuilder($pdo);
$form->setBuilder($builder);

// Some function or method that gets a post from the DB by primary Key
// Returns associative array, keyed by column name
$post = getPost($id);

$users = [
    'juser'=>'Joe User',
    'jdoe'=>'Jane Doe'
];

$builder->setColumnOptions([
    'userid'=>[
        'type'=>'select',
        'callback'=>function ($element) use ($users) {
            $element->options($users);
        }
    ]
]);

// Set some default classes (bootsrap-ish in this case)
$builder->setDefaultClasses([
    'textarea'=>'form-control',
    'text'=>'form-control',
    'select'=>'form-control'
]);

// Build the form object based on the posts table and $posts array
$form->build('posts', $post);

Displaying the Form Elements

You might have a template/view called form.php that looks like this:

<h1>Edit a Post</h1>

<h2><?=$post['title']; ?></h2>

<?=$form->begin(); ?>
<?=$form->getElement('id'); ?>

<div class="form-group">
    <?=$form->getElement('label_title'); ?>
    <?=$form->getElement('title'); ?>
</div>
<div class="form-group">
    <?=$form->getElement('label_post'); ?>
    <?=$form->getElement('post'); ?>
</div>
<div class="form-group">
    <?=$form->getElement('label_userid')->text('Author'); ?>
    <?=$form->getElement('userid')->indent(1); ?>
</div>

<?=$form->submit('Save')->class('btn btn-primary'); ?>
<?=$form->end(); ?>

And the resulting HTML might look like this:

<h1>Edit a Post</h1>

<form method="post">
<input type="hidden" id="id" name="id" value="1" maxlength="11" />
<div class="form-group">
    <label for="title">Title</label>
    <input type="text" id="title" name="title" value="My Post Title" class="form-control" maxlength="255" required />
</div>
<div class="form-group">
    <label for="post">Post</label>
    <textarea cols="10" rows="5" id="post" name="post" class="form-control" required>Blah blah blah</textarea>
</div>
<div class="form-group">
    <label for="userid">Author</label>
    <select id="userid" name="userid" class="form-control" required>
        <option value="juser">Joe User</option>
        <option value="jdoe" selected>Jane Doe</option>
    </select>
</div>

<button type="submit" class="btn btn-primary">Save</button>
</form>