@stonebitsrl It's really easy! Here is an example
First you need to create a new ServiceProvider. Let's call it HtmlServiceProvider.php
So in the app\Services\Html directory we create this file
<?php namespace App\Services\Html;
class HtmlServiceProvider extends \Collective\Html\HtmlServiceProvider {
/**
* Register the form builder instance.
*/
protected function registerFormBuilder()
{
$this->app->bindShared('form', function($app)
{
$form = new FormBuilder($app['html'], $app['url'], $app['session.store']->getToken());
return $form->setSessionStore($app['session.store']);
});
}
}
Note: I use the laravelcollective/html package for the formbuilder. This package is not maintained by Taylor anymore, but it is the same as illuminate/html
Now we need to create a FormBuilder class. I did this in the same directory, so App\Services\Html\FormBuilder.php
<?php namespace App\Services\Html;
class FormBuilder extends \Collective\Html\FormBuilder {
public function textfield($name, $label, $errors, $labelOptions = array(), $inputOptions = array())
{
$labelOptions['class'] = 'form-label';
$inputOptions['class'] = 'form-control';
$inputOptions['placeholder'] = $label;
return sprintf(
'<div class="form-group">%s<div%s>%s%s</div></div><!-- end form-group -->',
parent::label($name, $label, $labelOptions),
$errors->has($name) ? ' class="error-control"' : '',
parent::text($name, null, $inputOptions),
$errors->has($name) ? '<span class="error"><label class="error" for="' . $name . '">' . $errors->first($name) . '</label></span>' : ''
);
}
public function submit($value = null, $options = [])
{
$options['class'] = 'btn btn-cons btn-success' . (isset($options['class']) ? ' ' . $options['class'] : '');
return parent::submit($value, $options);
}
}
As you can see I extend the FormBuilder class from the laravelcollective\html package. If you use the Illuminate\html package then you need to extend that class of course.
Now you can override any function you want. I give a little example with the submit button and the textfield I use for my current project.
I will give you an example on how you can call them in your view, but first we need to register the FormBuilder. To do that we need to update the config/app.php file
// in your providers array remove the service provider from the package and replace it with the new service provider
'providers' => [
// .... Other service providers
'App\Providers\DatabaseServiceProvider',
'App\Providers\RouteServiceProvider',
'App\Services\Html\HtmlServiceProvider' // THe service provider we just created
];
You don't have to update the aliases array but it should look like this
'aliases' => [
// ... Other aliases
'Form' => 'Collective\Html\FormFacade',
'Html' => 'Collective\Html\HtmlFacade'
];
And I call both examples like this in my views
{!! Form::textfield('client_name', 'Client Name', $errors) !!}
{!! Form::submit('Submit my aweomse Form') !!}
You can override each function like you wish! If you don't override the function it will use the default function from the package ;)
Happy coding :D