Laravel Helper module for creating forms and input fields
Version 0.2 Beta, (*1)
Changelog
- 0.2 beta - Rewritten from scratch, not compatible with previus versions
Installation
- Require with composer
composer require plokko/formbuilder
- Add the provider and facades in the app config file
/config/app.php
'providers' => [
//...
//FormBuilder//
plokko\FormBuilder\FormBuilderProvider::class,
//...
]
//...
'aliases' => [
//...
//FormBuilder//
'FormBuilder'=> plokko\FormBuilder\Facades\FormBuilder::class,
//...
]
//...
Use
- create the FormBuilder instance
First create a new instance of FormBuilder in your view with the make command and assign it to a variable
<?php
$fb=FormBuilder::make(['route'=>route('my.destination.route')]);
The make function accepts the same parameters as laravelcollective/html Form::open, (*2)
- declaring the fields
You can then add fields using the requested field type as method name and field name as value
$fb->text('text_field'); //Input field (type=text)
$fb->email('email_field);//Email input field (type=email)
$fb->textarea('message');//Textarea_
To set the options to the field you can access the field when declared, (*3)
$fb->text('field_required')->required()->addClass('required_field');
or you can retrieve it later by accessing it by name as a parameter or array, (*4)
$fb->field_required->value('My value');
$fb['field_required']->addClass('my-other-class');
If you try to access and undeclared field the field will be automatically declared as a text field, (*5)
$fb->undeclared_field->value(1);
// Equals to: //
$fb->text('undeclared_field')->value(1);
To render the form you should first open and close the form using the openForm() and closeForm() functions, rendering the fields with the render() function between them., (*6)
{!! $fb->openForm() !!}
<!--render the fields/-->
{!! $fb->render() !!}
<!--Add some basic submit buttons-->
<button type=submit>Submit</button>
<button type=reset>Reset</button>
{!! $fb->closeForm() !!}
Customization
You can specify how the form will be rendered by applying a view to the FormBuilder class, (*7)
<?php
$fb=FormBuilder::make(['route'=>route('my.destination.route')])
->view('my.custom.view');
You can specify the view like following where $fields is an array containing all the defined fields, (*8)
@foreach($fields AS $field)
/**@var \plokko\FormBuilder\FormField $field**/?>
<div class="form-group">
{!! $field->label->render()!!}
@if($field->type!='checkbox'&&$field->type!='radio')
<?$field->addClass('form-control');/*Add class except for radio or checkbox fields*/?>
@endif
{!! $field->render() !!}
</div>
@endforeach
The view will be called with the render function., (*9)
Expanding functionalities
You can easly add or replace field types by changing the config file;
to do so first publish the config, (*10)
php artisan vendor:publish
Then edit the /config/app.php file, (*11)
<?php
return [
/** Default view for FormBuilder **/
'view'=>'formbuilder::bootstrap.form.base',
/** Registered FormField providers as type=>classname **/
'fieldProviders'=>[
'select' => plokko\FormBuilder\fields\SelectField::class,
'text' => plokko\FormBuilder\fields\InputField::class,
'email' => plokko\FormBuilder\fields\InputField::class,
'hidden' => plokko\FormBuilder\fields\InputField::class,
'number' => plokko\FormBuilder\fields\InputField::class,
'password' => plokko\FormBuilder\fields\InputField::class,
'file' => plokko\FormBuilder\fields\FileField::class,
'radio' => plokko\FormBuilder\fields\CheckboxField::class,
'checkbox' => plokko\FormBuilder\fields\CheckboxField::class,
'textarea' => plokko\FormBuilder\fields\InputField::class,
'select2' => plokko\FormBuilder\fields\Select2Field::class,
],
];
You can create a new field type by expanding the plokko\FormBuilder\fields\FormField class like so:, (*12)
<?php
namespace mynamespace;
use plokko\FormBuilder\fields\FormField;
class MyCustomField extends FormField
{
//...
function myFunction()
{
//...
return $this;
}
//...
}
and then adding it in the config file like so, (*13)
<?php
return [
/** Default view for FormBuilder **/
'view'=>'my.default.form.view',
/** Registered FormField providers as type=>classname **/
'fieldProviders'=>[
//...
'myfield'=>mynamespace/MyCustomField::class,
'myfield2'=>mynamespace/MyCustomField::class,
//...
],
];
You can then add the field using the declared name:, (*14)
//...
$fb->myfield('field1')->myFunction();
$fb->myfield2('field2')->myFunction()->required()->value(2);