Form Component

Coordinates form validation, errors, messages, values, and inputs in a DRY KISS way.

use BootPress\Form\Component as Form;

Packagist License MIT HHVM Tested PHP 7 Supported Build Status Code Climate Test Coverage


Coordinates form validation, errors, messages, values, and inputs in a DRY KISS way.

public object $validator ;

public string $eject ;

@var

If a form is submitted successfully then you should $page->eject() them using this value.

public array $header ;

@var

An array($attr => $value, ...) of attributes that will be included in the opening <form> tag.

public array $footer ;

@var

Any additional HTML that you want to be included just before the </form> tag.

public array $hidden ;

@var

All of the hidden form inputs that we put after the $form->footer.

public array $values ;

@var

You should set this array($field => $value, ...) to all of the default values for the form you are going to create. After you $form->validator->certified() it, these will be all of your filtered and validated values.

public object __construct ( [ string $name = 'form' [, string $method = 'post' ]] )

Creates the Form and Validator object instances.

@param $name

The name of your form.

@param $method

How you would like the form to be sent ie. 'post' or 'get'.

@example
$form = new \BootPress\Form\Component('form');

public set ( string $property , string|array $name [, mixed $value = null ] )

Set public properties. Useful for Twig templates that can't set them directly.

@param $property

The one you want to set. Either 'errors' (for the Validator), 'header', 'footer', 'hidden', or the 'values' above.

@param $name

Make this an array($name => $value, ...) to set multiple values at once.

@param $value

Only used if $name is a string, and you're not setting any 'footer' HTML.

public menu ( string $field [, array $menu [, string $prepend = null ]] )

This establishes the options for a checkbox, radio, or select menu field. The values are passed to the $form->validator->menu[$field] so that you can $form->validator->set($field, 'inList') with no params, and still be covered.

@param $field

The name of the field.

@param $menu

An array($value => $name, ...) of options to display in the menu.

@param $prepend

An optional non-value to prepend to the menu eg. '&nbsp;'. This is used for select menus when you would like a blank option up top.

@example
$form->menu('gender', array(
    'M' => 'Male',
    'F' => 'Female',
)); // A radio menu

$form->validator->set('gender', 'required|inList');

$form->menu('remember', array('Y' => 'Remember Me')); // A checkbox

public eject ( void )

Redirect the submitted form to prevent the back and refresh buttons from resubmitting it again.

@example
if ($vars = $form->validator->certified()) {
    // process $vars;
    $form->eject();
}

public string header ( void )

Create a <form> with all of the attributes you have established in the $form->header array. The values we automatically set (but may be overridden) are:

  • 'name' => The name of your form.
  • 'method' => Either 'get' or 'post'.
  • 'action' => The url to send the form. If sending via 'post', then we add a 'submitted' query paramteter to the current page. If sending via 'get', then all of the current page's query parameters will be removed and placed in hidden input fields.
  • 'accept-charset' => The $page->charset.
  • 'autocomplete' => Set to 'off'.

If you add a numeric (in megabytes) 'upload' field then we convert the megabytes to bytes, add the 'enctype="multipart/form-data"' to the header, and set a 'MAX_FILE_SIZE' hidden input with the number of bytes allowed.

@return

The opening <form> tag.

@example
echo $form->header();

public string fieldset ( string $legend [, string $html ] )

Wrap a <fieldset> around the included $html, and place a nice <legend> up top. This is not very difficult to do by hand, but it does look nice with all of the $html $form->field()'s nicely indented and looking like they belong where they are.

@param $legend

The fieldset's legend value.

@param $html

The HTML you would like this fieldset to enclose (if any). These args can go on forever, and they are all included as additional $html (strings) to place in the <fieldset> just after the <legend>. If this is an array then we implode('', $html) and include that.

@example
echo $form->fieldset('Sign In',
    $form->text('username'),
    $form->password('password')
);

public string input ( string $type , string[] $attributes )

Create an <input type="..."> field from an array of attributes. This is used internally when creating form fields using this class.

@param $type

The type of input.

@param $attributes

The input's other attributes.

@return

An html input tag.

@example
$form->footer[] = $form->input('submit', array('name' => 'Submit'));

echo $form->input('hidden', array('name' => 'field', 'value' => 'default'));

public string text ( string $field [, string[] $attributes ] )

Create an <input type="text" ...> field.

@param $field

The text input's name.

@param $attributes

Anything else you would like to add besides the 'name', 'id', 'value', and 'data-...' validation attributes.

@example
$form->validator->set('name', 'required');
$form->validator->set('email', 'required|email');

echo $form->text('name');
echo $form->text('email');

public string password ( string $field [, string[] $attributes ] )

Create an <input type="password" ...> input field.

@param $field

The password input's name.

@param $attributes

Anything else you would like to add besides the 'name', 'id', 'value', and 'data-...' validation attributes.

@example
$form->validator->set('password', 'required|alphaNumeric|minLength[5]|noWhiteSpace');
$form->validator->set('confirm', 'required|matches[password]');

echo $form->password('password');
echo $form->password('confirm');

public string checkbox ( string $field [, string[] $attributes [, string $wrap = '<label>%s</label>' ]] )

Create checkboxes from the $form->menu($field) you set earlier.

@param $field

The checkbox's name.

@param $attributes

Anything else you would like to add besides the 'name', 'value', 'checked', and 'data-...' validation attributes.

@param $wrap

The html that surrounds each checkbox.

@return

A checkbox <label><input type="checkbox" ...></label> html tag.

@example
$form->menu('remember', array('Y'=>'Remember Me'));

echo $form->checkbox('remember');

public string radio ( string $field [, string[] $attributes [, string $wrap = '<label>%s</label>' ]] )

Create radio buttons from the $form->menu($field) you set earlier.

@param $field

The radio button's name.

@param $attributes

Anything else you would like to add besides the 'name', 'value', 'checked', and 'data-...' validation attributes.

@param $wrap

The html that surrounds each radio button.

@return

Radio <label><input type="radio" ...></label> html tags.

@example
$form->menu('gender', array('M'=>'Male', 'F'=>'Female'));
$form->validator->set('gender', 'required|inList');

echo $form->radio('gender');

public string select ( string $field [, string[] $attributes ] )

Create a select menu from the $form->menu($field) you set earlier.

If the $field is an array (identified by '[]' at the end), then this will be a multiple select menu unless you set $attributes['multiple'] = false. You can optionally include a 'size' attribute to override our sensible defaults.

You can get fairly fancy with these creating optgroups and hier menus. We'll let the examples speak for themselves.

@param $field

The select menu's name.

@param $attributes

Anything else you would like to add besides the 'name', 'id', and 'data-...' validation attributes.

@return

A <select> tag with all it's <option>'s.

@example
$form->menu('save[]', array(
    4 => 'John Locke',
    8 => 'Hugo Reyes',
    15 => 'James Ford',
    16 => 'Sayid Jarrah',
    23 => 'Jack Shephard',
    42 => 'Jin &amp; Sun Kwon',
)); // A multiselect menu

$form->menu('transport', array(
    1 => 'Airplane',
    2 => 'Boat',
    3 => 'Submarine',
), '&nbsp;'); // A select menu

$form->menu('vehicle', array(
    'hier' => 'transport',
    1 => array(
        'Boeing' => array(
            4 => '777',
            5 => '737',
        ),
        'Lockheed' => array(
            6 => 'L-1011',
            7 => 'HC-130',
        ),
        8 => 'Douglas DC-3',
        9 => 'Beechcraft',
    ),
    2 => array(
        10 => 'Black Rock',
        11 => 'Kahana',
        12 => 'Elizabeth',
        13 => 'Searcher',
    ),
    3 => array(
        14 => 'Galaga',
        15 => 'Yushio',
    ),
), '&nbsp;'); // A hierselect menu

$form->validator->set(array(
    'save[]' => 'required|inList|minLength[2]',
    'vehicle' => 'required|inList',
));

echo $form->fieldset('LOST',
    $form->select('save[]'),
    $form->select('transport'),
    $form->select('vehicle')
);

public string textarea ( string $field [, string[] $attributes ] )

Create a <textarea ...> field.

@param $field

The textarea's name.

@param $attributes

Anything else you would like to add besides the 'name', 'id', and 'data-...' validation attributes. If you don't set the 'cols' and 'rows' then we will.

@example
$form->values['description'] = 'default';

echo $form->textarea('description');

public string close ( void )

Closes and cleans up shop.

@return

The closing </form> tag with the $form->footer and $form->hidden fields preceding it.

@example
echo $form->close();
Document Your Code

Installation

Add the following to your composer.json file.

{
    "require": {
        "bootpress/form": "^1.0"
    }
}

Example Usage

<?php

use BootPress\Form\Component as Form;

$form = new Form('form', 'post');

// Create some menus
$form->menu('gender', array(
    'M' => 'Male',
    'F' => 'Female',
));

$form->menu('remember', array('Y' => 'Remember Me'));

// Set the default values
$form->set('values', array(
    'name' => 'Daniel',
    'email' => 'me@example.com',
    'gender' => 'M',
));

Now the form's menus and default values have been set up, and you have a $form->validator object filled with $_POST vars, ready to go. You don't have to use the BootPress Validator Component, but it sure makes things easier for you.

$form->validator->set(array(
    'name' => 'required',
    'email' => 'required|email',
    'gender' => 'required|inList',
    'password' => 'required|minLength[5]|noWhiteSpace',
    'confirm' => 'required|matches[password]',
    'feedback' => 'maxWords[2]',
    'remember' => 'yesNo',
));

if ($vars = $form->validator->certified()) {
    echo '<pre>'.print_r($vars, true).'</pre>';
    // $form->eject();
}

When you create a $form->menu(), we automatically pass it's values to the validator so that you can $form->validator->set('field', 'inList') with no params, and still be covered. That's why we didn't put 'inList[M,F]' for your gender above. To create the form:

echo $form->header();
echo $form->fieldset('Form', array(
    $form->text('name', array('class' => 'form-control')),
    $form->text('email', array('placeholder' => 'Email Address')),
    $form->radio('gender'),
    $form->password('password'),
    $form->password('confirm'),
    $form->textarea('feedback'),
    $form->checkbox('remember'),
    $form->input('submit', array('name' => 'Submit')),
));
echo $form->close();

That would give you the following HTML:

<form name="form" method="post" action="http://example.com?submitted=form" accept-charset="utf-8" autocomplete="off">

    <fieldset><legend>Form</legend>

        <input type="text" class="form-control" name="name" id="nameI" value="Daniel" data-rule-required="true">

        <input type="text" placeholder="Email Address" name="email" id="emailII" value="me@example.com" data-rule-required="true" data-rule-email="true">

        <div class="radio"><label><input type="radio" name="gender" value="M" checked="checked" data-rule-required="true" data-rule-inList="M,F"> Male</label></div>
        <div class="radio"><label><input type="radio" name="gender" value="F"> Female</label></div>

        <input type="password" name="password" id="passwordIV" data-rule-required="true" data-rule-minlength="5" data-rule-nowhitespace="true">

        <input type="password" name="confirm" id="confirmV" data-rule-required="true">

        <textarea name="feedback" id="feedbackVI" cols="40" rows="10" data-rule-maxWords="2"></textarea>

        <div class="checkbox"><label><input type="checkbox" name="remember" value="Y"> Remember Me</label></div>

        <input type="submit" name="Submit">

    </fieldset>

</form>

You may want to put some labels and error messages in there, but this Form component is meant to be a bare-bones, just-get-the-hard-stuff-done first, so that you can style it anyway you like.