Download the PHP package iainheng/form without Composer
On this page you can find all versions of the php package iainheng/form. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download iainheng/form
More information about iainheng/form
Files in iainheng/form
Package form
Short Description A basic framework agnostic form building package with a few extra niceties like remembering old input and retrieving error messages.
License MIT
Informations about the package form
Important: This package is not actively maintained. For bug fixes and new features, please fork.
Form
Boring name for a boring package. Builds form HTML with a fluent-ish, hopefully intuitive syntax.
- Installation
- Basic Usage
- Remembering Old Input
- Error Messages
- CSRF Protection
- Data Binding
Installation
You can install this package via Composer by running this command in your terminal in the root of your project:
Laravel
This package works great as a replacement Form Builder that was removed in Laravel 5. The API is different but all of the features are there.
If you are using Laravel 4 or 5, you can register the FormServiceProvider to automatically gain access to the Old Input and Error Message functionality.
To do so, just update the providers
array in your config/app.php
:
You can also choose to use the Facade by adding an alias in config/app.php
:
Note that in Laravel 4, there is already a Form facade for the built-in Form Builder. If you want to use both, use a different alias. If you'd just like to use this one, remove the Form alias that points to the Illuminate component.
Basic Usage
- Getting Started
- Opening a Form
- Text and Password Fields
- Textareas
- Checkboxes and Radio Buttons
- Selects
- Buttons
- Hidden Inputs
- Labels
- Setting Attributes
Getting Started
First, instantiate a FormBuilder...
Next, use the FormBuilder to build an element. For example:
- All elements support method chaining, so you can add as many options to an element as you need.
- All elements implement
__toString()
so there is no need to manually render.
Opening a Form
Text and Password Fields
Text and password fields share the same interface.
Other available methods:
placeholder($string)
optional()
defaultValue($string)
disable()
enable()
Textareas
Textareas share the same interface as regular text fields, with a couple of extra useful methods.
Checkboxes and Radio Buttons
Selects
Buttons
Hidden Inputs
Labels
Basic Label
Wrapping another element
Setting Attributes
Remembering Old Input
The FormBuilder can remember old input and prepopulate your form fields if you redirect back to the form because of a validation error.
To make this work, you must create a class that implements the OldInputInterface
and pass it to the FormBuilder:
Now, your form elements will automatically populate with the user's old input data if it is available.
This works well with the defaultValue()
methods, allowing you to set a default value that will be overridden by old input if the user has already submitted the form.
This package ships with a Laravel implementation out of the box, called
IlluminateOldInput
.
Error Messages
FormBuilder also allows you to easily retrieve error messages for your form elements. To do so, just implement the ErrorStoreInterface
and pass it to the FormBuilder:
This package ships with a Laravel implementation out of the box, called
IlluminateErrorStore
.
You can also supply a format
parameter to getError()
to cleanup your markup. Instead of doing this:
...you can simply do this, which will display the formatted message if it exists, or nothing otherwise.
CSRF Protection
Assuming you set a CSRF token when instantiating the Formbuilder (or you are using Laravel), add a CSRF token to your form easily like so:
Data Binding
Sometimes you might have a form where all of the fields match properties on some sort of object or array in your system, and you want the user to be able to edit that data. Data binding makes this really easy by allowing you to bind an object or array to your form that will be used to automatically provide all of the default values for your fields.
This will work out of the box with Laravel's Eloquent models.
When using data binding, old input will still take priority over any of your bound values, so you can still easily redirect the user back to the form with any validation errors without losing any of the data they entered.
Note: Be sure to
bind
before creating any other form elements.