├── src └── AdamWathan │ └── BootForms │ ├── Facades │ └── BootForm.php │ ├── Elements │ ├── HelpBlock.php │ ├── InputGroup.php │ ├── CheckGroup.php │ ├── HorizontalFormGroup.php │ ├── OffsetFormGroup.php │ ├── FormGroup.php │ └── GroupWrapper.php │ ├── BootForm.php │ ├── BootFormsServiceProvider.php │ ├── HorizontalFormBuilder.php │ └── BasicFormBuilder.php ├── composer.json ├── LICENSE └── readme.md /src/AdamWathan/BootForms/Facades/BootForm.php: -------------------------------------------------------------------------------- 1 | message = $message; 12 | $this->addClass('help-block'); 13 | } 14 | 15 | public function render() 16 | { 17 | $html = 'renderAttributes(); 19 | $html .= '>'; 20 | $html .= $this->message; 21 | $html .= '

'; 22 | 23 | return $html; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "adamwathan/bootforms", 3 | "description": "Just a Formbuilder with some Bootstrap specific conveniences. Remembers old input, retrieves error messages and handles all your boilerplate Bootstrap markup automatically.", 4 | "authors": [ 5 | { 6 | "name": "Adam Wathan", 7 | "email": "adam.wathan@gmail.com" 8 | } 9 | ], 10 | "license": "MIT", 11 | "require": { 12 | "php": ">=5.4.0", 13 | "adamwathan/form": "^0.9.0" 14 | }, 15 | "require-dev": { 16 | "phpunit/phpunit": "3.7.*", 17 | "mockery/mockery": "0.9.*", 18 | "satooshi/php-coveralls": "dev-master" 19 | }, 20 | "autoload": { 21 | "psr-0": { 22 | "AdamWathan\\BootForms": "src/" 23 | } 24 | }, 25 | "minimum-stability": "stable" 26 | } 27 | -------------------------------------------------------------------------------- /src/AdamWathan/BootForms/BootForm.php: -------------------------------------------------------------------------------- 1 | basicFormBuilder = $basicFormBuilder; 12 | $this->horizontalFormBuilder = $horizontalFormBuilder; 13 | } 14 | 15 | public function open() 16 | { 17 | $this->builder = $this->basicFormBuilder; 18 | return $this->builder->open(); 19 | } 20 | 21 | public function openHorizontal($columnSizes) 22 | { 23 | $this->horizontalFormBuilder->setColumnSizes($columnSizes); 24 | $this->builder = $this->horizontalFormBuilder; 25 | return $this->builder->open(); 26 | } 27 | 28 | public function __call($method, $parameters) 29 | { 30 | return call_user_func_array([$this->builder, $method], $parameters); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Adam Wathan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/AdamWathan/BootForms/Elements/InputGroup.php: -------------------------------------------------------------------------------- 1 | beforeAddon[] = $addon; 14 | 15 | return $this; 16 | } 17 | 18 | public function afterAddon($addon) 19 | { 20 | $this->afterAddon[] = $addon; 21 | 22 | return $this; 23 | } 24 | 25 | public function type($type) 26 | { 27 | $this->attributes['type'] = $type; 28 | return $this; 29 | } 30 | 31 | protected function renderAddons($addons) 32 | { 33 | $html = ''; 34 | 35 | foreach ($addons as $addon) { 36 | $html .= ''; 37 | $html .= $addon; 38 | $html .= ''; 39 | } 40 | 41 | return $html; 42 | } 43 | 44 | public function render() 45 | { 46 | $html = '
'; 47 | $html .= $this->renderAddons($this->beforeAddon); 48 | $html .= parent::render(); 49 | $html .= $this->renderAddons($this->afterAddon); 50 | $html .= '
'; 51 | 52 | return $html; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/AdamWathan/BootForms/Elements/CheckGroup.php: -------------------------------------------------------------------------------- 1 | label = $label; 13 | } 14 | 15 | public function render() 16 | { 17 | if ($this->inline === true) { 18 | return $this->label->render(); 19 | } 20 | 21 | $html = 'renderAttributes(); 23 | $html .= '>'; 24 | $html .= $this->label; 25 | $html .= $this->renderHelpBlock(); 26 | 27 | $html .= ''; 28 | 29 | return $html; 30 | } 31 | 32 | public function inline() 33 | { 34 | $this->inline = true; 35 | 36 | $class = $this->control()->getAttribute('type') . '-inline'; 37 | $this->label->removeClass('control-label')->addClass($class); 38 | 39 | return $this; 40 | } 41 | 42 | public function control() 43 | { 44 | return $this->label->getControl(); 45 | } 46 | 47 | public function __call($method, $parameters) 48 | { 49 | call_user_func_array([$this->label->getControl(), $method], $parameters); 50 | return $this; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/AdamWathan/BootForms/Elements/HorizontalFormGroup.php: -------------------------------------------------------------------------------- 1 | controlSizes = $controlSizes; 14 | } 15 | 16 | public function render() 17 | { 18 | $html = 'renderAttributes(); 20 | $html .= '>'; 21 | $html .= $this->label; 22 | $html .= '
'; 23 | $html .= $this->control; 24 | $html .= $this->renderHelpBlock(); 25 | $html .= '
'; 26 | 27 | $html .= ''; 28 | 29 | return $html; 30 | } 31 | 32 | protected function getControlClass() 33 | { 34 | $class = ''; 35 | foreach ($this->controlSizes as $breakpoint => $size) { 36 | $class .= sprintf('col-%s-%s ', $breakpoint, $size); 37 | } 38 | return trim($class); 39 | } 40 | 41 | public function __call($method, $parameters) 42 | { 43 | call_user_func_array([$this->control, $method], $parameters); 44 | return $this; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/AdamWathan/BootForms/Elements/OffsetFormGroup.php: -------------------------------------------------------------------------------- 1 | control = $control; 11 | $this->columnSizes = $columnSizes; 12 | } 13 | 14 | public function render() 15 | { 16 | $html = '
'; 17 | $html .= '
'; 18 | $html .= $this->control; 19 | $html .= '
'; 20 | 21 | $html .= '
'; 22 | 23 | return $html; 24 | } 25 | 26 | public function setColumnSizes($columnSizes) 27 | { 28 | $this->columnSizes = $columnSizes; 29 | return $this; 30 | } 31 | 32 | protected function getControlClass() 33 | { 34 | $class = ''; 35 | foreach ($this->columnSizes as $breakpoint => $sizes) { 36 | $class .= sprintf('col-%s-offset-%s col-%s-%s ', $breakpoint, $sizes[0], $breakpoint, $sizes[1]); 37 | } 38 | return trim($class); 39 | } 40 | 41 | public function __toString() 42 | { 43 | return $this->render(); 44 | } 45 | 46 | public function __call($method, $parameters) 47 | { 48 | call_user_func_array([$this->control, $method], $parameters); 49 | return $this; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/AdamWathan/BootForms/Elements/FormGroup.php: -------------------------------------------------------------------------------- 1 | label = $label; 15 | $this->control = $control; 16 | $this->addClass('form-group'); 17 | } 18 | 19 | public function render() 20 | { 21 | $html = 'renderAttributes(); 23 | $html .= '>'; 24 | $html .= $this->label; 25 | $html .= $this->control; 26 | $html .= $this->renderHelpBlock(); 27 | 28 | $html .= ''; 29 | 30 | return $html; 31 | } 32 | 33 | public function helpBlock($text) 34 | { 35 | if (isset($this->helpBlock)) { 36 | return; 37 | } 38 | $this->helpBlock = new HelpBlock($text); 39 | return $this; 40 | } 41 | 42 | protected function renderHelpBlock() 43 | { 44 | if ($this->helpBlock) { 45 | return $this->helpBlock->render(); 46 | } 47 | 48 | return ''; 49 | } 50 | 51 | public function control() 52 | { 53 | return $this->control; 54 | } 55 | 56 | public function label() 57 | { 58 | return $this->label; 59 | } 60 | 61 | public function __call($method, $parameters) 62 | { 63 | call_user_func_array([$this->control, $method], $parameters); 64 | return $this; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/AdamWathan/BootForms/Elements/GroupWrapper.php: -------------------------------------------------------------------------------- 1 | formGroup = $formGroup; 13 | $this->target = $formGroup->control(); 14 | } 15 | 16 | public function render() 17 | { 18 | return $this->formGroup->render(); 19 | } 20 | 21 | public function helpBlock($text) 22 | { 23 | $this->formGroup->helpBlock($text); 24 | return $this; 25 | } 26 | 27 | public function __toString() 28 | { 29 | return $this->render(); 30 | } 31 | 32 | public function addGroupClass($class) 33 | { 34 | $this->formGroup->addClass($class); 35 | return $this; 36 | } 37 | 38 | public function removeGroupClass($class) 39 | { 40 | $this->formGroup->removeClass($class); 41 | return $this; 42 | } 43 | 44 | public function groupData($attribute, $value) 45 | { 46 | $this->formGroup->data($attribute, $value); 47 | return $this; 48 | } 49 | 50 | public function labelClass($class) 51 | { 52 | $this->formGroup->label()->addClass($class); 53 | return $this; 54 | } 55 | 56 | public function hideLabel() 57 | { 58 | $this->labelClass('sr-only'); 59 | return $this; 60 | } 61 | 62 | public function required($conditional = true) 63 | { 64 | if ($conditional) { 65 | $this->formGroup->label()->addClass('control-label-required'); 66 | } 67 | 68 | call_user_func_array([$this->target, 'required'], [$conditional]); 69 | return $this; 70 | } 71 | 72 | public function inline() 73 | { 74 | $this->formGroup->inline(); 75 | return $this; 76 | } 77 | 78 | public function group() 79 | { 80 | $this->target = $this->formGroup; 81 | return $this; 82 | } 83 | 84 | public function label() 85 | { 86 | $this->target = $this->formGroup->label(); 87 | return $this; 88 | } 89 | 90 | public function control() 91 | { 92 | $this->target = $this->formGroup->control(); 93 | return $this; 94 | } 95 | 96 | public function __call($method, $parameters) 97 | { 98 | call_user_func_array([$this->target, $method], $parameters); 99 | return $this; 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /src/AdamWathan/BootForms/BootFormsServiceProvider.php: -------------------------------------------------------------------------------- 1 | registerErrorStore(); 26 | $this->registerOldInput(); 27 | $this->registerFormBuilder(); 28 | $this->registerBasicFormBuilder(); 29 | $this->registerHorizontalFormBuilder(); 30 | $this->registerBootForm(); 31 | } 32 | 33 | protected function registerErrorStore() 34 | { 35 | $this->app->singleton('adamwathan.form.errorstore', function ($app) { 36 | return new IlluminateErrorStore($app['session.store']); 37 | }); 38 | } 39 | 40 | protected function registerOldInput() 41 | { 42 | $this->app->singleton('adamwathan.form.oldinput', function ($app) { 43 | return new IlluminateOldInputProvider($app['session.store']); 44 | }); 45 | } 46 | 47 | protected function registerFormBuilder() 48 | { 49 | $this->app->singleton('adamwathan.form', function ($app) { 50 | $formBuilder = new FormBuilder; 51 | $formBuilder->setErrorStore($app['adamwathan.form.errorstore']); 52 | $formBuilder->setOldInputProvider($app['adamwathan.form.oldinput']); 53 | $formBuilder->setToken($app['session.store']->token()); 54 | 55 | return $formBuilder; 56 | }); 57 | } 58 | 59 | protected function registerBasicFormBuilder() 60 | { 61 | $this->app->singleton('bootform.basic', function ($app) { 62 | return new BasicFormBuilder($app['adamwathan.form']); 63 | }); 64 | } 65 | 66 | protected function registerHorizontalFormBuilder() 67 | { 68 | $this->app->singleton('bootform.horizontal', function ($app) { 69 | return new HorizontalFormBuilder($app['adamwathan.form']); 70 | }); 71 | } 72 | 73 | protected function registerBootForm() 74 | { 75 | $this->app->singleton('bootform', function ($app) { 76 | return new BootForm($app['bootform.basic'], $app['bootform.horizontal']); 77 | }); 78 | } 79 | 80 | /** 81 | * Get the services provided by the provider. 82 | * 83 | * @return array 84 | */ 85 | public function provides() 86 | { 87 | return ['bootform']; 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/AdamWathan/BootForms/HorizontalFormBuilder.php: -------------------------------------------------------------------------------- 1 | [2, 10]]) 16 | { 17 | $this->builder = $builder; 18 | $this->columnSizes = $columnSizes; 19 | } 20 | 21 | public function setColumnSizes($columnSizes) 22 | { 23 | $this->columnSizes = $columnSizes; 24 | return $this; 25 | } 26 | 27 | public function open() 28 | { 29 | return $this->builder->open()->addClass('form-horizontal'); 30 | } 31 | 32 | protected function formGroup($label, $name, $control) 33 | { 34 | $label = $this->builder->label($label, $name) 35 | ->addClass($this->getLabelClass()) 36 | ->addClass('control-label') 37 | ->forId($name); 38 | 39 | $control->id($name)->addClass('form-control'); 40 | 41 | $formGroup = new HorizontalFormGroup($label, $control, $this->getControlSizes()); 42 | 43 | if ($this->builder->hasError($name)) { 44 | $formGroup->helpBlock($this->builder->getError($name)); 45 | $formGroup->addClass('has-error'); 46 | } 47 | 48 | return $this->wrap($formGroup); 49 | } 50 | 51 | protected function getControlSizes() 52 | { 53 | $controlSizes = []; 54 | foreach ($this->columnSizes as $breakpoint => $sizes) { 55 | $controlSizes[$breakpoint] = $sizes[1]; 56 | } 57 | return $controlSizes; 58 | } 59 | 60 | protected function getLabelClass() 61 | { 62 | $class = ''; 63 | foreach ($this->columnSizes as $breakpoint => $sizes) { 64 | $class .= sprintf('col-%s-%s ', $breakpoint, $sizes[0]); 65 | } 66 | return trim($class); 67 | } 68 | 69 | public function button($value, $name = null, $type = "btn-default") 70 | { 71 | $button = $this->builder->button($value, $name)->addClass('btn')->addClass($type); 72 | return new OffsetFormGroup($button, $this->columnSizes); 73 | } 74 | 75 | public function submit($value = "Submit", $type = "btn-default") 76 | { 77 | $button = $this->builder->submit($value)->addClass('btn')->addClass($type); 78 | return new OffsetFormGroup($button, $this->columnSizes); 79 | } 80 | 81 | public function checkbox($label, $name) 82 | { 83 | $control = $this->builder->checkbox($name); 84 | $checkGroup = $this->checkGroup($label, $name, $control)->addClass('checkbox'); 85 | 86 | return new OffsetFormGroup($this->wrap($checkGroup), $this->columnSizes); 87 | } 88 | 89 | protected function checkGroup($label, $name, $control) 90 | { 91 | $label = $this->builder->label($label, $name)->after($control); 92 | 93 | $checkGroup = new CheckGroup($label); 94 | 95 | if ($this->builder->hasError($name)) { 96 | $checkGroup->helpBlock($this->builder->getError($name)); 97 | $checkGroup->addClass('has-error'); 98 | } 99 | 100 | return $checkGroup; 101 | } 102 | 103 | public function radio($label, $name, $value = null) 104 | { 105 | if (is_null($value)) { 106 | $value = $label; 107 | } 108 | 109 | $control = $this->builder->radio($name, $value); 110 | $checkGroup = $this->checkGroup($label, $name, $control)->addClass('radio'); 111 | 112 | return new OffsetFormGroup($this->wrap($checkGroup), $this->columnSizes); 113 | } 114 | 115 | public function file($label, $name, $value = null) 116 | { 117 | $control = $this->builder->file($name)->value($value); 118 | $label = $this->builder->label($label, $name) 119 | ->addClass($this->getLabelClass()) 120 | ->addClass('control-label') 121 | ->forId($name); 122 | 123 | $control->id($name); 124 | 125 | $formGroup = new HorizontalFormGroup($label, $control, $this->getControlSizes()); 126 | 127 | if ($this->builder->hasError($name)) { 128 | $formGroup->helpBlock($this->builder->getError($name)); 129 | $formGroup->addClass('has-error'); 130 | } 131 | 132 | return $formGroup; 133 | } 134 | 135 | public function __call($method, $parameters) 136 | { 137 | return call_user_func_array([$this->builder, $method], $parameters); 138 | } 139 | } 140 | -------------------------------------------------------------------------------- /src/AdamWathan/BootForms/BasicFormBuilder.php: -------------------------------------------------------------------------------- 1 | builder = $builder; 17 | } 18 | 19 | protected function formGroup($label, $name, $control) 20 | { 21 | $label = $this->builder->label($label)->addClass('control-label')->forId($name); 22 | $control->id($name)->addClass('form-control'); 23 | 24 | $formGroup = new FormGroup($label, $control); 25 | 26 | if ($this->builder->hasError($name)) { 27 | $formGroup->helpBlock($this->builder->getError($name)); 28 | $formGroup->addClass('has-error'); 29 | } 30 | 31 | return $this->wrap($formGroup); 32 | } 33 | 34 | protected function wrap($group) 35 | { 36 | return new GroupWrapper($group); 37 | } 38 | 39 | public function text($label, $name, $value = null) 40 | { 41 | $control = $this->builder->text($name)->value($value); 42 | 43 | return $this->formGroup($label, $name, $control); 44 | } 45 | 46 | public function password($label, $name) 47 | { 48 | $control = $this->builder->password($name); 49 | 50 | return $this->formGroup($label, $name, $control); 51 | } 52 | 53 | public function button($value, $name = null, $type = "btn-default") 54 | { 55 | return $this->builder->button($value, $name)->addClass('btn')->addClass($type); 56 | } 57 | 58 | public function submit($value = "Submit", $type = "btn-default") 59 | { 60 | return $this->builder->submit($value)->addClass('btn')->addClass($type); 61 | } 62 | 63 | public function select($label, $name, $options = []) 64 | { 65 | $control = $this->builder->select($name, $options); 66 | 67 | return $this->formGroup($label, $name, $control); 68 | } 69 | 70 | public function checkbox($label, $name) 71 | { 72 | $control = $this->builder->checkbox($name); 73 | 74 | return $this->checkGroup($label, $name, $control); 75 | } 76 | 77 | public function inlineCheckbox($label, $name) 78 | { 79 | return $this->checkbox($label, $name)->inline(); 80 | } 81 | 82 | protected function checkGroup($label, $name, $control) 83 | { 84 | $checkGroup = $this->buildCheckGroup($label, $name, $control); 85 | return $this->wrap($checkGroup->addClass('checkbox')); 86 | } 87 | 88 | protected function buildCheckGroup($label, $name, $control) 89 | { 90 | $label = $this->builder->label($label, $name)->after($control)->addClass('control-label'); 91 | 92 | $checkGroup = new CheckGroup($label); 93 | 94 | if ($this->builder->hasError($name)) { 95 | $checkGroup->helpBlock($this->builder->getError($name)); 96 | $checkGroup->addClass('has-error'); 97 | } 98 | return $checkGroup; 99 | } 100 | 101 | public function radio($label, $name, $value = null) 102 | { 103 | if (is_null($value)) { 104 | $value = $label; 105 | } 106 | 107 | $control = $this->builder->radio($name, $value); 108 | 109 | return $this->radioGroup($label, $name, $control); 110 | } 111 | 112 | public function inlineRadio($label, $name, $value = null) 113 | { 114 | return $this->radio($label, $name, $value)->inline(); 115 | } 116 | 117 | protected function radioGroup($label, $name, $control) 118 | { 119 | $checkGroup = $this->buildCheckGroup($label, $name, $control); 120 | return $this->wrap($checkGroup->addClass('radio')); 121 | } 122 | 123 | public function textarea($label, $name) 124 | { 125 | $control = $this->builder->textarea($name); 126 | 127 | return $this->formGroup($label, $name, $control); 128 | } 129 | 130 | public function date($label, $name, $value = null) 131 | { 132 | $control = $this->builder->date($name)->value($value); 133 | 134 | return $this->formGroup($label, $name, $control); 135 | } 136 | 137 | public function dateTimeLocal($label, $name, $value = null) 138 | { 139 | $control = $this->builder->dateTimeLocal($name)->value($value); 140 | 141 | return $this->formGroup($label, $name, $control); 142 | } 143 | 144 | public function email($label, $name, $value = null) 145 | { 146 | $control = $this->builder->email($name)->value($value); 147 | 148 | return $this->formGroup($label, $name, $control); 149 | } 150 | 151 | public function file($label, $name, $value = null) 152 | { 153 | $control = $this->builder->file($name)->value($value); 154 | $label = $this->builder->label($label, $name)->addClass('control-label')->forId($name); 155 | $control->id($name); 156 | 157 | $formGroup = new FormGroup($label, $control); 158 | 159 | if ($this->builder->hasError($name)) { 160 | $formGroup->helpBlock($this->builder->getError($name)); 161 | $formGroup->addClass('has-error'); 162 | } 163 | 164 | return $this->wrap($formGroup); 165 | } 166 | 167 | public function inputGroup($label, $name, $value = null) 168 | { 169 | $control = new InputGroup($name); 170 | if (!is_null($value) || !is_null($value = $this->getValueFor($name))) { 171 | $control->value($value); 172 | } 173 | 174 | return $this->formGroup($label, $name, $control); 175 | } 176 | 177 | public function __call($method, $parameters) 178 | { 179 | return call_user_func_array([$this->builder, $method], $parameters); 180 | } 181 | } 182 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | > **Important: This package is not actively maintained.** For bug fixes and new features, please fork. 2 | 3 | BootForms 4 | =============== 5 | 6 | [![This Project Has Been Deprecated.](http://www.repostatus.org/badges/0.1.0/abandoned.svg)](http://www.repostatus.org/#abandoned) 7 | [![Code Climate](https://codeclimate.com/github/adamwathan/bootforms/badges/gpa.svg)](https://codeclimate.com/github/adamwathan/bootforms) 8 | [![Coverage Status](https://coveralls.io/repos/adamwathan/bootforms/badge.svg?branch=master)](https://coveralls.io/r/adamwathan/bootforms?branch=master) 9 | 10 | BootForms builds on top of my more general [Form](https://github.com/adamwathan/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! 11 | 12 | - [Installation](#installing-with-composer) 13 | - [Using BootForms](#using-bootforms) 14 | - [Basic Usage](#basic-usage) 15 | - [Customizing Elements](#customizing-elements) 16 | - [Reduced Boilerplate](#reduced-boilerplate) 17 | - [Automatic Validation State](#automatic-validation-state) 18 | - [Horizontal Forms](#horizontal-forms) 19 | - [Additional Tips](#additional-tips) 20 | - [Related Resources](#related-resources) 21 | 22 | ## Installing with Composer 23 | 24 | You can install this package via Composer by running this command in your terminal in the root of your project: 25 | 26 | ```bash 27 | composer require adamwathan/bootforms 28 | ``` 29 | 30 | ### Laravel 31 | 32 | If you are using Laravel 4 or 5, you can get started very quickly by registering the included service provider. 33 | 34 | Modify the `providers` array in `config/app.php` to include the `BootFormsServiceProvider`: 35 | 36 | ```php 37 | 'providers' => [ 38 | //... 39 | 'AdamWathan\BootForms\BootFormsServiceProvider' 40 | ], 41 | ``` 42 | 43 | Add the `BootForm` facade to the `aliases` array in `config/app.php`: 44 | 45 | ```php 46 | 'aliases' => [ 47 | //... 48 | 'BootForm' => 'AdamWathan\BootForms\Facades\BootForm' 49 | ], 50 | ``` 51 | 52 | You can now start using BootForms by calling methods directly on the `BootForm` facade: 53 | 54 | ```php 55 | BootForm::text('Email', 'email'); 56 | ``` 57 | 58 | ### Outside of Laravel 59 | 60 | 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. 61 | 62 | ```php 63 | $formBuilder = new AdamWathan\Form\FormBuilder; 64 | 65 | $formBuilder->setOldInputProvider($myOldInputProvider); 66 | $formBuilder->setErrorStore($myErrorStore); 67 | $formBuilder->setToken($myCsrfToken); 68 | 69 | $basicBootFormsBuilder = new AdamWathan\BootForms\BasicFormBuilder($formBuilder); 70 | $horizontalBootFormsBuilder = new AdamWathan\BootForms\HorizontalFormBuilder($formBuilder); 71 | 72 | $bootForm = new AdamWathan\BootForms\BootForm($basicBootFormsBuilder, $horizontalBootFormsBuilder); 73 | ``` 74 | 75 | > Note: You must provide your own implementations of `AdamWathan\Form\OldInputInterface` and `AdamWathan\Form\ErrorStoreInterface` when not using the implementations meant for Laravel. 76 | 77 | ## Using BootForms 78 | 79 | ### Basic Usage 80 | 81 | BootForms lets you create a label and form control and wrap it all in a form group in one call. 82 | 83 | ```php 84 | //
85 | //
86 | // 87 | // 88 | //
89 | //
90 | {!! BootForm::open() !!} 91 | {!! BootForm::text('Field Label', 'field_name') !!} 92 | {!! BootForm::close() !!} 93 | ``` 94 | 95 | > 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. 96 | 97 | ### Customizing Elements 98 | 99 | 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. 100 | 101 | Attributes can be added either via the `attribute` method, or by simply using the attribute name as the method name. 102 | 103 | ```php 104 | //
105 | // 106 | // 107 | //
108 | BootForm::text('First Name', 'first_name')->placeholder('John Doe'); 109 | 110 | //
111 | // 112 | // 116 | //
117 | BootForm::select('Color', 'color')->options(['red' => 'Red', 'green' => 'Green'])->select('green'); 118 | 119 | //
120 | BootForm::open()->get()->action('/users'); 121 | 122 | //
123 | // 124 | // 125 | //
126 | BootForm::text('First Name', 'first_name')->defaultValue('John Doe'); 127 | ``` 128 | 129 | For more information about what's possible, check out the documentation for [my basic Form package.](https://github.com/adamwathan/form) 130 | 131 | ### Reduced Boilerplate 132 | 133 | Typical Bootstrap form boilerplate might look something like this: 134 | 135 | ```html 136 | 137 |
138 | 139 | 140 |
141 |
142 | 143 | 144 |
145 |
146 | 147 | 148 |
149 |
150 | 151 | 152 |
153 |
154 | 155 | 156 |
157 | 158 |
159 | ``` 160 | 161 | BootForms makes a few decisions for you and allows you to pare it down a bit more: 162 | 163 | ```php 164 | {!! BootForm::open() !!} 165 | {!! BootForm::text('First Name', 'first_name') !!} 166 | {!! BootForm::text('Last Name', 'last_name') !!} 167 | {!! BootForm::date('Date of Birth', 'date_of_birth') !!} 168 | {!! BootForm::email('Email', 'email') !!} 169 | {!! BootForm::password('Password', 'password') !!} 170 | {!! BootForm::submit('Submit') !!} 171 | {!! BootForm::close() !!} 172 | ``` 173 | 174 | ### Automatic Validation State 175 | 176 | 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. 177 | 178 | Essentially, this takes code that would normally look like this: 179 | 180 | ```php 181 |
182 | 183 | 184 | {!! $errors->first('first_name', '

:message

') !!} 185 |
186 | ``` 187 | 188 | And reduces it to this: 189 | 190 | ```php 191 | {!! BootForm::text('First Name', 'first_name') !!} 192 | ``` 193 | 194 | ...with the `has-error` class being added automatically if there is an error in the session. 195 | 196 | ### Horizontal Forms 197 | 198 | To use a horizontal form instead of the standard basic form, simply swap the `BootForm::open()` call with a call to `openHorizontal($columnSizes)` instead: 199 | 200 | ```php 201 | 202 | // Width in columns of the left and right side 203 | // for each breakpoint you'd like to specify. 204 | $columnSizes = [ 205 | 'sm' => [4, 8], 206 | 'lg' => [2, 10] 207 | ]; 208 | 209 | {!! BootForm::openHorizontal($columnSizes) !!} 210 | {!! BootForm::text('First Name', 'first_name') !!} 211 | {!! BootForm::text('Last Name', 'last_name') !!} 212 | {!! BootForm::text('Date of Birth', 'date_of_birth') !!} 213 | {!! BootForm::email('Email', 'email') !!} 214 | {!! BootForm::password('Password', 'password') !!} 215 | {!! BootForm::submit('Submit') !!} 216 | {!! BootForm::close() !!} 217 | ``` 218 | 219 | ### Additional Tips 220 | 221 | #### Hiding Labels 222 | 223 | You can hide labels by chaining the `hideLabel()` helper off of any element definition. 224 | 225 | `BootForm::text('First Name', 'first_name')->hideLabel()` 226 | 227 | 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. 228 | 229 | #### Help Blocks 230 | 231 | You can add a help block underneath a form element using the `helpBlock()` helper. 232 | 233 | `BootForm::text('Password', 'password')->helpBlock('A strong password should be long and hard to guess.')` 234 | 235 | > Note: This help block will automatically be overridden by errors if there are validation errors. 236 | 237 | #### Model Binding 238 | 239 | BootForms makes it easy to bind an object to a form to provide default values. Read more about it [here](https://github.com/adamwathan/form#model-binding). 240 | 241 | ```php 242 | BootForm::open()->action( route('users.update', $user) )->put() 243 | BootForm::bind($user) 244 | BootForm::close() 245 | ``` 246 | 247 | ## Related Resources 248 | 249 | - [Laravel Translatable BootForms](https://github.com/Propaganistas/Laravel-Translatable-Bootforms), integrates BootForms with Dimsav's [Laravel Translatable](https://github.com/dimsav/laravel-translatable) package 250 | --------------------------------------------------------------------------------