Home> PHP Framework> Laravel> body text

Verification method in laravel

WBOY
Release: 2023-05-20 22:16:36
Original
1229 people have browsed it

Laravel is a popular PHP framework that combines ease of use with powerful features. One of the important functions is validation. Laravel provides multiple validation methods to easily verify the data submitted by the form and ensure the accuracy and security of the application data. This article will introduce commonly used verification methods in Laravel.

  1. Form validation

Form validation is the most commonly used validation method in Laravel. It is used to verify whether the data submitted by the form conforms to the specified rules. Each validation rule can define an error message to be output when validation fails. The following is an example of form validation:

public function store(Request $request) { $request->validate([ 'name' => 'required|string|max:255', 'email' => 'required|string|email|max:255|unique:users', 'password' => 'required|string|min:8|confirmed', ]); // 数据通过验证,将数据存储到数据库中 }
Copy after login

In the above example, the$request->validate([])method is used to validate the data in the request. This method accepts an array containing validation rules. The key name of the array represents the field name to be verified, and the key value represents the validation rule for the field.

Each validation rule is separated by a pipe character|, which contains the validation parameters of the rule. For example,required|string|max:255indicates that the field is required, of string type, and has a maximum length of 255 characters.unique:usersmeans verifying the uniqueness of this field in theuserstable.

If verification fails, an error message will be automatically returned. If the verification is successful, the following code logic will continue to be executed.

  1. JSON Validation

Laravel also provides JSON data validation methods. JSON validation is usually used in API interface development to verify whether the JSON data sent to the server complies with specified rules. Similar to form validation, JSON validation can also define validation rules and error messages.

The following is an example of JSON validation:

public function store(Request $request) { $validator = Validator::make($request->all(), [ 'name' => 'required|string|max:255', 'email' => 'required|string|email|max:255|unique:users', 'password' => 'required|string|min:8|confirmed', ]); if ($validator->fails()) { return response()->json([ 'status' => 'error', 'message' => $validator->errors(), ], 422); } // 数据通过验证,将数据存储到数据库中 }
Copy after login

In the above example, use theValidator::make()method to create a validation instance. Different from form validation, themake()method requires passing two parameters: validation data and validation rules. If validation fails, a JSON response containing error information needs to be returned. If the verification is successful, the following code logic will continue to be executed.

  1. Custom validation rules

In addition to Laravel’s built-in validation rules, developers can also customize validation rules. Custom rules can meet special verification needs, such as verification time, password strength, etc.

The following is an example of a custom validation rule:

Validator::extend('strong_password', function ($attribute, $value, $parameters, $validator) { return preg_match('/^(?=.*[a-zA-Z])(?=.*d)(?=.*(_|[^w])).+$/', $value); }); $validator = Validator::make($request->all(), [ 'password' => 'required|string|min:8|strong_password', ]);
Copy after login

In the above example, use theValidator::extend()method to create a custom validation rulestrong_password. This method accepts two parameters: the rule name and a closure function, which accepts four parameters: validation field name, validation field value, validation parameters, and validator object.

After defining custom rules, you need to use the custom rules in the validator instance. As shown in the above code,min:8|strong_passwordmeans that the password must be at least 8 characters long and must contain at least one uppercase and lowercase letter, one number, and one special character (except underscore _).

  1. Error message customization

When developing applications, it is often necessary to customize validation error messages. In Laravel, you can use themessage()method to customize error messages.

The following is an example of customizing the validation error message:

$validator = Validator::make($request->all(), [ 'name' => 'required|string|max:255', 'email' => 'required|string|email|max:255|unique:users', 'password' => 'required|string|min:8|confirmed', ]); $validator->message('email.required', '邮箱不能为空'); if ($validator->fails()) { return response()->json([ 'status' => 'error', 'message' => $validator->errors(), ], 422); }
Copy after login

In the above example, use themessage()method to customize the error message. This method accepts two parameters: the validation rule name and a custom error message.

The above are commonly used verification methods in Laravel. When writing applications, the correct use of verification methods can effectively ensure the accuracy and security of data.

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

source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!