Basics

Forms can be easily generated through provided command line generator.

For example, to generate form class with name LoginForm in the app/Http/Forms path, you just need to run this:

php artisan make:form Http/Forms/LoginForm

And it will generate file with name LoginForm.php in path ‘app/Http/Forms’ - Full path app/Http/Forms/LoginForm.php, and it will have this content:

<?php namespace App\Http\Forms;

use Kris\LaravelFormBuilder\Form;

class LoginForm extends Form
{
    public function buildForm()
    {
        // Add fields here...
    }
}

Fields

When creating form class through CLI it is possible to pass array of fields that will be automatically added to the class. Let’s take example from above. Most of the time, login form contains username and password fields. To generate form class with those fields this command needs to be run:

php artisan form:make Http/Forms/LoginForm --fields="username:text, password:password"

And it will generate this:

<?php namespace App\Http\Forms;

use Kris\LaravelFormBuilder\Form;

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

--fields option needs to contain comma separated list of name:fieldtype type of text.

Path

When --path option is not provided in the command, path where form class needs to be put is determined automatically from the namespace. When you create form Http/Forms/LoginForm, it will automatically prepend app/ to it, and add .php extension.

In some situations, for example, if you are using this package as a depencency for another package, you could provide path manually. Here’s how that would work:

php artisan make:form MyLoginForm --path="src/Auth/LoginForm"

Will generate form class with in path /var/www/laravel-project/src/Auth/LoginForm/MyLoginForm.php:

<?php namespace src\Auth\LoginForm;

use Kris\LaravelFormBuilder\Form;

class MyLoginForm extends Form
{
    public function buildForm()
    {
        // Add fields here...
    }
}

NOTE: Base path is determined from Laravel’s basePath() method from Application class, which is in example above /var/www/laravel-project/.

NOTE: Namespace is guessed from the path. In these situations, it’s best to use path option together with Namespace option.

Namespace

Namespace for the form class is most of the time guessed from Form class name provided to the CLI command. So when you create form Http/Forms/MyForm, it will prepend namespace which was set with app:name command, which is by default App and this results in App\Http\Forms.

In situations when the Path option IS provided, namespace is guessed from the path (Check example in Path section).

To override it, you can also provide --namespace option to the command. So to fix example above, we need to run this command:

NOTE Value for this option will be used as provided, so make sure to use backslashes to generate proper namespace.

php artisan make:form MyLoginForm --path="src/Auth/LoginForm" --namespace="App\Auth\LoginForm"

And than we get class in path /var/www/laravel-project/src/Auth/LoginForm/MyLoginForm.php with this content:

<?php namespace App\Auth\LoginForm;

use Kris\LaravelFormBuilder\Form;

class MyLoginForm extends Form
{
    public function buildForm()
    {
        // Add fields here...
    }
}

NOTE: Make sure to wrap --namespace value in single(‘) or double(“) quotes, so the backslash(\) is not lost. If you want to skip using any of those, make sure you escape it like this App\\Auth\\LoginForm.