PHPackages                             jonob/formly - 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. jonob/formly

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

jonob/formly
============

Form helper for Laravel 4

v2.0.0(11y ago)169072[1 issues](https://github.com/JonoB/formly/issues)[1 PRs](https://github.com/JonoB/formly/pulls)PHPPHP &gt;=5.3.0

Since Jan 13Pushed 11y ago1 watchersCompare

[ Source](https://github.com/JonoB/formly)[ Packagist](https://packagist.org/packages/jonob/formly)[ RSS](/packages/jonob-formly/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)DependenciesVersions (17)Used By (0)

Laravel Form Package
====================

[](#laravel-form-package)

Forms support in Laravel, with Twitter Bootstrap styling. All form inputs use Laravel's form helpers to create the actual html. Some added goodies like setting form defaults, repopulating forms after failed validation and showing failed validation errors.

There is a [5 minute video overview](http://www.screencast.com/t/OiBx2IChh9) of using Formly. Although this was originally done for Laravel 3, the concepts remain identical.

Installation
============

[](#installation)

### Composer

[](#composer)

Add `"jonob/formly": "dev-master",` to the `require` section of your `composer.json`:

```
"require": {
	"jonob/formly": "dev-master"
},

```

Now run `composer update`.

### Laravel

[](#laravel)

Add the following code to the `providers` section of the `app/config/app.php` file:

```
'Jonob\Formly\FormlyServiceProvider',
```

Add the following code to the `aliases` section of the `app/config/app.php` file:

```
'Formly' => 'Jonob\Formly\Formly',
```

Usage
=====

[](#usage)

### Routes and Controllers

[](#routes-and-controllers)

To start off with, you create a new form object in your route/controller. This can be done with a static method as follows:

```
$form = Formly::make();
```

Or you can instantiate it like so:

```
$form = new Formly();
```

You then pass the form object to your view as follows. This means that that formly will be available in your view with the $form variable.

```
return View::make('posts.form')->with('form', $form);
```

### Forms

[](#forms)

In generaly, Formly follows Laravel's default form helpers in terms of the method names and function parameters. There are two exceptions. Firstly, all methods are called non-statically and secondly the second parameter in Formly is the input's label. For example:

```
// Standard Laravel form input
Form::text($name, $value, $attributes);

// Formly
$form->text($name, $label, $value, $attributes);
```

Because we specify the label name in the method, there is no need to have a separate label field on your form - Formly will generate it for you automatically.

When it comes to opening your forms, then you just call the open method as follows. Notice that its not necessary to specify the action - by default Formly will POST to the current URL. You can of course override this if you wish.

```
$form->open();
```

Using this method has the added benefit that a hidden CSRF token will be automatically inserted for you. You can override this if you want.

### Setting form values

[](#setting-form-values)

#### Using formly to set default values

[](#using-formly-to-set-default-values)

If you are populating your form from existing data (for example, if you are editng a record from your database), then its not necessary to do this for each field. Let Formly do all the work for you as follows:

```
// Get the single post from the post model
$post::find($post_id);

// Pass the default values to Formly
$form = Formly::make($post);

// Create the view
return View::make('posts.form')->with('form', $form);
```

In order for this to work, the field names for your forms MUST have the same names as your database fields. If they are not the same, then Formly has no idea how to connect the two together.

You can populate fields manually if you wish:

```
// Pass the default values to Formly
$form = Formly::make(array('start_date' => date('Y-m-d')));

// Create the view
return View::make('posts.form')->with('form', $form);
```

#### Setting default values inline for each input

[](#setting-default-values-inline-for-each-input)

Alternatively, you can also set default values for individual form fields in the actual form. Values set in this way will override defaults set via the method above.

```
$form->text('start_date', 'Start Date', date('Y-m-d'));
```

#### Setting default via $\_POST

[](#setting-default-via-_post)

Well, this is not something that you do - Formly does it for you automatically. If, for example, you try save a form and validation fails, then Formly will automatically repopulate each input with the posted data.

#### Cascade

[](#cascade)

Based on the above, its evident that there are 3 methods of populating your forms. The order of precedence is as follows:

- First check if there is post data for the input
- Secondly check if a value has been set inline
- Lastly check if form defaults have been set

### Validation

[](#validation)

Formly automatically hooks up to Laravel's validation library with very little effort. Lets look at a full example.

```
// Controller
public function edit($id)
{
	$post = Post::find($id);

	if ( ! $post) {
		// do something. Maybe redirect or 404?
	}

	return View::make('posts.form')
		->with('form', Formly::make($post));
}

public function update()
{
	$rules = array(
	    'name'  => 'required|max:50',
	    'email' => 'required|email|unique:users',
	);

	$validation = Validator::make($input = Input::get(), $rules);

	if ($validation->fails()) {
        return Redirect::to('posts/edit/' Input::get('id'))->withErrors($validation)->withInput(Input::get());
    }
    return Redirect::to('posts');
}
```

Notice that if validation fails, then its necessary to redirect with the errors and the input. By doing this, we achieve two things:

- The form will be automatically re-populated with the posted data.
- Any errors will be highlighted (if you have enabled the options in Formly; see below)

Note that you do not need to do anything special to your form - simply by returning withErrors() and withInput(), Formly knows what to do

### Submit buttons

[](#submit-buttons)

Creating a submit button is easy:

```
$form->submit('Save');
```

By default, Formly will add in the Twitter Bootstrap 'btn' class. You can override this in the third parameter if you want:

```
$form->submit('Save', $attributes, 'some-class');
```

There are also some shortcuts for all the Twitter Bootstrap button styles:

```
// create a button with a class of 'btn btn-primary'
$form->submitPrimary('Save');

// and so on...
$form->submitInfo('Save');
$form->submitSuccess('Save');
$form->submitWarning('Save');
$form->submitDanger('Save');
$form->submitInverse('Save');
```

### Formly Options

[](#formly-options)

There are a couple of options that allow you to customise how Formly works. You can override these when the class is instantiated or through the `setOption()` method. Note that `setOption()`can be used to set many options at once, or a single option.

```
$defaults = Post::find($id);

$options = array(
	'formClass' => 'form_vertical'
);

// Set multiple options when the class is instantiated
$form = Formly::make($defaults, $options);

// Set multiple options using setOption()
$form = Formly::make()->setOption($options);

// Set a single option using setOption()
$form = Formly::make()->setOption('formClass', 'form_vertical');
```

##### formClass (default: form\_horizontal)

[](#formclass-default-form_horizontal)

By default, forms are styled using form-horizontal, but you can choose any of Bootstrap's other styles, such as form-vertical, form-inline, form-search

##### autoToken (default: true)

[](#autotoken-default-true)

Automatically adds a csrf token to the form\_open method

##### nameAsId (default: true)

[](#nameasid-default-true)

Automatically creates an id for each field based on the field name

##### idPrefix (default: field\_)

[](#idprefix-default-field_)

If name\_as\_id is enabled, then this string will be prefixed to the id attribute

##### requiredLabel (default: .req)

[](#requiredlabel-default-req)

Say you want to identify a label as being a required field on your form. Using formly, you can just append this string to the label parameter, and Formly will automatically use the required\_prefix, required\_suffix and required\_class

```
$form->text('start_date', 'Start Date.req');
```

##### requiredPrefix (default:'')

[](#requiredprefix-default)

If the required\_label has been set, then the text from this variable will be prefixed to your label

##### requiredSuffix (default:' \*')

[](#requiredsuffix-default-)

If the required\_label has been set, then the text from this variable will be added to the end of your label

##### requiredClass (default: 'label-required')

[](#requiredclass-default-label-required)

If the required\_label has been set, then this class will be added to the label's attribute. You want the label to be bold, for example, which you can then style in your css

##### controlGroupError (default: 'error')

[](#controlgrouperror-default-error)

Display a class for the control group if an input field fails validation

##### displayInlineErrors (default: false)

[](#displayinlineerrors-default-false)

If the field has failed validation, then inline errors will be shown

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 93.8% 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

Every ~43 days

Recently: every ~130 days

Total

16

Last Release

4226d ago

Major Versions

v1.2.9 → v2.0.02014-10-22

### Community

Maintainers

![](https://www.gravatar.com/avatar/ebe750a6aa5d1d9ee3bb3b31cfb557c3e2e27ddfd632c315ac1e9166775f33a2?d=identicon)[JonoB](/maintainers/JonoB)

---

Top Contributors

[![JonoB](https://avatars.githubusercontent.com/u/627725?v=4)](https://github.com/JonoB "JonoB (30 commits)")[![dljfield](https://avatars.githubusercontent.com/u/6544682?v=4)](https://github.com/dljfield "dljfield (2 commits)")

---

Tags

laravelform

### Embed Badge

![Health badge](/badges/jonob-formly/health.svg)

```
[![Health](https://phpackages.com/badges/jonob-formly/health.svg)](https://phpackages.com/packages/jonob-formly)
```

###  Alternatives

[laravie/html

HTML and Form Builders for the Laravel Framework

36184.6k4](/packages/laravie-html)[tomsix/laravel-components-library

A collection of pre-made Blade components for Laravel 7.x and up

613.1k](/packages/tomsix-laravel-components-library)[webup/laravel-form

A Laravel package to help build forms.

147.2k1](/packages/webup-laravel-form)[cornford/bootstrapper

An easy way to intergrate Twitter Bootstrap with Laravel.

232.7k](/packages/cornford-bootstrapper)[raftalks/form

Easy way to make Forms with PHP. Specially useful with Laravel 4.

311.1k1](/packages/raftalks-form)

PHPackages © 2026

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