Laravel Form Builder and why you should use it
By: Juan Rivas
Tags: Backend, Laravel, Laravel Framework

Forms are hard, even the simplest ones can be plagued with lots of complexities. For example, should I use the PRG pattern o should just stick to the normal post request for a submitted form? Where do I put the form validations? How to render fields and validation errors? What about ajax validation?.

In some frameworks like Django or Symfony we have an integrated Forms package that deals with lots of use cases for forms. In Laravel, we don’t have any form package out of the box, but there is a third-party package that can be used called Laravel Form Builder which is inspired by Symfony Forms.

Let's install the package with the following command:

composer require kris/laravel-form-builder

I will use Bootstrap 4 for the examples, so I’ll install the optional package

composer require ycs77/laravel-form-builder-bs4

Simple example

In this example, the most important part that you should be aware of is the abstraction. Laravel form builder will abstract the validation and the render process in a single class.

Validation errors

The method $form->redirectIfNotValid() will act similar to $this->validate(request(), $rules)), but with one advantage, the package will change the form field label name in the validation errors, this is a neat feature that is somewhat missing with the common validation process.

Also, the rendered form will have the validation errors below the field, and if you are using bootstrap will have the error CSS classes.

Some validation rules will render in the HTML itself, allowing HTML5 validations.

This is the default behavior and can be disabled using ‘client_validation’ => false in the form parameters when building the form or can be overridden via protected attribute class using $clientValidationEnabled = false;

To achieve something similar without Laravel form builder you need to create the form from scratch, I’ll use LaravelCollective HTML package to illustrate:

This is the result of the form with validation errors

This approach has some other problems, for example, as you can see, the error description doesn’t match the field label name, if you add more fields you need to update your validation rules, if you need HTML5 validation you have to type and do it accordingly to your validation rules… etc.

Custom fields

When building a big site, I often find myself making the same fields over and over, for example, a select2 field, a datetime field with some Javascript library, an E-Sign field, etc. For these cases, you can build your own custom fields with your custom settings. Here is an example with a select2 field:

In this example, whenever we add a select2 field type, we will have integrated the javascript.

Conditional rendering field

Inspired by Drupal FAPI and JS Framework, I made a simple form that has a field that can appear depending on the option that you choose from another field. For this, I use a modified custom field called Group, which is a PR in the Laravel form builder Github repository.

My real implementation is a little more complicated because I use PHP-Vars-To-Js-Transformer for some field settings and has some other features, but a simplified version could be:

This code will render a new field when you update the Post category field and select Books.

Conclusions

There are lots of complexities behind a form, maybe this package is not suited for every case, but I think that can cover most of the cases. On the other hand, it allows me to abstract forms easily.