Laravel 5.1 — custom validation rules

Time:2020-1-14

laravel5.1Provides very convenient validation rules to verifyHTTPWith the transferred data, you can use these rules to complete almost any verification you want.
Believe in majority utilizationlaravelAll the friends who have done the project have mastered it. Now I will do custom verification in another way.
This custom validation is available inlaravel 4.*The document in is very clear, but in thelaravel 5.*There is no explanation in it. Now let’s implement it.

Suppose an article has several labels(tags), we will customize the validation rules to verifytags

Create a validator class

stayApp\ServicesNext newValidationclass

<?php
namespace App\Services;

use Illuminate\Validation\Validator;

class Validation extends Validator{
    public function ValidateTags($attribute, $value, $parameters){

        return preg_match("/^[A-Za-z0-9-éèàù]{1,50}?(,[A-Za-z0-9-éèàù]{1,50})*$/", $value);
    }
}
?>

Register the custom validator

ValidationWhen the class is complete, we need to register it in the service container
stayApp\Providers\AppServiceProvider.phpOfbootMethod:

<?php
namespace App\Providers;

use Validator;
use App\Services\Validation;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Validator::resolver(function($translator, $data, $rules, $messages)
        {
            return new Validation($translator, $data, $rules, $messages);
        });
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

bootMethod addedValidator::resolver(..)This code

Request verification

stayPostRequest.phpWe can write like this

return [
    'tags' => 'tags
];

Because we are verifying the field data of the article, we also need to provide itstitle,summart,contentOther fields

<?php

namespace App\Http\Requests;

use App\Http\Models\Post;
use App\Http\Requests\Request;

class PostRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $id = $this->blog ? ',' . $this->blog : '';
        
        return [
            'title' =>  'required|max:255',
            'slug'  =>  'required|unique:posts,slug' . $id,
            'summary' => 'required|max:65000',
            'content' => 'required|max:65000',
            'tags' => 'tags'
        ];
    }
}

When you’re done, you canControllerTest for success in.

laravel5.1Provided in Validator::extendBut the advantage of this is that we can customize the validation class, and once registered, we can validate according to its rules.

Recommended Today

Programmer’s essential foundation: git command comprehensive learning

preface Mastering git command is a necessary foundation for every programmer. I used to use smartgit until I saw that the big guys were using git command. Looking back, I found that I forgot some git commands, so I wrote this blog post to review it~ https://github.com/whx123/Jav… Official account: little boy picking up snail List […]