Home >Common Problem >How to modify laravel verification in Chinese

How to modify laravel verification in Chinese

DDD
DDDOriginal
2023-07-05 14:54:331657browse

The steps for laravel to modify and verify Chinese: 1. Create a custom validator and create a new PHP file in the Requests folder; 2. Define verification rules and replace the original English verification rules with Chinese Verification rules; 3. Define Chinese prompt information in the messages method of the custom validator class; 4. Use a custom validator and use the validate method to verify the request data. If the verification fails, it will automatically Redirects the previous page and displays an error message to the user.

How to modify laravel verification in Chinese

#The operating environment of this article: Windows 10 system, laravel 9 version, dell g3 computer.

Laravel is a popular PHP development framework with built-in powerful authentication capabilities. In Laravel, the default validation rules are based on English, so to modify the validation in Chinese, some additional processing is required.

The following will introduce how to modify the Chinese information in Laravel validation rules.

1. Create a custom validator

First, we need to create a custom validator class. In Laravel, validator classes are generally placed in the app/Http/Requests directory. If there is no Requests folder in this directory, you can create it yourself.

Create a new PHP file in the Requests folder, such as CustomValidation.php. In this file, we define a custom validator class, which inherits from Laravel's basic validator class Illuminate\Foundation\Http\FormRequest. The code is as follows:

namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class CustomValidation extends FormRequest
{
    public function rules()
    {
        // 定义规则
    }
    public function messages()
    {
        // 定义中文提示信息
    }
}

2. Define validation rules

In the rules method of the custom validator class, we can define the validation rules we need. Here, we need to replace the original English verification rules with Chinese verification rules.

For example, suppose we want to verify the user name. The original rule may be 'name' => 'required|string|min:3|max:255'. Now we write this rule in Chinese, For example, 'name' => 'required|string|min:3|max:255'. After defining the rules, return them to the rules method.

The following is an example that implements a Chinese verification rule for user names:

public function rules()
{
    return [
        'name' => 'required|string|min:3|max:255',
    ];
}

3. Define Chinese prompt information

In customization In the messages method of the validator class, we can define Chinese prompt information. When rule validation fails, Laravel will return the corresponding error message based on the language displayed in the configuration file. Therefore, we need to define Chinese prompt information in the messages method.

Similar to the definition of rules, we can define different prompt information for each verification rule. For example, we define the Chinese prompt information for the username rule as follows:

public function messages()
{
    return [
        'name.required' => '用户名不能为空',
        'name.min' => '用户名至少为3个字符',
        'name.max' => '用户名最多为255个字符',
    ];
}

4. Using a custom validator

We have completed the creation and creation of the custom validator Rule definition, you can use it in controller methods that require validation. Suppose we want to validate a form request, we can inject an instance of a custom validator in the parameter of the controller method.

Use the validate method to verify the request data. If the verification fails, it will automatically redirect to the previous page and display an error message. You can add the following code in the controller method:

public function store(CustomValidation $request)
{
    $validatedData = $request->validate();
    // 执行其他逻辑
}

In the above code, we pass the CustomValidation class as a parameter to the store method. $request->validate() will automatically verify the request data and return the verified data.

Summary:

Through the above steps, we can successfully modify the Chinese information of the verification rules in Laravel. First, create a custom validator class and inherit from Illuminate\Foundation\Http\FormRequest. Then, define custom validation rules and replace the original English validation rules. Finally, define Chinese prompt information to be displayed to the user when verification fails.

The above is the detailed content of How to modify laravel verification in Chinese. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn