PHPackages                             anlutro/form - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. anlutro/form

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

anlutro/form
============

Dynamic form builder.

0.3.14(10y ago)131484[2 issues](https://github.com/anlutro/php-form/issues)1MITPHPPHP &gt;=5.4.0

Since Jan 21Pushed 10y ago4 watchersCompare

[ Source](https://github.com/anlutro/php-form)[ Packagist](https://packagist.org/packages/anlutro/form)[ RSS](/packages/anlutro-form/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependencies (5)Versions (24)Used By (1)

PHP Form Builder [![Build Status](https://camo.githubusercontent.com/95b0ba74d0252dbf64428e388345065d35b6cc83e524d296f0d2f7bc6b3ace97/68747470733a2f2f7472617669732d63692e6f72672f616e6c7574726f2f7068702d666f726d2e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/anlutro/php-form) [![Latest Version](https://camo.githubusercontent.com/5762520921d56f212d6c6a15e52cb0b7c2f0961a1b7d32d494ebc3284adde8ad/687474703a2f2f696d672e736869656c64732e696f2f6769746875622f7461672f616e6c7574726f2f7068702d666f726d2e737667)](https://github.com/anlutro/php-form/releases)
==================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================

[](#php-form-builder--)

Sick of the bugs and quirks present in the default Laravel 4 form builder, I wrote my own and made it framework-agnostic. Features include:

- Each form is its own object with (optionally) its own behaviours
- Set a model for the form object to pre-fill the form inputs with
- Get old input from session
- Validate input

WARNING: Backwards compatibility is not guaranteed during version 0.x.

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

[](#installation)

`composer require anlutro/form`

Pick the latest stable version from packagist or the GitHub tag list.

### Laravel 4

[](#laravel-4)

Add 'anlutro\\Form\\ServiceProvider' to the list of providers in app/config/app.php.

### Other frameworks/raw PHP

[](#other-frameworksraw-php)

You will need to set up a shared instance of `anlutro\Form\Builder`, and this should be injected into all Form instances.

In order to get input from a Form instance, you should `setRequest` on the Builder instance. The request should be an instance of `Symfony\Component\HttpFoundation\Request`.

For old input from session, CSRF tokens and validation to work, you need to construct and set a session and/or validation service on the Builder instance. The interfaces are located in the `Adapters` namespace, and once you have an object that implements these you can set them via `setSessionAdapter` and `setValidationAdapter`.

If you have written an adapter for popular libraries, please consider a pull request so it can be added to the package!

Usage
-----

[](#usage)

For simple forms, we'll use the class `anlutro\Form\DefaultForm`. Inject this into your controller...

```
use anlutro\Form\DefaultForm;
public function __construct(DefaultForm $form)
{
	$this->form = $form;
}
```

Or just construct it, if you have an instance of the `Form\Builder` available.

```
$this->form = new DefaultForm($formBuilder);
```

In your controller action you can define the behaviour of the form. All of the following are optional.

```
// The model is where the form gets its data. It can be an existing active-
// record model, entity or an array of dummy data.
$this->form->setModel($myModel);
$this->form->setAction('http://mysite.com/my-route');
$this->form->setMethod('PUT');
```

Pass the form to your view in the controller action that shows the form. In your view - using whatever templating engine you have available:

```
