Overview

Source Codacy Badge

This package is used to simplify the generation of HTML form inputs and the form tag itself. It can also create generic form elements by reverse engineering a database table. It's meant to be very simple without extra features you might find in other packages. For example, you wont find validation here, or saving state. Just dead-simple form elements.

The package is fully tested, but if you find any missing coverage, just let me know.

Also of note, I don't make any attempt to use a dependency injection container for loading the elements. In other words, you can't inject your own Text object into the form. This may be added in the future.

Form Builder (new as of 0.4.0)

Be sure to look at the documentation on the new form builder. The builder can create elements for you based on a database table. This makes it much faster to create the initial elements. Also, you can pass an array or an object that employs ArrayAccess to the builder to set the initial values of the form elements.

Builder Documentation

Installation

Installation via composer is easiest.

composer require vespula/form

You can also download or clone the repository. If you don't use composer, you will want to use some sort of autoloader.

Basic Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
<?php
// The `post` method is the default.
$form = new \Vespula\Form\Form('post');

// Set the id and class attributes
$form->id('my-form')->class('myClass');

// Display the opening tag for the form
echo $form->begin();

// Alternatively, you can output the opening tag in one line.
// However, the begin() method must come last!
echo $form->id('my-form')->class('myClass')->begin();

echo $form->text()->id('my-input')->value('Joe User');

// Close the form
echo $form->end();
?>

An example with HTML output

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
include '/path/to/vendor/autoload.php';
$form = new Vespula\Form\Form('get');
$form->id('my-form');
?>
<html doctype="html">

<head>
    <meta charset="utf-8" />
</head>

<body>
    <?php echo $form->begin(); ?>
        <div>
            <?php echo $form->label('My Label')->indent(3)->wrap($form->text()->id('foo')->placeholder('Put some text here'))->lf(); ?>
        </div>
        <div>
            <?php echo $form->select()->options(['cat'=>'cat', 'dog'=>'dog'])->indent(3)->lf(); ?>
        </div>
        <div>
            <?php echo $form->textarea()->value('Here is some text')->id('wolf')->lf(); ?>
        </div>
        <div>
            <?php echo $form->radio()->value('yes')->name('active')->id('active-yes')->label('Yes')->lf(); ?>
            <?php echo $form->radio()->value('no')->name('active')->id('active-no')->label('No')->lf(); ?>
        </div>
        <div>
            <?php echo $form->checkbox()->value('boxes')->name('containers[]')->id('boxes')->label('Boxes')->lf(); ?>
            <?php echo $form->checkbox()->value('bags')->name('containers[]')->id('bags')->label('Bags')->lf(); ?>
        </div>
        <div>
            <?php echo $form->button('My Button')->lf(); ?>
            <?php echo $form->submit('Submit')->lf(); ?>
        </div>
    <?php echo $form->end(); ?>
</body>
</html>

And the result...

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<html doctype="html">

<head>
    <meta charset="utf-8" />
</head>

<body>
    <form method="get" id="my-form">
        <div>
            <label>My Label
                <input type="text" id="foo" placeholder="Put some text here" />
            </label>
        </div>
        <div>
            <select>
                <option value="cat">cat</option>
                <option value="dog">dog</option>
            </select>
        </div>
        <div>
            <textarea id="wolf">Here is some text</textarea>
        </div>
        <div>
            <input type="radio" value="yes" name="active" id="active-yes" /><label for="active-yes">Yes</label>
            <input type="radio" value="no" name="active" id="active-no" /><label for="active-no">No</label>
        </div>
        <div>
            <input type="checkbox" value="boxes" name="containers[]" id="boxes" /><label for="boxes">Boxes</label>
            <input type="checkbox" value="bags" name="containers[]" id="bags" /><label for="bags">Bags</label>
        </div>
        <div>
            <button type="button">My Button</button>
            <button type="submit">Submit</button>
        </div>
    </form>
</body>
</html>

Direct access to form elements

You don't have to use a form object as a facade to create elements. If you want, you can create elements using the element classes instead. The form object simply provides convenient shortcuts to element creation.

1
2
3
4
<?php
$text = new \Vespula\Form\Element\Text();
echo $text->id('foo')->value('bar')->class('form-input');
?>