Home >PHP Framework >Laravel >laravel request validation rules
With the continuous development of Internet applications, more and more enterprises and developers are now choosing to use the Laravel framework for development. When developing an application, data input validation is a very important part to protect the application from malicious attacks and problems caused by accidental input. In the Laravel framework, request validation rules are one way to implement data input validation.
Laravel provides a simple and very powerful validation rule syntax to easily validate input data. In this article, we will discuss how to validate input data using Laravel's request validation rules.
The Laravel framework provides some basic validation rules that can be used for various purposes during the validation process. Here are some commonly used basic validation rules:
Using validation rules in a controller is very simple. Suppose we have a method named validateInput, which needs to validate an input field named "username". The code can be like this:
$this->validate($request, [ 'username' => 'required|min:3|max:20' ]);
The above code will verify whether the "username" field is included and the length is Between 3 and 20, if these rules are not met, a ValidationException will be thrown.
In addition to the basic validation rules, the Laravel framework also allows you to create your own validation rules. These custom validation rules can be defined based on your specific needs. Suppose we need to verify an input field named "password". The field must contain uppercase and lowercase letters, numbers and special symbols, and be 8 to 20 characters in length. The code can be like this:
Step 1 :Define verification rules
Validator::extend('my_password', function ($attribute, $value, $parameters, $validator) { return preg_match('/^(?=.*[a-z])(?=.*[A-Z])(?=.*d)(?=.*(_|[^w])).+$/', $value) && strlen($value) >= 8 && strlen($value) <= 20; });
In the above code, we define a verification rule named "my_password", using regular expressions and string length to verify passwords. This validation rule will return true if the password entered meets the requirements.
Step 2: Use custom validation rules
Using custom validation rules is the same as using basic validation rules. We just need to add custom validation rules in the validation rules array. Suppose we need to add the verification rule for "password" in the previous example. The code can be as follows:
$this->validate($request, [ 'username' => 'required|min:3|max:20', 'password' => 'required|my_password' ]);
In the above code, we add "my_password" to the verification rule. If the password input matches the custom rules, the verification passes.
In the Laravel framework, you can easily customize the format for validation error messages. Here is an example:
Suppose we need to customize the password entry error message, we can do this:
Step 1: Define the error message
$messages = [ 'my_password' => 'The password must contain upper and lowercase letters, numbers, and special symbols. Its length must be between 8 and 20 characters.' ];
In the above code, We define an error message called "my_password".
Step Two: Use Custom Messages
Using error messages is similar to using custom validation rules. Just pass the validation rules that require customized messages to the second parameter of the validate method. Suppose we need to customize the error message for the "password" field, the code can be like this:
$this->validate($request, [ 'password' => 'required|my_password' ], $messages);
In the above code, we pass the error message as the third parameter to the validate method. If the password input does not match the custom rules, Laravel will display an error message that we define.
Summary
In this article, we discussed Laravel request validation rules, including how to use basic validation rules and custom validation rules, and how to customize error messages. Laravel's validation rule syntax is very simple yet powerful. Using Laravel's request validation rules, you can easily validate input data and ensure the security of your application.
The above is the detailed content of laravel request validation rules. For more information, please follow other related articles on the PHP Chinese website!