Laravel Form Builder

Laravel Form Builder

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:

<?php

namespace App\Forms;

use Kris\LaravelFormBuilder\Form;

class SongForm extends Form
{
    public function buildForm()
    {
        $this
            ->add('name', 'text', [
                'rules' => 'required|min:5'
            ])
            ->add('lyrics', 'textarea', [
                'rules' => 'max:5000'
            ])
            ->add('publish', 'checkbox');
    }
}

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

$form = $formBuilder->create(\App\Forms\SongForm::class, [
    'method' => 'POST',
    'url' => route('song.store')
]);

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

{!! form($form) !!}

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

$form = $formBuilder->create(\App\Forms\SongForm::class);

if (!$form->isValid()) {
    return redirect()->back()->withErrors($form->getErrors())->withInput();
}

// ...

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

php artisan make:form Forms/SongForm \
  --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: News


Newsletter

Join the weekly newsletter and never miss out on new tips, tutorials, and more.

Laravel News Partners

Laravel Jobs

Senior PHP/Laravel Developer: Your Dream Work Environment
Remote
iPhone Photography School
In-house Laravel Developer.
Gold Coast / Australia
MXstore
Laravel Developer
Oak Brook, IL
Tidal Commerce
Senior Backend Engineer
Santa Monica only
Saatchi Art
Senior Laravel Developer
San Francisco
Stitch Labs
Senior Software Developer
South Jordan, UT
Lendio
Full-Stack Developer
Paris, France
Wingly