Laravel Extend Form Class

Laravel extend Form class

To extend/swap a Laravel core class, you can create a Service Provider:

File: app/App/Libraries/Extensions/FormBuilder/FormBuilderServiceProvider.php

<?php namespace App\Libraries\Extensions\FormBuilder;

use Illuminate\Support\ServiceProvider as IlluminateServiceProvider;
use App\Libraries\Extensions\FormBuilder\FormBuilder;

class FormBuilderServiceProvider extends IlluminateServiceProvider {

/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = true;

/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->bindShared('formbuilder', function($app)
{
$form = new FormBuilder($app['html'], $app['url'], $app['session.store']->getToken());

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

/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return array('formbuilder');
}

}

Create a Facade for it:

File: app/App/Libraries/Extensions/FormBuilder/FormBuilderFacade.php

<?php namespace App\Libraries\Extensions\FormBuilder;

use Illuminate\Support\Facades\Facade as IlluminateFacade;

class FormBuilderFacade extends IlluminateFacade {

/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor() { return 'formbuilder'; }

}

This would be your namespaced service class:

File: app/App/Libraries/Extensions/FormBuilder/FormBuilder.php

<?php namespace App\Libraries\Extensions\FormBuilder;

use \Illuminate\Html\FormBuilder as IlluminateFormBuilder;

class FormBuilder extends IlluminateFormBuilder {

public function text($name, $value = null, $options = array())
{
$options = $options + array('id'=>"field-{$name}");

return $this->input('text', $name, $value, $options);
}

}

Open app/config/app.php and your Service Provider to the list

'App\Libraries\Extensions\FormBuilder\FormBuilderServiceProvider',

And replace Laravel's Form alias with yours

    'Form'            => 'App\Libraries\Extensions\FormBuilder\FormBuilderFacade',

To test I created a router like this:

Route::any('test', function() {

return e(Form::text('first_name'));

});

And it gave me this result:

<input id="field-first_name" name="first_name" type="text">

Laravel Extend Form Helper Class

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

How to extend validation form request in Laravel 5.2?

You can extend your CreateUserRequest class like this:

<?php

namespace App\Http\Requests;

use CreateUserRequest;

Class OtherCreateUserRequest extends CreateUserRequest {

public function rules()
{
return parent::rules() + [
'additional' => 'rule',
];
}
}

Custom Form-Request dosen't work as expected- Backpack

If you call setupCreateOperation() at the end of your setupUpdateOperation() then the CreateRequest will override the UpdateRequest. What you can do instead is do that first:

    protected function setupUpdateOperation()
{
$this->setupCreateOperation();
$this->crud->setValidation(EditUserRequest::class);
}



Related Topics



Leave a reply



Submit