Home > PHP Framework > Laravel > laravel request validation rules

laravel request validation rules

PHPz
Release: 2023-05-26 15:08:37
Original
1585 people have browsed it

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.

  1. Basic 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:

  • required: Required field.
  • email: Must be a valid email address.
  • numeric: must be a number.
  • max: Maximum value.
  • min: minimum value.
  • size: Must match the specified size.
  • in: Must be one of the specified values.

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'
]);
Copy after login

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.

  1. Custom Validation Rules

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;
});
Copy after login

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'
]);
Copy after login

In the above code, we add "my_password" to the verification rule. If the password input matches the custom rules, the verification passes.

  1. Error message customization

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.'
];
Copy after login

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);
Copy after login

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!

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template