PHPackages                             balfour/laravel-form-builder - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. balfour/laravel-form-builder

ActiveLibrary[Templating &amp; Views](/categories/templating)

balfour/laravel-form-builder
============================

A library for building &amp; rendering forms in Laravel

0.0.1-alpha(6y ago)220MITPHPPHP &gt;=7.2.0

Since Feb 24Pushed 2y ago6 watchersCompare

[ Source](https://github.com/balfour-group/laravel-form-builder)[ Packagist](https://packagist.org/packages/balfour/laravel-form-builder)[ RSS](/packages/balfour-laravel-form-builder/feed)WikiDiscussions master Synced today

READMEChangelog (1)Dependencies (4)Versions (2)Used By (0)

laravel-form-builder
====================

[](#laravel-form-builder)

A library for building &amp; rendering forms in Laravel.

We originally developed this library for our internal systems which are based off of [Bootstrap 4](https://getbootstrap.com) and [CoreUI](https://coreui.io/). We've tried to *not be too opinionated about markup*; however the library does generate markup with Bootstrap 4 classes in mind.

If you're not using Bootstrap 4, or are using an older version, you'll need to add compatible CSS classes to handle the styling of components.

*This library is in early release and is pending unit tests.*

Table of Contents
-----------------

[](#table-of-contents)

- [Installation](#installation)
- [Creating a Form](#creating-a-form)
- [Filling a Form](#filling-a-form)
- [Form Validation](#form-validation)
    - [Controller Validation](#controller-validation)
    - [Form Request Validation](#form-request-validation)
- [Components](#components)
    - [Checkbox (Single)](#checkbox-single)
    - [Checkboxes (Multiple)](#checkboxes-multiple)
    - [DateInput](#dateinput)
    - [EmailInput](#emailinput)
    - [FieldSet](#fieldset)
    - [FileInput](#fileinput)
    - [HiddenInput](#hiddeninput)
    - [MobileNumberInput](#mobilenumberinput)
    - [NumberInput](#numberinput)
    - [PasswordInput](#passwordinput)
    - [PhoneNumberInput](#phonenumberinput)
    - [RadioButtonGroup](#radiobuttongroup)
    - [RichTextEditor](#richtextarea)
    - [Row](#row)
    - [Select](#select)
    - [TextArea](#textarea)
    - [TextInput](#textinput)
    - [TimePicker](#timepicker)
    - [ToggleSwitch](#toggleswitch)

Installation
------------

[](#installation)

```
composer require balfour/laravel-form-builder
```

Creating a Form
---------------

[](#creating-a-form)

You can either create your form on the fly by constructing a `Form`, or you can create a custom class such as `CreateUserForm` which builds the components in the class constructor.

```
namespace App\Forms;

use App\Models\Role;
use Balfour\LaravelFormBuilder\Form;
use Balfour\LaravelFormBuilder\Components\Checkboxes;
use Balfour\LaravelFormBuilder\Components\EmailInput;
use Balfour\LaravelFormBuilder\Components\MobileNumberInput;
use Balfour\LaravelFormBuilder\Components\NumberInput;
use Balfour\LaravelFormBuilder\Components\PhoneNumberInput;
use Balfour\LaravelFormBuilder\Components\Row;
use Balfour\LaravelFormBuilder\Components\TextInput;

class CreateUserForm extends Form
{
    public function __construct()
    {
        $this->route('post.create_user')
            ->button('Create')
            ->with([
                TextInput::build()
                    ->name('first_name')
                    ->required(),
                TextInput::build()
                    ->name('last_name')
                    ->required(),
                EmailInput::build()
                    ->required()
                    ->rule('unique:users'),
                Row::build()
                    ->with([
                        MobileNumberInput::build()
                            ->rule('unique:users'),
                        PhoneNumberInput::build()
                            ->name('office_number')
                            ->rule('unique:users'),
                    ]),
                Row::build()
                    ->with([
                        NumberInput::build()
                            ->name('phone_extension')
                            ->rule('unique:users'),
                        TextInput::build()
                            ->name('skype')
                            ->rule('unique:users'),
                    ]),
                Checkboxes::build()
                    ->name('roles')
                    ->options(Role::class)
                    ->setVisibility(auth()->user()->can('assign-role')),
            ]);
    }
}
```

You can create the form object in your controller and pass it through to your view.

```
return view('create_user', ['form' => new CreateUserForm()])
```

In your view, you can then call the `render` method on the form.

```

    Create User

{!! $form->render !!}

```

Filling a Form
--------------

[](#filling-a-form)

The `$form->fill()` method can be used to set the default values for all form components.

This method can either take an `array` or key =&gt; value pairs, or a `model`.

As an example, here is an `EditUserForm` which populates from an existing user model.

```
namespace App\Forms;

use App\Models\Role;
use App\Models\User;
use Balfour\LaravelFormBuilder\Form;
use Balfour\LaravelFormBuilder\Components\Checkboxes;
use Balfour\LaravelFormBuilder\Components\EmailInput;
use Balfour\LaravelFormBuilder\Components\MobileNumberInput;
use Balfour\LaravelFormBuilder\Components\NumberInput;
use Balfour\LaravelFormBuilder\Components\PhoneNumberInput;
use Balfour\LaravelFormBuilder\Components\Row;
use Balfour\LaravelFormBuilder\Components\TextInput;
use Illuminate\Validation\Rule;

class EditUserForm extends Form
{
    public function __construct(User $user)
    {
        $this->route('post.edit_user', [$user])
            ->button('Save')
            ->with([
                TextInput::build()
                    ->name('first_name')
                    ->required(),
                TextInput::build()
                    ->name('last_name')
                    ->required(),
                EmailInput::build()
                    ->required()
                    ->disabled(),
                Row::build()
                    ->with([
                        MobileNumberInput::build()
                            ->rule(Rule::unique('users')->ignore($user)),
                        PhoneNumberInput::build()
                            ->name('office_number')
                            ->rule(Rule::unique('users')->ignore($user)),
                    ]),
                Row::build()
                    ->with([
                        NumberInput::build()
                            ->name('phone_extension')
                            ->rule(Rule::unique('users')->ignore($user)),
                        TextInput::build()
                            ->name('skype')
                            ->rule(Rule::unique('users')->ignore($user)),
                    ]),
                Checkboxes::build()
                    ->name('roles')
                    ->options(Role::class)
                    ->setVisibility(auth()->user()->can('assign-role'))
                    ->defaults(function () use ($user) {
                        return $user->roles
                            ->pluck('id')
                            ->toArray();
                    }),
                ToggleSwitch::build()
                    ->name('is_two_factor_auth_enforced')
                    ->label('2 Factor Authentication Enforced')
                    ->onLabel('Yes')
                    ->offLabel('No'),
                ToggleSwitch::build()
                    ->name('is_enabled')
                    ->label('Account Enabled')
                    ->onLabel('Yes')
                    ->offLabel('No'),
            ])->fill($user);
    }
}
```

When we construct the form, we just need to pass in a user model and all components will be populated from the model.

```
use App\Forms\EditUserForm;
use App\Models\User;

$user = User::find(3);
$form = new EditUserForm($user);

echo $form->render();
```

Form Validation
---------------

[](#form-validation)

The library will automagically generate validation rules for you based on the nested components of the form. The `$form->getValidationRules()` method can be called to generate the rules.

The validation of the actual form data is left up to you.

### Controller Validation

[](#controller-validation)

```
use App\Forms\CreateUserForm;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

class UserController extends Controller
{
    public function postCreate(Request $request)
    {
        $request->validate((new CreateUserForm())->getValidationRules());
    }
}
```

### Form Request Validation

[](#form-request-validation)

```
namespace App\Http\Requests;

use App\Forms\CreateUserForm;
use Illuminate\Foundation\Http\FormRequest;

class CreateUserRequest extends FormRequest
{
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return (new CreateUserForm())->getValidationRules();
    }
}
```

Components
----------

[](#components)

The following components are bundled into this package:

- [Checkbox (Single)](#checkbox-single)
- [Checkboxes (Multiple)](#checkboxes-multiple)
- [DateInput](#dateinput)
- [EmailInput](#emailinput)
- [FieldSet](#fieldset)
- [FileInput](#fileinput)
- [HiddenInput](#hiddeninput)
- [MobileNumberInput](#mobilenumberinput)
- [NumberInput](#numberinput)
- [PasswordInput](#passwordinput)
- [PhoneNumberInput](#phonenumberinput)
- [RadioButtonGroup](#radiobuttongroup)
- [RichTextEditor](#richtextarea)
- [Row](#row)
- [Select](#select)
- [TextArea](#textarea)
- [TextInput](#textinput)
- [TimePicker](#timepicker)
- [ToggleSwitch](#toggleswitch)

You can also create your own custom components by implementing `ComponentInterface`, or `FormControlInterface` which extends `ComponentInterface`.

### Checkbox (single)

[](#checkbox-single)

```
use Balfour\LaravelFormBuilder\Components\Checkbox;

Checkbox::build()
    ->name('remember_me')
    ->label('Remember me for my next visit')
    ->defaults(1);

Checkbox::build()
    ->name('agree')
    ->label('I agree to the terms and conditions')
    ->rule('accepted');
```

### Checkboxes (multiple)

[](#checkboxes-multiple)

```
use App\Models\Role;
use Balfour\LaravelFormBuilder\Components\Checkboxes;

$user = auth()->user();

// using an array of options
$roles = Role::pluck('name', 'id')
    ->toArray();

Checkboxes::build()
    ->name('roles')
    ->options($roles)
    ->defaults(function () use ($user) {
        return $user->roles
            ->pluck('id')
            ->toArray();
    });

// using a model, or options class
// the class must have a static listify() method which returns an array of key => value pairs
Checkboxes::build()
    ->name('roles')
    ->options(Role::class)
    ->setVisibility(auth()->user()->can('assign-role'))
    ->defaults(function () use ($user) {
        return $user->roles
            ->pluck('id')
            ->toArray();
    });

// using a callable to resolve options
Checkboxes::build()
    ->name('roles')
    ->options(function () {
        return Role::pluck('name', 'id')
            ->toArray();
    });
```

### DateInput

[](#dateinput)

```
use Balfour\LaravelFormBuilder\Components\DateInput;

// if no label is given, it's automagically generated from the component's name
DateInput::build()
    ->name('start_date');

// we can add a custom validation rule on top of the automagically generated ones
DateInput::build()
    ->name('start_date')
    ->rule('after:today');
```

### EmailInput

[](#emailinput)

```
use Balfour\LaravelFormBuilder\Components\EmailInput;

EmailInput::build()
    ->name('email');

// here's another custom validation rule on top of the 'email' one
EmailInput::build()
    ->name('email')
    ->rule('unique:users,email');
```

### FieldSet

[](#fieldset)

```
use Balfour\LaravelFormBuilder\Components\DateInput;
use Balfour\LaravelFormBuilder\Components\FieldSet;

FieldSet::build()
    ->legend('General')
    ->with([
        DateInput::build()
            ->name('start_date')
            ->required(),
        DateInput::build()
            ->name('end_date')
            ->required(),
    ]);
```

### FileInput

[](#fileinput)

```
use Balfour\LaravelFormBuilder\Components\FileInput;

FileInput::build()
    ->name('photo')
    ->required()
    ->mimes(['image/jpeg'])
    ->maxSize('5120')
```

### HiddenInput

[](#hiddeninput)

```
use Balfour\LaravelFormBuilder\Components\HiddenInput;

HiddenInput::build()
    ->name('user_id')
    ->defaults(1);
```

### MobileNumberInput

[](#mobilenumberinput)

```
use Balfour\LaravelFormBuilder\Components\MobileNumberInput;

MobileNumberInput::build()
    ->defaults(auth()->user()->mobile_number);
```

### NumberInput

[](#numberinput)

```
use Balfour\LaravelFormBuilder\Components\NumberInput;

NumberInput::build()
    ->name('age');
```

### PasswordInput

[](#passwordinput)

```
use Balfour\LaravelFormBuilder\Components\PasswordInput;

PasswordInput::build();

// if you want a custom label...
PasswordInput::build()
    ->name('Your Password');
```

### PhoneNumberInput

[](#phonenumberinput)

```
use Balfour\LaravelFormBuilder\Components\PhoneNumberInput;

PhoneNumberInput::build()
    ->defaults(auth()->user()->phone_number);
```

### RadioButtonGroup

[](#radiobuttongroup)

```
use Balfour\LaravelFormBuilder\Components\RadioButtonGroup;

RadioButtonGroup::build()
    ->name('food_preference')
    ->options([
        [
            'key' => 'chicken',
            'value' => 'Chicken',
        ],
        [
            'key' => 'beef',
            'value' => 'Beef',
        ],
    ])
    ->defaults('chicken');

// you can also add icons
RadioButtonGroup::build()
    ->name('food_preference')
    ->options([
        [
            'key' => 'chicken',
            'value' => 'Chicken',
            'icon' => 'fas fa-egg',
        ],
        [
            'key' => 'beef',
            'value' => 'Beef',
            'icon' => 'fas fa-hamburger',
        ],
    ])
    ->defaults('chicken');
```

### RichTextEditor

[](#richtexteditor)

```
use Balfour\LaravelFormBuilder\Components\RichTextEditor;

// this will render a textarea with a 'wysiwyg' class
// it's up to you to instantiate a rich text editor on this element, such as with
// TinyMCE or Froala Editor
RichTextEditor::build()
    ->name('description')
    ->rows(10)
    ->required();
```

### Row

[](#row)

```
use Balfour\LaravelFormBuilder\Components\DateInput;
use Balfour\LaravelFormBuilder\Components\Row;

Row::build()
    ->with([
        DateInput::build()
            ->name('start_date')
            ->required(),
        DateInput::build()
            ->name('end_date')
            ->required(),
    ]);
```

### Select

[](#select)

```
use App\Models\Country;
use Balfour\LaravelFormBuilder\Components\Select;

// using an array of options, and include a default 'empty' option
Select::build()
    ->name('country_id')
    ->options([
        1 => 'South Africa',
        2 => 'Amsterdam',
        3 => 'Italy',
    ])
    ->hasEmptyOption();

// using a model, or options class
// the class must have a static listify() method which returns an array of key => value pairs
Select::build()
    ->name('country_id')
    ->options(Country::class)
    ->defaults(1);

// using a callable to resolve options
Select::build()
    ->name('country_id')
    ->options(function () {
        return [
            1 => 'South Africa',
            2 => 'Amsterdam',
            3 => 'Italy',
        ];
    });
```

### TextArea

[](#textarea)

```
use Balfour\LaravelFormBuilder\Components\TextArea;

TextArea::build()
    ->name('description')
    ->rows(15)
    ->required()
    ->defaults('This is the default description.');
```

### TextInput

[](#textinput)

```
use Balfour\LaravelFormBuilder\Components\TextInput;

TextInput::build()
    ->name('first_name')
    ->required()
    ->defaults(auth()->user()->first_name);

// you can show a placeholder message
TextInput::build()
    ->name('first_name')
    ->required()
    ->placeholder('Your First Name')
    ->defaults(auth()->user()->first_name);

// you can also customise the type, and add a custom validation rule
TextInput::build()
    ->name('email')
    ->type('email')
    ->required()
    ->defaults(auth()->user()->email)
    ->rule('unique:users,email');
```

### TimePicker

[](#timepicker)

```
use Balfour\LaravelFormBuilder\Components\TimePicker;

// the component is rendered as an input with a 'time-picker' class
// you'd need to use a timepicker js lib of your choice to render it
TimePicker::build()
    ->name('start_time')
    ->required()
    ->defaults('14:30');
```

### ToggleSwitch

[](#toggleswitch)

```
use Balfour\LaravelFormBuilder\Components\ToggleSwitch;

ToggleSwitch::build()
    ->name('two_factor_authentication_enabled')
    ->defaults(true);

// use custom on and off labels
ToggleSwitch::build()
    ->name('subscribe')
    ->onLabel('Yes')
    ->offLabel('No')
    ->defaults(true);
```

###  Health Score

20

—

LowBetter than 14% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity37

Early-stage or recently created project

 Bus Factor1

Top contributor holds 88.6% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

2269d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/054093c4138d9bea138f6226b632f8f7ad0adb516562480f7f77868d481e75f0?d=identicon)[balfourgroup](/maintainers/balfourgroup)

---

Top Contributors

[![matthewgoslett](https://avatars.githubusercontent.com/u/1571743?v=4)](https://github.com/matthewgoslett "matthewgoslett (31 commits)")[![nicja](https://avatars.githubusercontent.com/u/2102143?v=4)](https://github.com/nicja "nicja (4 commits)")

### Embed Badge

![Health badge](/badges/balfour-laravel-form-builder/health.svg)

```
[![Health](https://phpackages.com/badges/balfour-laravel-form-builder/health.svg)](https://phpackages.com/packages/balfour-laravel-form-builder)
```

###  Alternatives

[livewire/livewire

A front-end framework for Laravel.

23.5k75.5M1.8k](/packages/livewire-livewire)[roots/acorn

Framework for Roots WordPress projects built with Laravel components.

9682.1M97](/packages/roots-acorn)[watson/validating

Eloquent model validating trait.

9723.3M47](/packages/watson-validating)[yajra/laravel-oci8

Oracle DB driver for Laravel via OCI8

8703.0M17](/packages/yajra-laravel-oci8)[laragear/two-factor

On-premises 2FA Authentication for out-of-the-box.

339785.3k8](/packages/laragear-two-factor)[pressbooks/pressbooks

Pressbooks is an open source book publishing tool built on a WordPress multisite platform. Pressbooks outputs books in multiple formats, including PDF, EPUB, web, and a variety of XML flavours, using a theming/templating system, driven by CSS.

44643.1k1](/packages/pressbooks-pressbooks)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
