Form class is the basis of the package. It contains all logic that is handled from instantiation to rendering.

By default, it should contain buildForm method that will be called on form instantiation through FormBuilder.

Here is simple example of a LoginForm:

<?php namespace App\Forms;

use Kris\LaravelFormBuilder\Form;

class LoginForm extends Form
{
    public function buildForm()
    {
        $this
            ->add('username', 'text')
            ->add('password', 'password')
            ->add('remember_me', 'checkbox');

        // This data is passed as 3rd parameter to `create` method
        // in the controller
        if ($this->getData('is_admin') === true) {
            $this->add('roles', 'choice', [
                'choices' => ['admin' => 'Admin', 'manager' => 'Manager']
            ]);
        }
    }
}

Instantiating this form is done with FormBuilder class or Facade that is provided:

<?php namespace App\Http\Controllers;

use Kris\LaravelFormBuilder\FormBuilderTrait;

class AuthController extends Controller
{
    use FormBuilderTrait;

    public function login()
    {
        // Note the trait used. FormBuilder class can be injected if wanted.
        $form = $this->form('App\Forms\LoginForm', [
            'method' => 'POST',
            'route' => action('AuthController@postLogin')
        ], ['is_admin' => true]);
    }

    public function postLogin()
    {
        // Code for logging in user...
    }
}

FormBuilder::create accepts 3 parameters:

No. Parameter Type
1 Form class path String (Required)
2 Form options Array (Optional)
3 Form data Array (Optional)

Form class path

This is the full class name of the Form class that we want to create.

Namespace can be skipped if configuration has default_namespace option set.

For more info check Package configuration

Form options

These are the options that will be used in the form.

It can contain these items:

<?php
$model = User::find(1);
$formOptions = [
    'method' => 'POST',
    'url' => action('AuthController@postLogin'),
    'class' => 'form-horizontal',
    'language_name' => '',                          // translation prefix used to translate fields with nested translations
    'template' => 'user.form'                       // Template used for the main form layout
    'model' => $model,                              // Not passed to view, just used in form class
    'name' => 'users',                              // Not passed to view, just used in form class
    'data' => ['some_dummy_data' => 'some text'],   // Not passed to view, just used in form class
];

First 3 items are passed to view to Form::open() method, which just set’s the action, method, and some additional attributes like css class.

language_name

This option is used to allow setting up translations per form. For example, if you have ContactForm, you can do this in your translation:

Create file resources/lang/en/contact_form.php

<?php
// resources/lang/en/contact_form.php

return [
    'subject' => 'Enter subject',
    'message' => 'What is your message?'
];

And when you create form just pass it in:

<?php
// ...
FormBuilder::create(ContactForm::class, [
    'language_name' => 'contact_form'
]);
// ...

Or if you want to use same file for all forms, it can be something like this:

<?php
// resources/lang/en/translations.php

return [
    'contact_form' => [
        'subject' => 'Enter subject',
        'message' => 'What is your message?'
    ]
];

And then add language_name like this:

<?php
// ...
FormBuilder::create(ContactForm::class, [
    'language_name' => 'translations.contact_form'
]);
// ...

More information on Issue #206.

model

model is Eloquent model that will be used to bind values to form.

It will be done automatically!

name

name is used for creating Named form.

template

template is used for overriding default template that is used to print main form layout. Make sure it has the similar structure as default one.

data

data is just another way to pass some static data that can be used in the form.

Note: Do not confuse this with model. Data is just passed to the Form class instance to be used for anything inside the class.