Home >PHP Framework >Laravel >Laravel Development: How to validate form requests using Laravel Validation?
Laravel is a popular PHP web development framework that provides many convenient features to speed up developers' work. Among them, Laravel Validation is a very practical function that can help us easily verify form requests and user-entered data. This article will introduce how to use Laravel Validation to validate form requests.
Laravel Validation is a form validation method built into the Laravel framework. It provides a concise, standardized, and easy-to-use way to validate form requests and user input data. You can use various rules to verify various data types, reducing code redundancy and simplifying the code development process.
First, we need to use Laravel's Validation class in the controller:
use IlluminateSupportFacadesValidator;
When we need to validate a request, we can use Validator: :make() method:
public function store(Request $request) { $validator = Validator::make($request->all(), [ 'name' => 'required|min:3|max:255', 'email' => 'required|email|unique:users,email', 'password' => 'required|min:8|confirmed', ]); if ($validator->fails()) { return redirect('register') ->withErrors($validator) ->withInput(); } // 在此处写入数据到数据库的逻辑 }
In the above code, we use the Validator::make() method to define validation rules. The first parameter passed to the make method is the request data that needs to be verified. The second parameter is an array defining validation rules. The keys in the array are the names of fields that need to be validated, and the values are validation rules in the form of strings.
In the above code, we define three fields:
If any rule fails to verify, we will send an error message Return to the registration page and use the withInput() method to return the data previously submitted by the user.
When using Laravel Validation, there are many rules available. Some common rules are listed below:
These rules are only part of the validation rules , you can also use more rules to validate the data. You can find a more detailed list of validation rules in Laravel's documentation.
Sometimes, we need custom rules to validate data. In Laravel, we can use the Validator::extend() method to implement custom validation rules. The following is an example of a custom rule:
Validator::extend('phone_number', function ($attribute, $value, $parameters, $validator) { return preg_match('/^1[3-9][0-9]{9}$/', $value); });
In this example, we define a custom rule "phone_number", which will verify whether $formData['phone_number'] meets your own defined specifications. If the verification is successful, return true, otherwise return false.
We can also customize the error message to make the error message more humane. The following is an example of a custom error message:
$validator = Validator::make($request->all(), [ 'email' => 'required|email|unique:users,email', 'password' => 'required|min:8|confirmed', ]); // 自定义错误信息 $validator->setAttributeNames([ 'email' => '邮箱', 'password' => '密码', ]); // 自定义错误信息模板 $validator->setCustomMessages([ 'required' => ':attribute字段不能为空', 'email' => '请输入正确的邮箱地址', 'unique' => ':attribute已经被占用了', 'min' => ':attribute长度不能小于:min个字符', 'confirmed' => '密码和确认密码不匹配', ]);
In the above code, we use the setAttributeNames() method to change the field names to make the error message more humane. We also used the setCustomMessages() method to define error message templates to make it easier and faster to change the error message and make it more user-friendly.
Through the above introduction, we learned how to use Laravel Validation to verify form requests. This can avoid errors caused by incorrect user input, handle error messages more conveniently, and save development time. Laravel Validation can better ensure the robustness and security of applications.
The above is the detailed content of Laravel Development: How to validate form requests using Laravel Validation?. For more information, please follow other related articles on the PHP Chinese website!