FormBuilder
Based on laravel-form-builder (https://github.com/kristijanhusak/laravel-form-builder). I add default no editable form and few complex form fields. I add the validation system directly in the form part.
Install / Use
/learn @Distilleries/FormBuilderREADME
Laravel 5 Form Builder
Based on laravel-form-builder (https://github.com/kristijanhusak/laravel-form-builder). I add default no editable form and few complex form fields. I add the validation system directly in the form part.
Table of contents
- Installation
- Basic usage
- Type of form
- Form
- FormView
- FormValidator 1. Use the validation client side 2. Use the validation server side 3. Check the rules on your controller
- List of fields
- Input
- Choice 1. Select 2. Radio 3. Checkbox 4. Ajax
- Tag
- Upload
- TinyMce
- Textarea
- Button
- Address Picker
- Form
- Controller
- Troubleshooting
##Installation
Add on your composer.json
"require": {
"distilleries/form-builder": "2.*",
}
run composer update.
Add Service provider to config/app.php:
'providers' => [
// ...
'Distilleries\FormBuilder\FormBuilderServiceProvider',
]
And Facade (also in config/app.php)
'aliases' => [
// ...
'FormBuilder' => 'Distilleries\FormBuilder\Facades\FormBuilder',
]
Export the configuration:
php artisan vendor:publish --provider="Distilleries\FormBuilder\FormBuilderServiceProvider"
Export the views (optional):
php artisan vendor:publish --provider="Distilleries\FormBuilder\FormBuilderServiceProvider" --tag="views"
###Basic usage
Creating form classes is easy. With a simple artisan command I can create form:
php artisan make:form Forms/PostForm
you create form class in path app/Forms/PostForm.php that looks like this:
<?php namespace App\Forms;
use Distilleries\FormBuilder\FormValidator;
class PostForm extends FormValidator
{
public static $rules = [];
public static $rules_update = null;
public function buildForm()
{
// Add fields here...
$this->addDefaultActions();
}
}
You can add fields which you want when creating command like this:
php artisan make:form Forms/SongForm --fields="name:text, lyrics:textarea, publish:checkbox"
And that will create form in path app/Forms/SongForm.php with content:
<?php namespace App\Forms;
use Distilleries\FormBuilder\FormValidator;
class SongForm extends FormValidator
{
public static $rules = [];
public static $rules_update = null;
public function buildForm()
{
$this
->add('name', 'text')
->add('lyrics', 'textarea')
->add('publish', 'checkbox');
$this->addDefaultActions();
}
}
##Type of form
###Form
This is the base class from the package https://github.com/kristijanhusak/laravel-form-builder/tree/laravel-4. It use to add the fields and generate the form. Check the readme to know how use the component.
###FormView Extend the class Form to add a render with edit.
####Use the view
To display an not editable form you can use form_view or form_rest_view.
To display a specific field you can use form_widget_view.
{!! form_view($form) !!}
In your form field you can add an option to not display this field on the view noInEditView.
For example in user form I add a choice to allow the password change. I don't want it in the view part.
$this->add('change_password', 'checkbox', [
'default_value' => 1,
'label' => _('Check it if you want change your password'),
'checked' => false,
'noInEditView' => true
]);
Other way, you have a sub form and you don't want display some fields.
You can specify an option call do_not_display_ plus the name of the field.
Example I have a customer form and this form use a sub form user. On the user form I don't want display the role choice:
$this->add('user', 'form', [
'label' => _('User'),
'icon' => 'user',
'class' => \FormBuilder::create('Distilleries\Expendable\Forms\User\UserForm', [
'model' => $this->getUserModel(),
'do_not_display_role_id' => true
])
]);
###FormValidator Extend the FormView and add the system of validation.
public static $rules = [];
public static $rules_update = null;
The both table use the rules of laravel.
If the $rules_update keep in null the $rules is use to validate the form.
####Use the validation client side
By default I use jQuery validation Engine for the javascript validation.
When you add a field you can add an option validation to add the javascript validation.
$this->add('email', 'email',
[
'validation' => 'required,custom[email]',
]);
####Use the validation server side
If you have a form User like this:
<?php namespace Project\Forms;
use Distilleries\FormBuilder\FormValidator;
class UserForm extends FormValidator {
public static $rules = [
'email' => 'required|email|unique:users',
'password' => 'required|min:8',
'status' => 'required|integer',
'role_id' => 'required|integer',
];
public static $rules_update = [
'id' => 'required',
'email' => 'required|email|unique:users,email',
'status' => 'required|integer',
'role_id' => 'required|integer',
];
// ------------------------------------------------------------------------------------------------
public function buildForm()
{
$this
->add($this->model->getKeyName(), 'hidden')
->add('email', 'email',
[
'label' => _('Email'),
'validation' => 'required,custom[email]',
]);
$id = $this->model->getKey();
if (!empty($id))
{
$this->add('change_password', 'checkbox', [
'default_value' => 1,
'label' => _('Check it if you want change your password'),
'checked' => false,
'noInEditView' => true
]);
}
$this->add('password', 'password',
[
'label' => _('Password'),
'attr' => ['id'=>'password'],
'validation' => 'required',
'noInEditView' => true
])
->add('password_match', 'password',
[
'label' => _('Repeat Password'),
'validation' => 'required,equals[password]',
'noInEditView' => true
])
->add('status', 'choice', [
'choices' => StaticLabel::status(),
'empty_value' => _('-'),
'validation' => 'required',
'label' => _('Status')
])
->add('role_id', 'choice', [
'choices' => \Role::getChoice(),
'empty_value' => _('-'),
'validation' => 'required',
'label' => _('Role')
])
->addDefaultActions();
}
protected function getUpdateRules()
{
$key = \Input::get($this->model->getKeyName());
static::$rules_update['email'] = 'required|email|unique:users,email,' . $key;
return parent::getUpdateRules();
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
}
You can see the password field is not require on the update.
I have an other specific rule. I want check if the email address is unique without if the email is use by your-self.
In the FormValidator you have two methods to get the update or general rules (getGeneralRules, getUpdateRules) . You can override them to return the good rules.
That what I do in the UserForm. I override the method getUpdateRules to add th
Related Skills
node-connect
352.0kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
111.1kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
352.0kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
352.0kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。


