Rapid form generation with Bootstrap 3 and Laravel.

Related tags

Laravel bootforms
Overview

Important: This package is not actively maintained. For bug fixes and new features, please fork.

BootForms

This Project Has Been Deprecated. Code Climate Coverage Status

BootForms builds on top of my more general Form package by adding another layer of abstraction to rapidly generate markup for standard Bootstrap 3 forms. Probably not perfect for your super custom branded ready-for-release apps, but a huge time saver when you are still in the prototyping stage!

Installing with Composer

You can install this package via Composer by running this command in your terminal in the root of your project:

composer require adamwathan/bootforms

Laravel

If you are using Laravel 4 or 5, you can get started very quickly by registering the included service provider.

Modify the providers array in config/app.php to include the BootFormsServiceProvider:

'providers' => [
    //...
    'AdamWathan\BootForms\BootFormsServiceProvider'
  ],

Add the BootForm facade to the aliases array in config/app.php:

'aliases' => [
    //...
    'BootForm' => 'AdamWathan\BootForms\Facades\BootForm'
  ],

You can now start using BootForms by calling methods directly on the BootForm facade:

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.

$formBuilder = new AdamWathan\Form\FormBuilder;

$formBuilder->setOldInputProvider($myOldInputProvider);
$formBuilder->setErrorStore($myErrorStore);
$formBuilder->setToken($myCsrfToken);

$basicBootFormsBuilder = new AdamWathan\BootForms\BasicFormBuilder($formBuilder);
$horizontalBootFormsBuilder = new AdamWathan\BootForms\HorizontalFormBuilder($formBuilder);

$bootForm = new AdamWathan\BootForms\BootForm($basicBootFormsBuilder, $horizontalBootFormsBuilder);

Note: You must provide your own implementations of AdamWathan\Form\OldInputInterface and AdamWathan\Form\ErrorStoreInterface when not using the implementations meant for Laravel.

Using BootForms

Basic Usage

BootForms lets you create a label and form control and wrap it all in a form group in one call.

//
// // //
// {!! BootForm::open() !!} {!! BootForm::text('Field Label', 'field_name') !!} {!! BootForm::close() !!} ">
//  
   
//
// // //
//
{!! BootForm::open() !!} {!! BootForm::text('Field Label', 'field_name') !!} {!! BootForm::close() !!}

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.

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.

Attributes can be added either via the attribute method, or by simply using the attribute name as the method name.

// // //
BootForm::text('First Name', 'first_name')->placeholder('John Doe'); //
// // //
BootForm::select('Color', 'color')->options(['red' => 'Red', 'green' => 'Green'])->select('green'); //
BootForm::open()->get()->action('/users'); //
// // //
BootForm::text('First Name', 'first_name')->defaultValue('John Doe'); ">
// 
   
// // //
BootForm::text('First Name', 'first_name')->placeholder('John Doe'); //
// // //
BootForm::select('Color', 'color')->options(['red' => 'Red', 'green' => 'Green'])->select('green'); // BootForm::open()->get()->action('/users'); //
// // //
BootForm::text('First Name', 'first_name')->defaultValue('John Doe');

For more information about what's possible, check out the documentation for my basic Form package.

Reduced Boilerplate

Typical Bootstrap form boilerplate might look something like this:

">
<form>
  <div class="form-group">
    <label for="first_name">First Namelabel>
    <input type="text" class="form-control" name="first_name" id="first_name">
  div>
  <div class="form-group">
    <label for="last_name">Last Namelabel>
    <input type="text" class="form-control" name="last_name" id="last_name">
  div>
  <div class="form-group">
    <label for="date_of_birth">Date of Birthlabel>
    <input type="date" class="form-control" name="date_of_birth" id="date_of_birth">
  div> <div class="form-group"> <label for="email">Email addresslabel> <input type="email" class="form-control" name="email" id="email"> div> <div class="form-group"> <label for="password">Passwordlabel> <input type="password" class="form-control" name="password" id="password"> div> <button type="submit" class="btn btn-default">Submitbutton> form>

BootForms makes a few decisions for you and allows you to pare it down a bit more:

{!! BootForm::open() !!}
  {!! BootForm::text('First Name', 'first_name') !!}
  {!! BootForm::text('Last Name', 'last_name') !!}
  {!! BootForm::date('Date of Birth', 'date_of_birth') !!}
  {!! BootForm::email('Email', 'email') !!}
  {!! BootForm::password('Password', 'password') !!}
  {!! BootForm::submit('Submit') !!}
{!! BootForm::close() !!}

Automatic Validation State

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.

Essentially, this takes code that would normally look like this:

has('first_name') ? 'has-error' : '' !!}"> {!! $errors->first('first_name', '

:message

') !!}
">
 
class="form-group {!! $errors->has('first_name') ? 'has-error' : '' !!}"> "text" class="form-control" id="first_name"> {!! $errors->first('first_name', '

:message

'
) !!}

And reduces it to this:

{!! BootForm::text('First Name', 'first_name') !!}

...with the has-error class being added automatically if there is an error in the session.

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:

// 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.

BootForm::text('First Name', 'first_name')->hideLabel()

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.

Help Blocks

You can add a help block underneath a form element using the helpBlock() helper.

BootForm::text('Password', 'password')->helpBlock('A strong password should be long and hard to guess.')

Note: This help block will automatically be overridden by errors if there are validation errors.

Model Binding

BootForms makes it easy to bind an object to a form to provide default values. Read more about it here.

BootForm::open()->action( route('users.update', $user) )->put()
BootForm::bind($user)
BootForm::close()

Related Resources

Comments
  • Support for ->required() on the label within a form group

    Support for ->required() on the label within a form group

    By intercepting the required($conditional) function here we will add automatically the control-label-required CSS class to the <label> HTML element.

    This way you are able to use CSS to style the <label> HTML element however you want, you can even use pseudo-elements to add an asterisk:

    label.control-label-required:after {
        content: " *";
    }
    

    Obviously we wont interfere with Form Builder and we'll call the required() function defined within adamwathan/form no matter what. The CSS class will just be added conditionally.

    opened by clemblanco 8
  • Generate CSRF token automatically

    Generate CSRF token automatically

    Hi,

    with L5 latest update the CSRF middleware is added to the application stack which means it's executed every time. For some reason, your Form Builder is not generating the hidden input for the _token. Any idea why or do we need to call a method?

    Thanks!

    enhancement 
    opened by slovenianGooner 8
  • Add form-group methods

    Add form-group methods

    This PR adds some handy methods to interact with the created FormGroup.

    • addGroupClass
    • removeGroupClass
    • groupData

    In my case this is particularly useful for setting the form-group-lg class to the form group.

    opened by Propaganistas 7
  • OldInput laravel

    OldInput laravel

    Hi,

    I can't seem to get the oldinput to work in laravel i have this form

    <?php $formTypeEdit=true; ?>
                {!! BootForm::openHorizontal($columnSizes)->put() !!}
                {{--{!! BootForm::bind($user) !!}--}}
                <h1>Edit User</h1>
    
                    <?php if($formTypeEdit){ ?>
                    {!! BootForm::hidden('id') !!}
                    <?php }?>
                    {!! BootForm::email(trans('user.fields.email'), 'email') !!}
                    <?php if(!$formTypeEdit){?>
                    {!! BootForm::password(trans('user.fields.password'), 'password') !!}
                    {!! BootForm::password(trans('user.fields.confirm_password'),'password_confirmation') !!}
                    <?php }?>
                    {!! BootForm::text(trans('user.fields.firstname'),'firstname') !!}
                    {!! BootForm::text(trans('user.fields.lastname'),'lastname') !!}
                {!! BootForm::submit('edit') !!}
                {!!  BootForm::close() !!}
    

    should i add something to it to get it to work or should it work out of the box?

    Thanks in advance,

    opened by realtwister 7
  • Configurable ControllerClasses in horizontal forms

    Configurable ControllerClasses in horizontal forms

    In HorizontalFormGroup.php the ControllerClass 'col-lg-' is hardCoded.

    protected function getControlClass()
    {
        return 'col-lg-' . $this->controlWidth;
    }
    

    Would be nice if this is configurable as well as the width itself. For example I want to set "col-sm-6 col-md-4 col-lg-2" for my labels and different settings for the control in this 3 classes again.

    I really like the approach of https://github.com/dwightwatson/bootstrap-form where this can be done via laravel configs.

    enhancement 
    opened by AlexErmer 7
  • Bug in select ?

    Bug in select ?

    Hello,

    I try to make a select with this code:

    ...
    $formatlist=array("0" => "Select a format", "1"=>"A4", "2"=>"A3"); 
    ...
    
    {{ BootForm::select('Format','format',$formatlist)}}
    

    and the result is:

    <select id="format" class="form-control" name="format">
    <option value="Select a format">Select a format</option>
    <option value="A4">A4</option>
    <option value="A3">A3</option>
    </select>
    

    instead of

    <select id="format" class="form-control" name="format">
    <option value="0">Select a format</option>
    <option value="1">A4</option>
    <option value="2">A3</option>
    </select>
    

    Where is the mistake ? can you help me please ?

    Best regards,

    Dominique

    opened by DominiqueFERET 7
  • [Feature] Required field

    [Feature] Required field

    Hi,

    Is there any option to mark field as required? I see this like new method on form element, like: BootForm::text('Name', 'name')->required() And then field will turn red with additional * or something :)

    enhancement 
    opened by msieprawski 7
  • Remove render() calls from API tests.

    Remove render() calls from API tests.

    Removing render() calls from BasicFormBuilderTest.php tests (they should auto-render). If render() is not required in practical use:

    {!! BootForm::inlineRadio('Hidden', 'visibility') !!}
    

    Then render() should not be called in your BasicFormBuilderTest.php tests. Thoughts?

    opened by jesseleite 6
  • New Feature, insert input-group in controls

    New Feature, insert input-group in controls

    • Created element InputGroup
    • Add method inputGroup in class FormGroup and GroupWrapper

    Usage

    BootForm::text('Username', 'username')->inputGroup()->beforeAddon('@')
    
    BootForm::text('Site', 'site')->inputGroup()->afterAddon('.com.br')
    
    opened by moura137 6
  • Laravel Error

    Laravel Error

    I managed to get the files installed however here is a new error i'm getting

    Argument 1 passed to AdamWathan\Form\ErrorStore\IlluminateErrorStore::__construct() must be an instance of Illuminate\Session\Store, instance of Illuminate\Session\SessionManager given, called in /vendor/adamwathan/bootforms/src/AdamWathan/BootForms/BootFormsServiceProvider.php on line 36 and defined

    opened by sicsol 6
  • Add support to input array

    Add support to input array

    I create a request

    php artisan make:request FormRequest

    Create a rules for input array

    public function rules() { $rules = []; foreach($this->request->get('input_name') as $key) { $rules['input_name.'.$key] = 'required'; } return $rules; }

    But receive a error:

    FatalErrorException in FormGroup.php line 0: Method AdamWathan\Form\Elements\Text::__toString() must not throw an exception

    opened by mjfavaro 5
  • Updated and maintained fork

    Updated and maintained fork

    Hi all,

    I've updated and started to maintain a fork of this package. You can take a look at:

    glhd/bootforms

    It's been updated for Laravel 5.6 and also has a lot more docblocks/etc for better autocomplete inside an IDE.

    opened by inxilpro 0
  • Support Laravel 5.5 auto-discovery

    Support Laravel 5.5 auto-discovery

    Hi,

    This is a pretty nifty form builder I am using on a couple of my projects, so I thought why not update it to:

    • Support provider & facade auto-discovery for Laravel 5.5 & above
    • Merge some PHPDocs from another pull request #110 (which make life real easier when working in an IDE such as PHPStorm, IMO)
    opened by soham2008xyz 0
  • On the fly field options

    On the fly field options

    Is it possible to generate "on the fly" field attributes in blade? In dependence of some conditions I need for example make checkbox disabled/enabled or checked/unchecked etc.

    opened by elephantux 0
  • Custom attribute for each option

    Custom attribute for each option

    I have this code: {!! BootForm::select( trans('project.client'), "client_id")->options($clients->mapForSelectForm())->select($project->client ? $project->client->getRouteKey() : null) !!}

    Is it possible to add extra attribute to each option?

    opened by ImArtur 0
  • Change columnSizes per form-group?

    Change columnSizes per form-group?

    I use Bootform::openHorizontal with an array with my columnSizes and it is applied to all form-groups I add to the form. On some fields I have a relatively long title and the input control is just a checkbox or date-field. I wonder if it is possible to override the columnSizes for specific form-groups?

    opened by apiening 1
Releases(v0.9.0)
Owner
Adam Wathan
Creator of Tailwind CSS, author of Refactoring UI, host of Full Stack Radio.
Adam Wathan
Composer package which adds support for HTML5 elements using Laravels Form interface (e.g. Form::date())

Laravel HTML 5 Inputs Composer package which adds support for HTML5 elements by extending Laravel's Form interface (e.g. Form::date()) Adds support fo

Small Dog Studios 11 Oct 13, 2020
Laravel Livewire form component with declarative Bootstrap 5 fields and buttons.

Laravel Livewire Forms Laravel Livewire form component with declarative Bootstrap 5 fields and buttons. Requirements Bootstrap 5 Installation composer

null 49 Oct 29, 2022
Builder - A handful of tools for Rapid Laravel Development

Grafite Builder Grafite has archived this project and no longer supports or develops the code. We recommend using only as a source of ideas for your o

Grafite Inc 997 Dec 22, 2022
Manage your staff from one place. Featuring Staff leave management πŸ–, payslips πŸ’΅ generation & emailing, messaging πŸ“¨and more πŸ› ! Built with ❀️ with Laravel

Staff Management System This little buddy can help you manage your staff database! Built with ?? with Laravel #FEATURES 1 Staff management/ database S

Ezekiel Oladejo 45 Jan 3, 2023
Localization Helper - Package for convenient work with Laravel's localization features and fast language files generation

Localization Helper Package for convenient work with Laravel's localization features and fast language files generation. Installation Via Composer $ c

Galymzhan Begimov 0 Jul 13, 2019
A code generation tool for Laravel developers.

Blueprint is an open-source tool for rapidly generating multiple Laravel components from a single, human readable definition. Watch a quick demo of Bl

Laravel Shift 2.4k Jan 5, 2023
Painless html generation

Painless html generation This package helps you generate HTML using a clean, simple and easy to read API. All elements can be dynamically generated an

Spatie 616 Dec 28, 2022
Blacksmith is a code generation tool which automates the creation of common files that you'd typically create for each entity in your application.

Blacksmith is a code generation tool which automates the creation of common files that you'd typically create for each entity in your application.

Indatus 197 Dec 30, 2022
Framework - πŸ™ƒ Phony. Real-like Fake Data Generation Framework

?? Framework This repository contains the ?? Phony Framework. ?? Start generating fake data with ?? Phony Framework, visit the main Phony Repository.

Phonyland 5 Oct 31, 2022
Boilerplate code for protecting a form with proof of work. Uses javascript in the browser to generate the hashcash and PHP on the server to generate the puzzle and validate the proof of work.

Boilerplate code for protecting a form with proof of work. Uses javascript in the browser to generate the hashcash and PHP on the server to generate the puzzle and validate the proof of work.

Jameson Lopp 28 Dec 19, 2022
Razorpay payment gateway integration in laravel with submit form and storing details in payment table.

Integrating razorpay payment gateway in laravel with submit form and storing payment details in payment table. How to settup the project in your local

Mohammed-Thamnees 3 Apr 15, 2021
A powerful form builder, for Laravel and other frameworks (stand-alone too)

Former A Laravelish way to create and format forms Former outputs form elements in HTML compatible with your favorite CSS framework (Bootstrap and Fou

null 1.3k Dec 22, 2022
A quick and incomplete example of how to validate a form using a FormValidator class on the simple-mvc

Simple MVC Description This repository is a simple PHP MVC structure from scratch. It uses some cool vendors/libraries such as Twig and Grumphp. For t

Romain Clair 1 Nov 24, 2021
Laravel Form builder for version 5+!

Laravel 5 form builder Form builder for Laravel 5 inspired by Symfony's form builder. With help of Laravels FormBuilder class creates forms that can b

Kristijan Husak 1.7k Dec 31, 2022
πŸ§™β€β™€οΈ Arcanist takes the pain out of building multi-step form wizards in Laravel.

Installation Arcanist requires PHP 8 and Laravel 8. composer require laravel-arcanist/arcanist Documentation You can find the full documentation here

Arcanist 378 Jan 3, 2023
Reactive Form Builder for Vue.js with Laravel Support

Dynamic Form Builder for Laravel with Vue.js Create even the most complex forms with ease, using two-sided validation, eloquent, nested elements, cond

Laraform 340 Dec 31, 2022
A TALL-based Laravel Livewire component to replace the (multiple) select HTML input form with beautiful cards.

TALL multiselect cards A TALL-based Laravel Livewire component to replace the (multiple) select HTML input form with beautiful cards. Table of content

Frederic Habich 19 Dec 14, 2022
Generate form validators for Laravel: an extension of way/generators

Laravel Form Validator Contents Introduction Installation Usage Example Tests Introduction After using Jeffrey Way's Generator and his Validator packa

John Evans 6 Jan 14, 2022
This package can use form request just as Laravel do.

Lumen Form Request This package can use form request just as Laravel do. Installation Install by composer $ composer require chhw/form-request In

null 2 Nov 17, 2021