Ray
Ray

Reputation: 3060

Laravel Extend Form Helper Class

I'm using Laravel 5.1 and have installed the Collective's HTML and FORM helper.

I'm now trying to extend this to handle a couple of custom form elements but am struggling to get it working. My current error is:

 Argument 1 passed to Collective\Html\FormBuilder::__construct() must be an instance of Collective\Html\HtmlBuilder, instance of Illuminate\Foundation\Application given

My class extends FormBuilder as follows:

namespace Helpers;

use \Collective\Html\FormBuilder;

class MyFormHelper extends FormBuilder
{


    public function arrayHidden($name, $value = null, $options = array())
    {
        ddd($value);
        return $this->input('hidden', $name, $value, $options);
    }
}

so at the moment I'm simply expecting to evaluate $value

My knowledge of extending classes is limited but assumed this would work

The FormBuilder class from the package is:

<?php namespace Collective\Html;

use DateTime;
use Illuminate\Routing\UrlGenerator;
use Illuminate\Session\Store as Session;
use Illuminate\Support\Traits\Macroable;

class FormBuilder {

use Macroable;

/**
 * The HTML builder instance.
 *
 * @var \Collective\Html\HtmlBuilder
 */
protected $html;

/**
 * The URL generator instance.
 *
 * @var \Illuminate\Routing\UrlGenerator  $url
 */
protected $url;

/**
 * The CSRF token used by the form builder.
 *
 * @var string
 */
protected $csrfToken;

/**
 * The session store implementation.
 *
 * @var \Illuminate\Session\Store
 */
protected $session;

/**
 * The current model instance for the form.
 *
 * @var mixed
 */
protected $model;

/**
 * An array of label names we've created.
 *
 * @var array
 */
protected $labels = array();

/**
 * The reserved form open attributes.
 *
 * @var array
 */
protected $reserved = array('method', 'url', 'route', 'action', 'files');

/**
 * The form methods that should be spoofed, in uppercase.
 *
 * @var array
 */
protected $spoofedMethods = array('DELETE', 'PATCH', 'PUT');

/**
 * The types of inputs to not fill values on by default.
 *
 * @var array
 */
protected $skipValueTypes = array('file', 'password', 'checkbox', 'radio');

/**
 * Create a new form builder instance.
 *
 * @param  \Illuminate\Routing\UrlGenerator  $url
 * @param  \Collective\Html\HtmlBuilder  $html
 * @param  string  $csrfToken
 * @return void
 */
public function __construct(HtmlBuilder $html, UrlGenerator $url, $csrfToken)
{
    $this->url = $url;
    $this->html = $html;
    $this->csrfToken = $csrfToken;
}

So - how can I correctly extend FormBuilder to add custom functions?

THanks

Upvotes: 2

Views: 1444

Answers (1)

Wader
Wader

Reputation: 9883

So to extend the FormBuilder there are 2 steps you have to take. You've done the first one which is create your own class that physically extends the existing FormBuilder class from laravel.

The 2nd step you must take is to register that new class with laravel's container. To do this you can setup a new service provider as below.

class FormBuilderServiceProvider extends Illuminate\Support\ServiceProvider {

    // Wait until all other service providers have been run before ours
    protected $defer = true;

    public function register()
    {
        $this->app->bindShared('formbuilder', function($app)
        {
            $form = new \Your\FormBuilder($app['html'], $app['url'], $app['session.store']->getToken());

            return $form->setSessionStore($app['session.store']);
        });
    }

    public function provides()
    {
        return ['formbuilder'];
    }

}

You now just need to update your config/app.php by removing the existing service provider and replacing it with your new one.

You can read more on this on the Service Container and Service Providers pages of the laravel documentation.

http://laravel.com/docs/5.1/container http://laravel.com/docs/5.1/providers

Upvotes: 3

Related Questions