当前位置:网站首页>Use of laravel verifier

Use of laravel verifier

2022-06-24 23:12:00 Wang Daochang's way of programming

One 、 Three ways to use the verifier

1.1 Verifier class

php artisan make:request DemoRequest

Then the framework will generate a Request/DemoRequest.php The catalog file for

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class DemoRequest extends FormRequest{
    
    public function authorize(){
    
        return false;
    }

    public function rules(){
    
        return [];
    }
    public function messages(){
    
      return [];
    }
}

1.2 controller Layer built-in verifier

Democontroller.php In file

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class DemoController extends Controller{
    
    public function index(Request $request){
    
        $this->validate();
    }
}

1.3 Verifier facade

$validate = Validator::make();
# Judge whether the verification is passed fails()/
if($validate->fails()){
    
  var_dump($validate->getMessageBag()->getMessages())
} 

Two 、 Custom validator

2.1 Generate by command

php artisan make:rule DemoValidate

model event

Model Time :https://blog.csdn.net/qq_37868757/article/details/107043935

3、 ... and 、 Verify that the attributes are translated into Chinese

3.1 Download font library

Download and switch font library :
First you need to download Font library
When the download is complete , In the compressed package src/zh-CN The folder is copied to the project directory resources/lang Under the folder .
modify config->app.php file , Modify the code as follows :

'locale' => 'zh-CN',

because captcha There is no Chinese explanation in the Chinese bag , So you need to manually add Chinese explanations , The specific operation is as follows :
open resources/zh-CN/validation.php, Add the following key value pairs to the total array :

'captcha'                  => ':attribute  Incorrect .',

stay attributes The following key value pairs are appended to the array :

'captcha'               => ' Verification Code ',
原网站

版权声明
本文为[Wang Daochang's way of programming]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241719271330.html