Coordinates form validation, errors, messages, values, and inputs in a DRY KISS way.
Coordinates form validation, errors, messages, values, and inputs in a DRY KISS way.
A BootPress Validator Component instance.
If a form is submitted successfully then you should $page->eject()
them using this value.
An array($attr => $value, ...)
of attributes that will be included in the opening <form>
tag.
Any additional HTML that you want to be included just before the </form>
tag.
All of the hidden form inputs that we put after the $form->footer
.
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.
Creates the Form and Validator object instances.
The name of your form.
How you would like the form to be sent ie. 'post' or 'get'.
$form = new \BootPress\Form\Component('form');
Set public properties. Useful for Twig templates that can't set them directly.
The one you want to set. Either 'errors' (for the Validator), 'header', 'footer', 'hidden', or the 'values' above.
Make this an array($name => $value, ...)
to set multiple values at once.
Only used if $name is a string, and you're not setting any 'footer' HTML.
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.
The name of the field.
An array($value => $name, ...)
of options to display in the menu.
An optional non-value to prepend to the menu eg. ' '. This is used for select menus when you would like a blank option up top.
$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
Redirect the submitted form to prevent the back and refresh buttons from resubmitting it again.
if ($vars = $form->validator->certified()) {
// process $vars;
$form->eject();
}
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:
$page->charset
.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.
The opening <form>
tag.
echo $form->header();
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.
The fieldset's legend value.
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.
echo $form->fieldset('Sign In',
$form->text('username'),
$form->password('password')
);
Create an <input type="...">
field from an array of attributes. This is used internally when creating form fields using this class.
The type of input.
The input's other attributes.
An html input tag.
$form->footer[] = $form->input('submit', array('name' => 'Submit'));
echo $form->input('hidden', array('name' => 'field', 'value' => 'default'));
Create an <input type="text" ...>
field.
The text input's name.
Anything else you would like to add besides the 'name', 'id', 'value', and 'data-...' validation attributes.
$form->validator->set('name', 'required');
$form->validator->set('email', 'required|email');
echo $form->text('name');
echo $form->text('email');
Create an <input type="password" ...>
input field.
The password input's name.
Anything else you would like to add besides the 'name', 'id', 'value', and 'data-...' validation attributes.
$form->validator->set('password', 'required|alphaNumeric|minLength[5]|noWhiteSpace');
$form->validator->set('confirm', 'required|matches[password]');
echo $form->password('password');
echo $form->password('confirm');
Create checkboxes from the $form->menu($field)
you set earlier.
The checkbox's name.
Anything else you would like to add besides the 'name', 'value', 'checked', and 'data-...' validation attributes.
The html that surrounds each checkbox.
A checkbox <label><input type="checkbox" ...></label>
html tag.
$form->menu('remember', array('Y'=>'Remember Me'));
echo $form->checkbox('remember');
Create radio buttons from the $form->menu($field)
you set earlier.
The radio button's name.
Anything else you would like to add besides the 'name', 'value', 'checked', and 'data-...' validation attributes.
The html that surrounds each radio button.
Radio <label><input type="radio" ...></label>
html tags.
$form->menu('gender', array('M'=>'Male', 'F'=>'Female'));
$form->validator->set('gender', 'required|inList');
echo $form->radio('gender');
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.
The select menu's name.
Anything else you would like to add besides the 'name', 'id', and 'data-...' validation attributes.
A <select>
tag with all it's <option>
's.
$form->menu('save[]', array(
4 => 'John Locke',
8 => 'Hugo Reyes',
15 => 'James Ford',
16 => 'Sayid Jarrah',
23 => 'Jack Shephard',
42 => 'Jin & Sun Kwon',
)); // A multiselect menu
$form->menu('transport', array(
1 => 'Airplane',
2 => 'Boat',
3 => 'Submarine',
), ' '); // 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',
),
), ' '); // 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')
);
Create a <textarea ...>
field.
The textarea's name.
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.
$form->values['description'] = 'default';
echo $form->textarea('description');
Closes and cleans up shop.
The closing </form>
tag with the $form->footer
and $form->hidden
fields preceding it.
echo $form->close();
Add the following to your composer.json
file.
{
"require": {
"bootpress/form": "^1.0"
}
}
<?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.