You can now start using BootForms by calling methods directly on the BootForm facade:, (*5)
BootForm::text('Email', 'email');
Outside of Laravel
Usage outside of Laravel is a little trickier since there's a bit of a dependency stack you need to build up, but it's not too tricky., (*6)
$formBuilder = new TalentAsia\Form\FormBuilder;
$formBuilder->setOldInputProvider($myOldInputProvider);
$formBuilder->setErrorStore($myErrorStore);
$formBuilder->setToken($myCsrfToken);
$basicBootFormsBuilder = new TalentAsia\BootForms\BasicFormBuilder($formBuilder);
$horizontalBootFormsBuilder = new TalentAsia\BootForms\HorizontalFormBuilder($formBuilder);
$bootForm = new TalentAsia\BootForms\BootForm($basicBootFormsBuilder, $horizontalBootFormsBuilder);
Note: You must provide your own implementations of TalentAsia\Form\OldInputInterface and TalentAsia\Form\ErrorStoreInterface when not using the implementations meant for Laravel., (*7)
Using BootForms
Basic Usage
BootForms lets you create a label and form control and wrap it all in a form group in one call., (*8)
Note: Don't forget to open() forms before trying to create fields! BootForms needs to know if you opened a vertical or horizontal form before it can render a field, so you'll get an error if you forget., (*9)
Customizing Elements
If you need to customize your form elements in any way (such as adding a default value or placeholder to a text element), simply chain the calls you need to make and they will fall through to the underlying form element., (*10)
Attributes can be added either via the attribute method, or by simply using the attribute name as the method name., (*11)
Another nice thing about BootForms is that it will automatically add error states and error messages to your controls if it sees an error for that control in the error store., (*14)
Essentially, this takes code that would normally look like this:, (*15)
...with the has-error class being added automatically if there is an error in the session., (*17)
Horizontal Forms
To use a horizontal form instead of the standard basic form, simply swap the BootForm::open() call with a call to openHorizontal($columnSizes) instead:, (*18)
// Width in columns of the left and right side
// for each breakpoint you'd like to specify.
$columnSizes = [
'sm' => [4, 8],
'lg' => [2, 10]
];
{!! BootForm::openHorizontal($columnSizes) !!}
{!! BootForm::text('First Name', 'first_name') !!}
{!! BootForm::text('Last Name', 'last_name') !!}
{!! BootForm::text('Date of Birth', 'date_of_birth') !!}
{!! BootForm::email('Email', 'email') !!}
{!! BootForm::password('Password', 'password') !!}
{!! BootForm::submit('Submit') !!}
{!! BootForm::close() !!}
Additional Tips
Hiding Labels
You can hide labels by chaining the hideLabel() helper off of any element definition., (*19)
The label will still be generated in the markup, but hidden using Bootstrap's .sr-only class, so you don't reduce the accessibility of your form., (*21)
Help Blocks
You can add a help block underneath a form element using the helpBlock() helper., (*22)
BootForm::text('Password', 'password')->helpBlock('A strong password should be long and hard to guess.'), (*23)
Note: This help block will automatically be overridden by errors if there are validation errors., (*24)
Model Binding
BootForms makes it easy to bind an object to a form to provide default values., (*25)