-
Notifications
You must be signed in to change notification settings - Fork 0
Core update
laravel-form-maker has been built from the ground up to be a modular api that you can easily modify to fit your unique needs. Most of the package core services and models are bind to the service container and can be swap with your own implementation with ease.
In config/form-maker.php you will see a list of all the services and models that you can customize. All those classes implements a contract that your new service has to respect in order to be accepted. Most of the time, you just need to extends the existing class that you want to tweak and overwrite the methods or attributes listed on it. After you will need to update the path in the config file to your new implementation file. You will see some examples bellow.
All models has a different whitelist of html attributes they support. If for any reason, you need to add one to the list, you can do has follow :
- Extends the existing checkbox class
use Belvedere\FormMaker\Models\Nodes\Inputs\Checkbox\Checkboxer;
class Checkbox extends Checkboxer
{
//
}
- Add your custom attribute in the list.
use Belvedere\FormMaker\Models\Nodes\Inputs\Checkbox\Checkboxer;
class Checkbox extends Checkboxer
{
/**
* Checkbox constructor.
*
* @param array $attributes
*/
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
$this->addAvailableAttributes([
'pattern',
]);
}
}
- Update the checkbox path to your new implementation in the config file.
'nodes' => [
'inputs' => [
'checkbox' => \App\Checkbox::class,
...
],
]
- Voilà! Checkbox now accepts pattern attribute.
If you need an html element that is not supported by the api for your project, no worries, you can easily add an entire new one to the list and request it just has you would do for the ones that comes by default.
Here are the steps you need to take to create your own custom input node.
- Create a new class and extends the parent class of an input model. Make sure you pick a type that is not already present since all your inputs of that type will be identified by that key value. In this example, the type is custom
use Belvedere\FormMaker\Models\Nodes\Inputs\Input;
use Belvedere\FormMaker\Scopes\NodeScope;
class Custom extends Input
{
/**
* Apply the type scope.
*
* @return void
*/
protected static function boot()
{
parent::boot();
static::addGlobalScope(new NodeScope('custom'));
}
/**
* Custom constructor.
*
* @param array $attributes
*/
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
}
}
- Register this class has a service provider. Make sure you also add the alias prefix by the key form-maker. because this is the id laravel-form-maker will use to resolve your provider from the service container. It has to be unique.
use Illuminate\Support\ServiceProvider;
class CustomServiceProvider extends ServiceProvider
{
/**
* Register the service provider.
*
* @return void
*/
public function register(): void
{
$this->app->bind(Custom::class, function ($app) {
return new Custom();
});
$this->app->alias(Custom::class, 'form-maker.custom');
}
}
-
Add you provider to your app providers in config/app.php
-
Also add the path to your new class file in the form-maker config file. In this case, we add it to the inputs list. This is useful for filtering if, for example, later you want to get all your form nodes that are inputs.
'nodes' => [
'inputs' => [
'custom' => \App\Form\Custom::class,
...
]
]
- There you go! You now have a brand new input type that you can customize to fit your project requirements.
$form->add('custom')
->withHtmlAttributes(['required' => 'required'])
->withRules(['required' => 'required', 'max' => 10])
->save();
One thing to keep in mind, because you need to define an alias, you won't be able to use laravel deferred providers like the defaults does. Therefore, your custom providers will be loaded from the filesystem on every request.