ycs77/laravel-form-field-type 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ycs77/laravel-form-field-type

Composer 安装命令:

composer require ycs77/laravel-form-field-type

包简介

Fast set the form fields of the Laravel form builder.

README 文档

README

Latest Version on Packagist Software License Build Status Style CI Build Status Total Downloads

Fast set the form fields of the Laravel form builder.

Install

Laravel form builder must be installed.

Via Composer

composer require ycs77/laravel-form-field-type

Publish config

php artisan vendor:publish --tag=laravel-form-field-type-config

Suggestions can be matched with Laravel form builder BS4.

Usage

First, create one form fields class:

php artisan make:formfields UserFormFields

The commonly used fields can be defined in config/field.php and the FieldType will be loaded automatically.

In this case, the 'phone' fields have been defined in config/field.php, so they can be used directly.

app/FormFields/UserFormFields

<?php

namespace App\FormFields;

use Ycs77\LaravelFormFieldType\FormFields;

class UserFormFields extends FormFields
{
    /**
     * Return form fields array.
     *
     * @return array
     */
    public function fields()
    {
        return [
            'name' => [
                'rules' => 'required|50',
            ],
            'phone',
            'submit',
        ];
    }
}

Second, add FormFields, FormFieldsTrait to controller:

app/Http/Controllers/MyController

<?php

namespace App\Http\Controllers;

use App\FormFields\UserFormFields;
use Illuminate\Http\Request;
use Ycs77\LaravelFormFieldType\Traits\FormFieldsTrait;

class MyController extends Controller 
{
    use FormFieldsTrait;

    protected $formFields;

    public function __construct(UserFormFields $formFields)
    {
        $this->formFields = $formFields;
    }

    public function index()
    {
        $form = $this->renderForm([
            'url'    => '/url',
            'method' => 'POST',
        ]);

        // Response view ...
    }

    public function store(Request $request)
    {
        $data = $this->validateFormData($request);

        // Save model data ...
    }
}

Custom validate message

Add property validateMessage to controller.

protected $validateMessage = [
    'dimensions' => 'The maximum length and width of the image is 4000x4000px.',
];

Custom failed message

Add property failedMessage to controller.

protected $failedMessage = [
    'images' => 'Can only upload up to 5 images.',
];

Custom lang path

Add property langPath to controller.

protected $langPath = 'validation.attributes';

Methods

type

If you enter a field defined by config/field.php, the field will be returned.

Return the complete type of the specified type of data:

Get field:

$array = FieldType::type('age', [
    'type' => 'number',
]);

// [
//     'id'   => 'age',
//     'type' => 'number',
// ]

Or use same:

$array = FieldType::type('age', 'number');

// [
//     'id'   => 'age',
//     'type' => 'number',
// ]

Get exist field type:

$array = FieldType::type('nickname', [
    'type' => 'name',
    'rules' => 'required',
]);

// [
//     'id'    => 'nickname',
//     'type'  => 'text',
//     'rules' => 'required',
// ]

Override field type:

$array = FieldType::type('name', [
    'rules' => 'required',
]);

// [
//     'id'    => 'name',
//     'type'  => 'text',
//     'rules' => 'required',
// ]

If use front_rules attribute, only front use this rules:

$array = FieldType::type('name', [
    'type' => 'name',
    'front_rules' => 'required',
]);

// [
//     'id'    => 'name',
//     'type'  => 'text',
//     'rules' => 'required',
// ]

fields

The fields method is to traverse the array to execute the field method.

Parsing field data:

$fields = [
    'name',
    'age' => [
        'type' => 'number',
    ],
];
$array = FieldType::fields($fields);

// [
//     [
//         'id'    => 'name',
//         'type'  => 'text',
//         'rules' => 'required|max:20',
//     ],
//     [
//         'id'   => 'age',
//         'type' => 'number',
//     ],
// ]

list

Return the ID of each field:

$fields = [
    'name',
    'age' => [
        'type' => 'number',
    ],
];
$array = FieldType::list($fields);

// ['name', 'age']

casts

Transform to the right type:

$fields = [
    'name',
    'meeting_time' => [
        'type'  => 'datetime-local',
        'rules' => 'required',
    ],
];
$data = [
    'name'         => 'Bob',
    'meeting_time' => '2018-01-01T00:00',
];
$array = FieldType::casts($fields, $data);

// [
//     'name'         => 'Bob',
//     'meeting_time' => '2018-01-01 00:00:00',
// ]

form

Compile the form:

$form = $this->plain();
$fields = [
    'name',
    'meeting_time' => [
        'type' => 'datetime-local',
        'rules' => 'required',
    ],
];
$form = FieldType::form($form, $fields);

// => \Kris\LaravelFormBuilder\Form
// All fields have been added to the form.

rules

If use back_rules attribute, only back use this rules.

Parsing validation rules:

$fields = [
    'name',
    'phone' => [
        'rules' => 'required',
    ],
    'age' => [
        'back_rules' => 'required',
    ],
];
$array = FieldType::rules($fields);

// [
//     'name'  => 'required|max:20',
//     'phone' => 'required',
//     'age'   => 'required',
// ]

统计信息

  • 总下载量: 152
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-01-17

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固