iyoworks / form-builder
一个面向对象表单构建器,它从表单创建中提取表单展示。基于 Flyn San 的表单构建器。
v1.0.4
2015-01-14 23:21 UTC
Requires
- php: >=5.3.0
- illuminate/support: ~4
- iyoworks/html: ~1
This package is not auto-updated.
Last update: 2024-09-28 16:15:23 UTC
README
简单直观的表单构建器
基于 https://github.com/Flynsarmy/laravel-form-builder
安装
在 composer.json 中添加此包并运行 composer update(或直接运行 composer require iyoworks/form-builder:1.0.*)
"iyoworks/form-builder": "1.0.*"
更新 composer 后,将 ServiceProvider 添加到 app/config/app.php 文件中的 providers 数组中
'Iyoworks\FormBuilder\FormBuilderServiceProvider',
并且可选地将 Facade 添加到同一文件中的 aliases 数组中。这将允许全局回调(稍后会有更多介绍)。
'FormBuilder' => 'Iyoworks\FormBuilder\Facades\FormBuilder',
使用
添加/编辑/删除字段
创建表单,添加字段,渲染。
$form = FormBuilder::form(); $form->action('UserController@create') //$form->add[FieldType]([field_slug] [, Field Label]); $form->addText('first_name', 'First Name'); $form->addSelect('gender')->options(['male'=>'Male', 'female'=>'Female', 'none'=>'Not Telling']); $form->render();
需要编辑或删除字段吗?
// Set field with id 'gender' to have 3 options instead of 2. $form->getField('gender')->options(['m'=>'Male', 'f'=>'Female', 'n'=>'Not Telling']); // Remove the gender field $form->remove('gender');
精确添加字段到您想要的位置
// Add last name after first name $form->addAfter('first_name', 'last_name', 'text'); $form->addTextAfterFirstName('last_name'); $form->addBefore('last_name', 'first_name', 'text'); $form->addTextAfterLastName('first_name');
闭包也得到了支持
use Iyoworks\FormBuilder\Form; use Iyoworks\FormBuilder\Field; // Closure support for FormBuilder $form = FormBuilder::form(function(Form $form) { $form->url('users/create'); $form->addText('first_name'); $form->addSelect('gender'->options(['M'=>'Male', 'F'=>'Female']); })->html();
echo $form->open(), $form->render(), $form->close(); # the same as echo $form->html();
字段设置
您可以将字段添加到行中
$form->addRow(function($form){ $form->addText('first_name', 'First Name') ->label('First Name') ->description('Enter your first name') ->columns(12); $form->addText('last_name', 'Last Name') ->label('First Name') ->description('Enter your last name') ->columns(12); }); $form->addEmail('email', 'Email Address') $form->addSubmit('Submit')->addClass('btn btn-block btn-primary');
回调
回调可用于渲染您的表单,使其看起来完全符合您的要求。
支持的回调包括
beforeForm(Form $form) afterForm(Form $form) beforeField(Form $form, Field $field) afterField(Form $form, Field $field)
它们可以按表单基础使用
// Per-form Callbacks $form->beforeField(function(Form $form, Field $field) { // Use field settings to display your form nicely return '<label>' . $field->label . '</label>'; });
或者使用可选的 Facade,在全局基础上使用
// Global form callbacks FormBuilder::bind('beforeField', function(Form $form, Field $field) { return '<div class="form-group"><label>'.$field->label.'</label>'; }) ->bind('afterField', function(Form $form, Field $field) { $output = ''; if ( $field->description ) $output .= '<p class="help-block">' . $field->description . '</p>'; return $output . '</div>'; }); $form = FormBuilder::form(function(Form $form) { $form->route('user.create'); $form->addText('first_name', 'First Name'); $form->addText('last_name')->label('Last Name'); }); echo $form->model($model)->html();
许可证
Laravel Form Builder 是开源软件,根据 MIT 许可证 许可。