Laravel Form Builder


October 23rd, 2018


Laravel Form Builder is a package by Kristijan Husak that incorporates an API similar to Symfony’s form builder for Laravel 5 applications.

Form builder for Laravel 5 inspired by Symfony’s form builder. With the help of Laravels FormBuilder class creates forms that can be easily modified and reused.

Here’s an example of a simple form using the Form Builder:

3namespace App\Forms;
5use Kris\LaravelFormBuilder\Form;
7class SongForm extends Form
9 public function buildForm()
10 {
11 $this
12 ->add('name', 'text', [
13 'rules' => 'required|min:5'
14 ])
15 ->add('lyrics', 'textarea', [
16 'rules' => 'max:5000'
17 ])
18 ->add('publish', 'checkbox');
19 }

Then inside of a controller, you create a form object that will be available in the view:

1$form = $formBuilder->create(\App\Forms\SongForm::class, [
2 'method' => 'POST',
3 'url' => route('')

In your blade template, here’s how you’d render the form object:

1{!! form($form) !!}

On a store method in your controller, you can create a form object and perform validation on it with the following:

1$form = $formBuilder->create(\App\Forms\SongForm::class);
3if (!$form->isValid()) {
4 return redirect()->back()->withErrors($form->getErrors())->withInput();
7// ...

This package also comes with an artisan console command for generating forms and fields:

1php artisan make:form Forms/SongForm \
2 --fields="name:text, lyrics:textarea, publish:checkbox"

If you’re not familiar with Symfony or the Form Builder component, you might find this approach confusing—it’s one of the most complex Symfony components in my opinion. Though I am not sure the matching level of API this package supports as seen in the Symfony Form Component, getting used to generating the HTML from the form component might take you some getting used to.

I personally prefer the simpler approach to forms and form requests that Laravel provides out-of-the-box, but if you’re looking for something like the Symfony form builder give this package a shot!

You can check it out and get installation instructions on the official GitHub repo and extensive documentation is also available.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.

Laravel News Partners