Using BootForms with Laravel 5.4

Adam Wathan’s BootForms makes it painless to work with Bootstrap forms in your Laravel Blade templates. If you’ve been using BootForms and just upgraded to Laravel 5.4, you might have run into some issues with your views.

There’s an open pull request which fixes this issue, so hopefully this post will be irrelevant very soon. Until then, however, the following will help you get BootForms working in your Laravel 5.4 application.

Extend BootFormsServiceProvider

The issue is in \AdamWathan\BootForms\BootFormsServiceProvider so we’ll create a new class that extends it and apply the fix there. Laravel 5.4 no longer uses Symfony’s SessionInterface for its session handlers and replaces it with a new \Illuminate\Contracts\Session\Session interface. This change means that any calls to the getToken() method on sessions must be replaced with token(). The registerFormBuilder() method in BootFormsServiceProvider calls getToken() so we need to change that.

First, create a new service provider class called BootFormsServiceProvider. The following Artisan command will scaffold this for you:

$ php artisan make:provider BootFormsServiceProvider

Open the newly created app\Providers\BootFormsServiceProvider.php in your project and change its contents to the following:

<?php
namespace App\Providers;
use AdamWathan\Form\FormBuilder;
use AdamWathan\BootForms\BootFormsServiceProvider as OriginalProvider;
class BootFormsServiceProvider extends OriginalProvider
{
/**
* Override the registerFormBuilder method to use token()
* instead of getToken() on new Session interface in Laravel 5.4
*
* @return FormBuilder
*/
protected function registerFormBuilder()
{
$this->app->singleton('adamwathan.form', function ($app) {
$formBuilder = new FormBuilder;
$formBuilder->setErrorStore($app['adamwathan.form.errorstore']);
$formBuilder->setOldInputProvider($app['adamwathan.form.oldinput']);
$formBuilder->setToken($app['session.store']->token());
return $formBuilder;
});
}
}

If you are using a custom namespace for your application, be sure to change it above.

Register the extended provider with your application

Now, you just need to register this new provider in config\app.php. Comment out the line loading Adam’s provider (you’ll probably want to swap this back in when Laravel 5.4 support ships in BootForms itself) and load in your new provider class.

// AdamWathan\BootForms\BootFormsServiceProvider::class,
App\Providers\BootFormsServiceProvider::class,

Once this change has been made, your BootForms-powered views will work again.

Show your support

Clapping shows how much you appreciated Joe Lennon’s story.