Here are some useful methods and properties on form class:

Methods

Properties

add

This method is used to add a field to the form. It is used in Form class buildForm method, but it can be also used after form instantiation.

It accepts 3 arguments:

Description Type Required Default
Field name String true -
Field type String false text
Field options Array false []
<?php
$this
    ->add('first_name', 'text', [
            'label' => 'First name'
    ]);

addBefore

This method is used to add a field before some other field. It’s mostly useful after the form instantiation, if You want to add new field at specific location.

It accepts 4 arguments:

Description Type Required Default
Before field name String true -
Field name String true -
Field type String false text
Field options Array false []
<?php
$form = FormBuilder::plain()
    ->add('username', 'text')
    ->add('password', 'password')
    ->add('submit', 'submit');

$form->addBefore('password', 'full_name', 'text');

addAfter

Similar to addBefore, this method is used to add a field before some other field.

It’s mostly useful after the form instantiation, if You want to add new field at specific location.

It accepts 4 arguments:

Description Type Required Default
Before field name String true -
Field name String true -
Field type String false text
Field options Array false []
<?php
$form = FormBuilder::plain()
    ->add('username', 'text')
    ->add('password', 'password')
    ->add('submit', 'submit');

$form->addAfter('password', 'address', 'text');

compose

This method is used when you want to add fields from one form to another form.

It accepts 3 arguments:

Description Type Required Default    
Form which fields will be added Form ChildFormType String true -
$options String false []    
$modify String false false    
<?php namespace App\Forms;

use Kris\LaravelFormBuilder\Form;

class AddressForm extends Form
{
    public function buildForm()
    {
        $this->add('street', 'text')
            ->add('city', 'text');
    }
}

class UserForm extends Form
{
    public function buildForm()
    {
        $this->add('name', 'text')
            ->add('email', 'email')
            ->compose('App\Forms\AddressForm');
    }
}

class CompanyForm extends Form
{
    public function buildForm()
    {
        $this->add('name', 'text')
            ->add('phone', 'tel')
            ->compose('App\Forms\AddressForm');
    }
}

modify

In situations when there is need for modifying existing field on the form class, we can use this method.

It accepts 4 arguments:

Description Type Required Default
Field name to modify String true -
New field type for the field String false text
Field options to add/overwrite Array false []
Should options be totally overwritten Boolean false false
<?php
$form = FormBuilder::plain()
    ->add('username', 'text')
    ->add('password')
    ->add('submit', 'submit');

$form->modify('username', 'text', [
    'attr' => ['disabled' => true]
]);

remove

Since forms can be reused, sometimes certain fields are not needed in different places. Using this method we can remove those fields.

It accepts 1 argument:

Description Type Required Default
Field name to remove String true -
<?php
$form = FormBuilder::plain()
    ->add('username', 'text')
    ->add('email', 'email')
    ->add('password')
    ->add('submit', 'submit');

$form->remove('email');

setModel (DEPRECATED)

Note: This method is deprecated and will be removed in version 2. Pass model through options when creating a form. More info in Form options section

After form instantiation, we can set the model for the form, that will be used to bind values for the fields in the form.

<?php
$model = User::find(1);

$form = FormBuilder::plain()
    ->add('username', 'text')
    ->add('email', 'email')
    ->add('password')
    ->setModel($model);

Another way to pass the model is through formOptions parameter:

<?php
$model = User::find(1);

$form = FormBuilder::plain(['model' => $model])
    ->add('username', 'text')
    ->add('email', 'email')
    ->add('password');

getModel

Get the model that was passed to the form class. Can be used in buildForm for referencing some relationship or data from the model, or after the instantiation.

<?php namespace App\Forms;

use Kris\LaravelFormBuilder\Form;

class CommentForm extends Form
{
    public function buildForm()
    {
        $this->add('body', 'textarea');

        if ($this->getModel()->owner === \Auth::id()) {
            $this->add('publish', 'checkbox');
        }
    }
}

getRequest

Get the current request.

returns Illuminiate\Http\Request instance.

getFields

Get all fields of the single Form class.

returns Array of fields.

getField

Get single field from the Form class.

It accepts 1 argument:

Description Type Required Default
Name of the field String true -
<?php
$model = User::find(1);

$form = FormBuilder::plain(['model' => $model])
    ->add('username', 'text')
    ->add('email', 'email')
    ->add('password');

$usernameField = $form->get('username');

Note: Another way to get single field from the Form class is getting it by property. Form class has magic methods that handle finding fields.

Using above form:

    $usernameField = $form->username;

disableFields

Disable all fields in a form, by adding disabled attribute to the fields. Useful when you only want to use form as read only.

$form = FormBuilder::create('App\Forms\SearchForm');

$form->disableFields();

enableFields

Enable all fields in a form, by removing disabled attribute to the fields.

$form = FormBuilder::create('App\Forms\SearchForm');

$form->enableFields();

formOptions

These options are passed to the view into the Form::open method.

If Form class will always have same method or url during it’s usage, it’s good pratice to set this property on the form class to avooid repeating.

Type: Array Default: ['method' => 'GET', 'url' => null]

<?php namespace App\Forms;

use Kris\LaravelFormBuilder\Form;

class SearchForm extends Form
{
    protected $formOptions = [
        'method' => 'GET',
        'url' => '/search'
    ];

    public function buildForm()
    {
        $this->add('term', 'text');
    }
}

And then it’s enough to call this:

$form = FormBuilder::create('App\Forms\SearchForm');

showFieldErrors

This property is used to determine if we want to show validation errors in the form under each field.

Type: Boolean

Default: true

clientValidationEnabled

Since version 1.6.30 form automatically generates html5 validation properties in html. Setting this to false will remove those properties.

Type: Boolean

Default: true