Home > Article > PHP Framework > Verification method in laravel
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.
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', ]); // 数据通过验证,将数据存储到数据库中 }
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:255
indicates that the field is required, of string type, and has a maximum length of 255 characters. unique:users
means verifying the uniqueness of this field in the users
table.
If verification fails, an error message will be automatically returned. If the verification is successful, the following code logic will continue to be executed.
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); } // 数据通过验证,将数据存储到数据库中 }
In the above example, use the Validator::make()
method to create a validation instance. Different from form validation, the make()
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.
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', ]);
In the above example, use the Validator::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_password
means 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 _).
When developing applications, it is often necessary to customize validation error messages. In Laravel, you can use the message()
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); }
In the above example, use the message()
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!