├── .gitignore
├── LICENSE
├── README.md
├── composer.json
├── roadmap.md
└── src
├── Bootstrap4FormsFacade.php
├── Bootstrap4FormsServiceProvider.php
├── FormBuilder.php
└── FormService.php
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 | roadmap.md
3 |
4 | /.idea
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2018 Sean Tymon
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 all
13 | 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 THE
21 | SOFTWARE.
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Bootstrap 4 forms for Laravel 5/6/7/8
2 |
3 | [![Latest Version on Packagist][ico-version]][link-packagist]
4 | [![Total Downloads][ico-downloads]][link-downloads]
5 |
6 | This is a package for creating Bootstrap 4 styled form elements in Laravel 5/6.
7 |
8 | ## Features
9 |
10 | - Labels
11 | - Error messages
12 | - Bootstrap 4 markup and classes (including state, colors, and sizes)
13 | - Error validation messages
14 | - Form fill (using Model instance, array or after form submission when a validation error occurs)
15 | - Internationalization
16 | - Add parameters using php chaining approach
17 | - Zero dependences (no Laravel Collective dependency)
18 |
19 | ## Introduction
20 |
21 | ### Before
22 |
23 | ```html
24 |
25 |
26 |
32 | @if($errors->has('username'))
33 |
{{$errors->first('username')}}
34 | @endif
35 |
36 | ```
37 |
38 | ### After
39 |
40 | ```php
41 | Form::text('username', 'Username')
42 | ```
43 |
44 | ## Installation
45 |
46 | #### Require the package using Composer.
47 |
48 | ```bash
49 | composer require netojose/laravel-bootstrap-4-forms
50 | ```
51 |
52 | ### Laravel 5.5 or above
53 |
54 | If you is using Laravel 5.5, the auto discovery feature will make everything for you and your job is done, you can start using now. Else, follow the steps below to install.
55 |
56 | ### Laravel 5.4
57 |
58 | #### Add the service provider to your config/app.php file
59 |
60 | ```php
61 | 'providers' => [
62 | //...
63 | NetoJose\Bootstrap4Forms\Bootstrap4FormsServiceProvider::class,
64 | ],
65 | ```
66 |
67 | #### Add the BootForm facade to the aliases array in config/app.php:
68 |
69 | ```php
70 | 'aliases' => [
71 | //...
72 | 'Form' => NetoJose\Bootstrap4Forms\Bootstrap4FormsFacade::class,
73 | ],
74 | ```
75 |
76 | ## Usage
77 |
78 | ### Basic form controls
79 |
80 | #### Opening and closing a form
81 |
82 | ```php
83 | // Opening a form using POST method
84 |
85 | {!!Form::open()!!}
86 | // ... Form components here
87 | {!!Form::close()!!}
88 | ```
89 |
90 | > Opening the form will add \_token field automatically for you
91 |
92 | #### Inline form
93 |
94 | ```php
95 | // Making all inputs inline
96 | {!!Form::open()->formInline()!!}
97 |
98 | // You can use FALSE to turn off disable form inline
99 | {!!Form::open()->formInline(false)!!}
100 | ```
101 |
102 | #### Fieldset
103 |
104 | | Param | Type | Default | Description |
105 | | -------- | ------ | ------- | --------------- |
106 | | \$legend | string | null | Fieldset Legend |
107 |
108 | ```php
109 | // Example
110 | {!!Form::fieldsetOpen('Legend title')!!}
111 | // ... fieldset content
112 | {!!Form::fieldsetClose()!!}
113 | ```
114 |
115 | ### Basic inputs
116 |
117 | #### Text inputs
118 |
119 | | Param | Type | Default | Description |
120 | | --------- | ------ | ------- | ------------- |
121 | | \$name | string | null | Input name |
122 | | \$label | string | null | Input label |
123 | | \$default | string | null | Default value |
124 |
125 | ```php
126 | // Example
127 | {!!Form::text('name', 'User name')!!}
128 | ```
129 |
130 | ##### Textarea
131 |
132 | | Param | Type | Default | Description |
133 | | --------- | ------ | ------- | ------------- |
134 | | \$name | string | null | Input name |
135 | | \$label | string | null | Input label |
136 | | \$default | string | null | Default value |
137 |
138 | ```php
139 | // Example
140 | {!!Form::textarea('description', 'Description')!!}
141 | ```
142 |
143 | ##### Select
144 |
145 | | Param | Type | Default | Description |
146 | | --------- | ------ | ------- | -------------- |
147 | | \$name | string | null | Input name |
148 | | \$label | string | null | Input label |
149 | | \$options | array | [] | Select options |
150 | | \$default | string | null | Default value |
151 |
152 | ```php
153 | // Example
154 | {!!Form::select('city', 'Choose your city', [1 => 'Gotham City', 2 => 'Springfield'])!!}
155 | ```
156 |
157 | ##### Options
158 |
159 | | Param | Type | Default | Description |
160 | | ---------- | -------- | ------- | ------------- |
161 | | \$options | iterable | [] | Options list |
162 | | \$valueKey | string | null | key for value |
163 | | \$idKey | string | null | key for id |
164 |
165 | ```php
166 | // Example
167 |
168 | // With array
169 | {!!Form::select('city', 'Choose your city')->options([1 => 'Gotham City', 2 => 'Springfield'])!!}
170 |
171 | // With collection
172 | $cities = collect([1 => 'Gotham City', 2 => 'Springfield'])
173 | {!!Form::select('city', 'Choose your city')->options($cities)!!}
174 |
175 | // With model collection
176 | $cities = \App\City::all();
177 | {!!Form::select('city', 'Choose your city')->options($cities)!!}
178 |
179 | // Your model should have id and name attributes. If these keys are different, you can pass second and/or third parameters (you can use the second parameter to access some model acessor, also)
180 | $cities = \App\City::all();
181 | {!!Form::select('city', 'Choose your city')->options($cities, 'city_name', 'id_object_field')!!}
182 |
183 | // When you are using collections, you can use prepend method (https://laravel.com/docs/5.8/collections#method-prepend) to add an first empty value, like "Choose your city"
184 | $cities = \App\City::all();
185 | {!!Form::select('city', 'Choose your city')->options($cities->prepend('Choose your city', ''))!!}
186 | ```
187 |
188 | ##### Checkbox
189 |
190 | | Param | Type | Default | Description |
191 | | --------- | ------- | ------- | ------------- |
192 | | \$name | string | null | Input name |
193 | | \$label | string | null | Input label |
194 | | \$value | string | null | Input value |
195 | | \$checked | boolean | null | Default value |
196 |
197 | ```php
198 | // Example
199 | {!!Form::checkbox('orange', 'Orange')!!}
200 | ```
201 |
202 | ##### Radio
203 |
204 | | Param | Type | Default | Description |
205 | | --------- | ------- | ------- | ------------- |
206 | | \$name | string | null | Input name |
207 | | \$label | string | null | Input label |
208 | | \$value | string | null | Input value |
209 | | \$checked | boolean | null | Default value |
210 |
211 | ```php
212 | // Example
213 | {!!Form::radio('orange', 'Orange')!!}
214 | ```
215 |
216 | ##### File
217 |
218 | | Param | Type | Default | Description |
219 | | ------- | ------ | ------- | ----------- |
220 | | \$name | string | null | Input name |
221 | | \$label | string | null | Input label |
222 |
223 | ```php
224 | // Example
225 | {!!Form::file('doc', 'Document')!!}
226 | ```
227 |
228 | #### Date inputs
229 |
230 | | Param | Type | Default | Description |
231 | | --------- | ------ | ------- | ------------- |
232 | | \$name | string | null | Input name |
233 | | \$label | string | null | Input label |
234 | | \$default | string | null | Default value |
235 |
236 | ```php
237 | // Example
238 | {!!Form::date('birthday', 'Birthday')!!}
239 | ```
240 |
241 | #### Tel inputs
242 |
243 | | Param | Type | Default | Description |
244 | | --------- | ------ | ------- | ------------- |
245 | | \$name | string | null | Input name |
246 | | \$label | string | null | Input label |
247 | | \$default | string | null | Default value |
248 |
249 | ```php
250 | // Example
251 | {!!Form::tel('number', 'Phone number')!!}
252 | ```
253 |
254 | #### Time inputs
255 |
256 | | Param | Type | Default | Description |
257 | | --------- | ------ | ------- | ------------- |
258 | | \$name | string | null | Input name |
259 | | \$label | string | null | Input label |
260 | | \$default | string | null | Default value |
261 |
262 | ```php
263 | // Example
264 | {!!Form::time('hour', 'Meeting hour')!!}
265 | ```
266 |
267 | #### URL inputs
268 |
269 | | Param | Type | Default | Description |
270 | | --------- | ------ | ------- | ------------- |
271 | | \$name | string | null | Input name |
272 | | \$label | string | null | Input label |
273 | | \$default | string | null | Default value |
274 |
275 | ```php
276 | // Example
277 | {!!Form::urlInput('website', 'You website')!!}
278 | ```
279 |
280 | #### Range inputs
281 |
282 | | Param | Type | Default | Description |
283 | | --------- | ------ | ------- | ------------- |
284 | | \$name | string | null | Input name |
285 | | \$label | string | null | Input label |
286 | | \$default | string | null | Default value |
287 |
288 | ```php
289 | // Example
290 | {!!Form::range('name', 'User name')!!}
291 | ```
292 |
293 | ##### Hidden
294 |
295 | | Param | Type | Default | Description |
296 | | --------- | ------- | ------- | ------------- |
297 | | \$name | string | null | Input name |
298 | | \$default | boolean | null | Default value |
299 |
300 | ```php
301 | // Example
302 | {!!Form::hidden('user_id')!!}
303 | ```
304 |
305 | ##### Anchor
306 |
307 | | Param | Type | Default | Description |
308 | | ------- | ------ | ------- | ----------- |
309 | | \$value | string | null | Anchor text |
310 | | \$url | string | null | Anchor url |
311 |
312 | ```php
313 | // Example
314 | {!!Form::anchor("Link via parameter", 'foo/bar')!!}
315 | ```
316 |
317 | ##### Buttons
318 |
319 | | Param | Type | Default | Description |
320 | | ------- | ------ | ------- | ------------ |
321 | | \$value | string | null | Button value |
322 | | \$color | string | null | Button color |
323 | | \$size | string | null | button size |
324 |
325 | ###### Submit
326 |
327 | ```php
328 | // Example
329 | {!!Form::submit("Send form")!!}
330 | ```
331 |
332 | ###### Button
333 |
334 | ```php
335 | // Example
336 | {!!Form::button("Do something", "warning", "lg")!!}
337 | ```
338 |
339 | ###### Reset
340 |
341 | ```php
342 | // Example
343 | {!!Form::reset("Clear form")!!}
344 | ```
345 |
346 | ### Chainable methods
347 |
348 | > This package uses [chaining](https://en.wikipedia.org/wiki/Method_chaining) feature, allowing easly pass more parameters.
349 |
350 | ### Filling a form
351 |
352 | | Param | Type | Default | Description |
353 | | ------ | ------------ | ------- | ------------------------ |
354 | | \$data | object/array | array | Data fo fill form inputs |
355 |
356 | ```php
357 | // Examples
358 |
359 | // With initial data using a Model instance
360 | $user = User::find(1);
361 | {!!Form::open()->fill($user)!!}
362 |
363 | // With initial array data
364 | $user = ['name' => 'Jesus', 'age' => 33];
365 | {!!Form::open()->fill($user)!!}
366 | ```
367 |
368 | ### Url
369 |
370 | Use in anchors and forms openings
371 |
372 | | Param | Type | Default | Description |
373 | | ----- | ------ | ------- | ----------- |
374 | | \$url | string | null | Url |
375 |
376 | ```php
377 | // Example
378 | {!!Form::anchor("Link via url")->url('foo/bar')!!}
379 | ```
380 |
381 | ### Route
382 |
383 | Use in anchors and forms openings
384 |
385 | | Param | Type | Default | Description |
386 | | ------- | ------ | ------- | ----------- |
387 | | \$route | string | null | Route name |
388 |
389 | ```php
390 | // Example
391 | {!!Form::anchor("Link via route")->route('home')!!}
392 | ```
393 |
394 | ### Error Bag
395 |
396 | Use if you have more then one form per page. You set an identifier for each form, and the errors will be attached for that specific form
397 |
398 | | Param | Type | Default | Description |
399 | | ------- | ------ | ------- | -------------- |
400 | | \$value | string | null | Error bag name |
401 |
402 | ```php
403 | // Example: attach this form to a error bag called "registerErrorBag"
404 | {!!Form::open()->route('register.post')->errorBag("registerErrorBag")!!}
405 |
406 | // ------------------------------------------------------
407 |
408 | // Now, in your controller (register.post route), you can redirect the user to a form page again, with erros inside a error bag called "registerErrorBag"
409 | public function register(Request $request)
410 | {
411 | $validator = Validator::make($request->all(), [
412 | // ... rules here
413 | ]);
414 |
415 | if ($validator->fails()) {
416 | return redirect()
417 | ->route('register.form')
418 | ->withInput()
419 | ->withErrors($validator, 'registerErrorBag');
420 | }
421 |
422 | // Proced to register here
423 | }
424 |
425 | // ------------------------------------------------------
426 |
427 | // If your validation is on a Form Request, you can add a protected method "$errorBag" to set a ErrorBag name
428 |
429 | class RegisterRequest extends FormRequest
430 | {
431 |
432 | protected $errorBag = 'registerErrorBag';
433 |
434 | public function authorize()
435 | {
436 | return true;
437 | }
438 |
439 | public function rules()
440 | {
441 | return [
442 | // ... rules here
443 | ];
444 | }
445 | }
446 | ```
447 |
448 | ### Errors
449 |
450 | Show all errors inside a panel
451 |
452 | | Param | Type | Default | Description |
453 | | ------- | ------ | ------- | ----------- |
454 | | \$title | string | null | Panel title |
455 |
456 | ```php
457 | // Example
458 | {!!Form::errors("The form has errors")!!}
459 | ```
460 |
461 | ### Disable validation messages
462 |
463 | Disable success/error status and validation error message
464 |
465 | | Param | Type | Default | Description |
466 | | ---------- | ------- | ------- | --------------- |
467 | | \$disabled | boolean | false | Disabled status |
468 |
469 | ```php
470 | // Example
471 | {!!Form::text('username', 'User name')->disableValidation()!!}
472 |
473 | // You can use FALSE to turn off disable validation (to enable it)
474 | {!!Form::text('username', 'User name')->disableValidation(false)!!}
475 | ```
476 |
477 | ### Checked
478 |
479 | Set the checkbox/radio checked status
480 |
481 | | Param | Type | Default | Description |
482 | | --------- | ------- | ------- | -------------- |
483 | | \$checked | boolean | true | Checked status |
484 |
485 | ```php
486 | // Examples
487 |
488 | // Using readonly field
489 | {!!Form::checkbox('agree', 'I agree')->checked()!!}
490 |
491 | // You can use FALSE to turn off checked status
492 | {!!Form::checkbox('agree', 'I agree')->checked(false)!!}
493 | ```
494 |
495 | ### Inline
496 |
497 | Set the checkbox/radio checked status
498 |
499 | ```php
500 | // Examples
501 | {!!Form::radio('orange', 'Orange')->inline()!!}
502 |
503 | {!!Form::checkbox('orange', 'Orange')->inline()!!}
504 |
505 | // You can use FALSE to turn off inline status
506 | {!!Form::checkbox('orange', 'Orange')->inline(false)!!}
507 | ```
508 |
509 | ### Placeholder
510 |
511 | | Param | Type | Default | Description |
512 | | ------------- | ------ | ------- | ---------------- |
513 | | \$placeholder | string | null | Placeholder text |
514 |
515 | ```php
516 | // Example
517 | {!!Form::text('name', 'Name')->placeholder('Input placeholder')!!}
518 | ```
519 |
520 | ### Select Multiple
521 |
522 | ```php
523 | // Example
524 | {!!Form::select('city', 'Choose your city', [1 => 'Gotham City', 2 => 'Springfield'])->multiple()!!}
525 | ```
526 |
527 | ### Locale
528 |
529 | Using locale, the package will look for a resources/lang/{CURRENT_LANG}/forms/user.php language file and uses labels and help texts as keys for replace texts
530 |
531 | ```php
532 | // Example
533 | {!!Form::open()->locale('forms.user')!!}
534 | ```
535 |
536 | ### Help Text
537 |
538 | | Param | Type | Default | Description |
539 | | ------ | ------ | ------- | ----------- |
540 | | \$text | string | null | Help text |
541 |
542 | ```php
543 | // Example
544 | {!!Form::text('name', 'Name')->help('Help text here')!!}
545 | ```
546 |
547 | ### Custom attributes
548 |
549 | | Param | Type | Default | Description |
550 | | ------- | ----- | ------- | ----------------------- |
551 | | \$attrs | array | [] | Custom input attributes |
552 |
553 | ```php
554 | // Example
555 | {!!Form::text('name', 'Name')->attrs(['data-foo' => 'bar', 'rel'=> 'baz'])!!}
556 | ```
557 |
558 | ### Custom attributes in wrapper div (\