In browser form validation in laravel

In laravel 5 I describe models without specifying fields in model class. Some magic identifies which fields exist in database for this model.

use IlluminateDatabaseEloquentModel;

class MyModel extends Model {}

When I describe migration for model’s table, I specify field type, length and so on. This info exists only in my head and in database.

When I need to validate data, I have to specify requirements again.

What I want – describe all requirements for all fields in one place.
Like so:

class MyModel extends Model {
    public $form_fields = [
        'email' => ['required', 'email', 'human_name' => 'E-mail', 'input_type' => 'text'],
        'age' => ['required', 'positive_integer', 'range:18,90', 'input_type' => 'text'],
        'first_name' => ['string', 'human_name' => 'Your first name', 'input_type' => 'text'],
        'agreement' => ['required', 'human_name' 

Why I need this?

This way I want to ensure that all rules for all models will be written in one place.

Later I want to take these rules in validation

$this->validate($request, MyModel::$form_fields);

and in templates

{{ show_field(MyModel::$form_fields['email']) }}
{{ show_field(MyModel::$form_fields['age']) }}
{{ show_field(MyModel::$form_fields['first_name']) }}

show_field is some global function which takes ‘human_name’ and ‘input_type’ and generates html code with all required validation rules. May be there is some function or class with required functionality exists?

How can I achieve this?

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *