• This repository has been archived on 27/Feb/2018
  • Stars
    star
    419
  • Rank 103,397 (Top 3 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 11 years ago
  • Updated almost 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Rapid form generation with Bootstrap 3 and Laravel.

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.

//  <form method="POST">
//    <div class="form-group">
//      <label for="field_name">Field Label</label>
//      <input type="text" class="form-control" id="field_name" name="field_name">
//    </div>
//  </form>
{!! 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.

// <div class="form-group">
//    <label for="first_name">First Name</label>
//    <input type="text" class="form-control" id="first_name" name="first_name" placeholder="John Doe">
// </div>
BootForm::text('First Name', 'first_name')->placeholder('John Doe');

// <div class="form-group">
//   <label for="color">Color</label>
//   <select class="form-control" id="color" name="color">
//     <option value="red">Red</option>
//     <option value="green" selected>Green</option>
//   </select>
// </div>
BootForm::select('Color', 'color')->options(['red' => 'Red', 'green' => 'Green'])->select('green');

// <form method="GET" action="/users">
BootForm::open()->get()->action('/users');

// <div class="form-group">
//    <label for="first_name">First Name</label>
//    <input type="text" class="form-control" id="first_name" name="first_name" value="John Doe">
// </div>
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 Name</label>
    <input type="text" class="form-control" name="first_name" id="first_name">
  </div>
  <div class="form-group">
    <label for="last_name">Last Name</label>
    <input type="text" class="form-control" name="last_name" id="last_name">
  </div>
  <div class="form-group">
    <label for="date_of_birth">Date of Birth</label>
    <input type="date" class="form-control" name="date_of_birth" id="date_of_birth">
  </div>
  <div class="form-group">
    <label for="email">Email address</label>
    <input type="email" class="form-control" name="email" id="email">
  </div>
  <div class="form-group">
    <label for="password">Password</label>
    <input type="password" class="form-control" name="password" id="password">
  </div>
  <button type="submit" class="btn btn-default">Submit</button>
</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:

<div class="form-group {!! $errors->has('first_name') ? 'has-error' : '' !!}">
  <label for="first_name">First Name</label>
  <input type="text" class="form-control" id="first_name">
  {!! $errors->first('first_name', '<p class="help-block">:message</p>') !!}
</div>

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

More Repositories

1

laracon2017

PHP
469
star
2

workcation

Vue
425
star
3

vue-tailwind-examples

Vue
390
star
4

eloquent-oauth

Braindead simple social login with Laravel and Eloquent.
PHP
374
star
5

laravel-preset

PHP
277
star
6

sublime-phpunit

Run individual unit test files directly from Sublime
Python
276
star
7

theming-tailwind-demo

HTML
267
star
8

form

Super basic form HTML builder, only really exists so I can pull it in for some other more useful projects.
PHP
232
star
9

eloquent-oauth-l5

Eloquent OAuth service provider and assets for Laravel 5
PHP
214
star
10

tailwind-take-home-project

Vue
132
star
11

vue-shopify-sortable-demo

HTML
113
star
12

fullstackradio.com

JavaScript
90
star
13

test-driven-laravel-from-scratch

PHP
81
star
14

tailwind-css-variable-text-opacity-demo

JavaScript
77
star
15

entypo-optimized

61
star
16

tailwindcss-dark-mode-prototype

JavaScript
58
star
17

workcation-theme-ui

JavaScript
38
star
18

vueconfto-demo

Vue
38
star
19

rebuilding-netlify

JavaScript
32
star
20

sublime-themes

Personal mods for ST themes I use
31
star
21

tailwind-animation-playground

HTML
27
star
22

laracon-madrid

The source code for my "Advanced Vue Component Design" at Laracon EU Madrid.
Vue
27
star
23

cra-tailwind-3

HTML
27
star
24

tailwind-jit-vite-example

Vue
25
star
25

tailwind-starter

CSS
24
star
26

rebuilding-allbirds-with-tailwindcss

JavaScript
24
star
27

laracon-2020

Vue
24
star
28

faktory

An attempt to bring the wonders of FactoryGirl to PHP.
PHP
22
star
29

rebuilding-transistor

JavaScript
22
star
30

tailwindcss-jit-next

JavaScript
17
star
31

tailwind-ui-navbar-react

JavaScript
16
star
32

nuxt-storybook

Vue
16
star
33

workcation-splash-page

HTML
15
star
34

vue-sidebar-layout

Vue
15
star
35

next10-tailwind

JavaScript
14
star
36

cra-tailwind-demo

JavaScript
12
star
37

next-nested-routing

JavaScript
12
star
38

laracon-online-2019

PHP
10
star
39

jit-vite-preact

TypeScript
9
star
40

nuxt-nested-layouts

Vue
9
star
41

tweeter-eu

PHP
8
star
42

tailwind-jit-cra-example

JavaScript
7
star
43

tailwind-cra-next

JavaScript
7
star
44

jit-postcss-demo

HTML
7
star
45

vue-rename-tailwind-config

JavaScript
7
star
46

activerest

ActiveRecord wrapper for REST APIs
PHP
7
star
47

headbangstagram

JavaScript
6
star
48

postcss8-tailwind

JavaScript
6
star
49

dotfiles

JavaScript
6
star
50

solid-tailwind-setup-example

JavaScript
5
star
51

tailwind-browserslist-test

HTML
5
star
52

tailwind-color-name-test

JavaScript
5
star
53

facktory

An attempt to bring the wonders of FactoryGirl to PHP
PHP
5
star
54

phxtailwindui

Elixir
4
star
55

tw3-cra-typescript-example

HTML
4
star
56

tw-devtools-perf

Vue
4
star
57

tailwind-color-function-demo

JavaScript
4
star
58

socialnorm

PHP
4
star
59

eloquent-oauth-l4

Eloquent OAuth service provider and assets for Laravel 4
PHP
4
star
60

tailwind-2-aspect-ratio

CSS
4
star
61

upgrade-config

JavaScript
4
star
62

next-cssnano-issue

CSS
3
star
63

sveltekit-forms-esm-demo

Svelte
3
star
64

at-apply-stuck

JavaScript
3
star
65

game-of-life

HTML
3
star
66

tailwind-plugin-color-extend-example

JavaScript
3
star
67

vite-issue-repro

Vue
3
star
68

micheltazartes

JavaScript
3
star
69

lets-refactor-zendcon

PHP
3
star
70

tailwindcss-i2369

JavaScript
2
star
71

tailwind-repro-8597

HTML
2
star
72

base-comment-delete-test

CSS
2
star
73

focus-visible-test

CSS
2
star
74

socialnorm-google

Google provider for SocialNorm
PHP
2
star
75

eloquent-oauth-example

Basic example Laravel app with minimal setup required for Eloquent OAuth
PHP
2
star
76

issue-10419

JavaScript
2
star
77

tailwind-repro-8661

CSS
2
star
78

vue-tailwind-library

Vue
1
star
79

thready-site

HTML
1
star
80

next-color-import-test

JavaScript
1
star
81

next-tailwind-hot-reload-issue

JavaScript
1
star
82

tw-postcss7-purge-test

CSS
1
star
83

tailwind-issue-7685

HTML
1
star
84

simple-design-ssphp16

PHP
1
star
85

vite-vue2-jit-issue

HTML
1
star
86

gulp-postcss-dependency-bug

JavaScript
1
star
87

vagrant-php

Just a little provisioning script and Vagrantfile, I'm sure I'll refine it over time.
Shell
1
star
88

parcel-2-postcss-dependency-bug

JavaScript
1
star
89

tailwindcss-forms-class-strategy-test

CSS
1
star
90

tw-postcss8-issue

JavaScript
1
star
91

eloquent-oauth-l5-example

PHP
1
star
92

larawedding

PHP
1
star
93

socialnorm-linkedin

LinkedIn provider for SocialNorm
PHP
1
star
94

postcss-normalize-bug

CSS
1
star
95

postcss-use-bug

JavaScript
1
star
96

vite-jsx-build-fail

Vue
1
star
97

turbopack-test

CSS
1
star
98

metamarkdown

PHP
1
star