nickwest / form-maker
直接从 Eloquent 模型和集合创建兼容 Bulma 的表单和表格
v5.5.1
2018-02-23 21:48 UTC
Requires
- php: >=7.0
- illuminate/database: 5.5.*
- illuminate/support: 5.5.*
- illuminate/validation: 5.5.*
- maatwebsite/excel: ^2.1
- nesbot/carbon: ^1.21
Requires (Dev)
- fzaninotto/faker: ~1.4
- phpunit/phpunit: ~6.0
README
Laravel FormMaker 是由 Nick West 创建并维护的。它旨在最小化创建支持 Eloquent 模型 CRUD 功能的简单 HTML 表单所需的时间和努力。
Laravel FormMaker 包含一个可用于基于底层表结构的 Eloquent 模型自动生成表单视图的特质。通过这种方式创建的表单可以完全自定义。包含基本的验证,并且可以轻松扩展。
截至版本 5.5,Laravel FormMaker 可以轻松地进行主题化,以支持各种前端 css/js 框架,并包括必要的 blade 模板,以支持开箱即用的 Bulma 框架。
安装
Laravel FormMaker 需要 PHP 7+。这个特定版本支持 Laravel 5.5。它可能在旧版本上也能工作,但未在 < 5.5 的版本上测试。
要获取最新版本,您只需要通过 Composer 需求该包。
Composer require nickwest/form-maker
FormMaker 支持以下 Laravel 功能:Laravel 自动发现。在 Laravel 5.5+ 中可能不需要,但为了手动添加该包,应在您的 config/app.php 中添加以下提供者。
Nickwest\FormMaker\FormMakerServiceProvider::class,
Nickwest\FormMaker\bulma\FormMakerBulmaThemeServiceProvider::class
用法
向 Eloquent 模型添加功能
class Sample extends Model{
use FormTrait;
protected $table = 'sample';
// Options for setting up the form:
// OPTION 1: Create a method in the model that will be called by the controller
// The method name doesn't matter, it's up to you.
public function prepareForm()
{
// Set a default Form Field label postfix
$this->label_postfix = ':';
// This comes from the FormTrait. It generates form field data by looking at
// the model's table columns
$this->generateFormData();
// Use a custom theme for the generated markup
$this->Form()->setTheme(new \Nickwest\FormMaker\bulma\Theme());
// By Default all fields will be displayed
// We can set specific fields to be displayed by the form.
// The order here dictates the order the fields will show on the form
// $this->Form()->setDisplayFields( array(
// 'email',
// 'attendance_date',
// 'placard_color',
// 'photograph',
// 'phone_number',
// 'link_to_website',
// 'meal_choice',
// 'fruits_they_like',
// 'plus_one',
// ));
// Or we can remove only selected display fields and the rest will
// display in the same order they appear in the underlying table.
$this->Form()->removeDisplayFields([
'id',
'created_at',
'updated_at',
]);
// Set field types for fields that aren't just default types
$this->Form()->setTypes([
'email' => 'email',
'attendance_date' => 'date',
'placard_color' => 'color',
'photograph' => 'file',
'phone_number' => 'tel',
'link_to_website' => 'url',
'meal_choice' => 'select',
'fruits_they_like' => 'checkbox',
'plus_one' => 'radio', // Yes/No enum in database
]);
// Fields set up as Enums in the database will automatically have enum
// options available, but enums are not requires for multiple choice fields
$this->Form()->fruits_they_like->setOptions([
'banana' => 'Banana',
'strawberry' => 'Strawberry',
'apple' => 'Apple',
'mango' => 'Mango',
'passion Fruit' => 'Passion Fruit',
'orange' => 'Orange',
'kiwi' => 'Kiwi',
'pear' => 'Pear',
'pineapple' => 'Pineapple'
]);
$this->Form()->meal_choice->setOptions([
'cheap_meal' => 'Chicken',
'average_meal' => 'Beef',
'expensive_meal' => 'Lobster',
]);
// Make it a multi-select field
$this->Form()->meal_choice->multiple = true;
// Set examples to show up below the field
$this->Form()->setExamples([
'email' => 'ex: example@example.com',
]);
// Set a pattern (used to add pattern="" attribute to field)
$this->Form()->phone_number->attributes->pattern = '\\d{3}[\\-]\\d{3}[\\-]\\d{4}';
// Set a placeholder (similar to example, but uses placeholder attribute)
$this->Form()->phone_number->attributes->placeholder = '123-456-7890';
// Set some validation (This uses Laravel's Validator facade:
// https://laravel.net.cn/docs/5.5/validation
$this->validation_rules = [
'email' => 'email|max:256',
'fruits_they_like' => 'in:'.implode(',', array_keys($this->Form()->fruits_they_like->options)),
'meal_choice' => 'in:'.implode(',', array_keys($this->Form()->meal_choice->options)),
// you can add an validation rules you want here.
];
// Include a delete button?
$this->Form()->allow_delete = false;
}
// OPTION 2: Do all of this in the controller. You would choose this
// method if your form is likely to look different at different routes.
// If it will always be the same having a method to call is less repetition.
// You can also modify the form after calling the method above, so a mix of
// the two works as well.
// Option 3: Do this in the constructor. I would advise against doing this
// in the constructor since generateFormData() runs a query to the get the
// table structure.
}
控制器
// The GET route for the form page
public function getForm(Request $request, int $id=0)
{
$blade_data = [
'page_title' => 'Sample Form',
'whatever_else' => 'Any data your base view need can be passed through',
];
// Find the Model if an ID is passed in, otherwise start a new one.
$Sample = Sample::findOrNew($id);
// prepare the form as described in the Model method above
$Sample->prepareForm();
// This is a FormMaker trait method. It populates form values based on
// data in the current Model and by defaults and other settings
// configured in the step before.
$Sample->setAllFormValues();
// If there's old request data, set it.
if($request->old())
{
$Sample->setPostValues($request->old());
}
// Make a view that extends 'base_layout'
return $Sample->getFormView($blade_data, 'base_layout', 'content');
}
其他
FormMaker 不需要使用特质。Form 和 Field 类也可以独立使用。代替自动生成表单字段,需要手动声明。