ycs77/laravel-form-field-type

Fast set the form fields of the Laravel form builder.

v1.3.2 2022-02-09 10:27 UTC

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',
// ]