Write request validation in Laravel learning tutorial, laravelvalidation

Source: Internet
Author: User

Write request validation in Laravel learning tutorial, laravelvalidation

Preface

This article mainly introduces the related content written by request validation of Laravel. When using laravel to write an api, when the request sent by the front end is a method such as POST/PUT/PATH, request validation is required. Although the Angular/Vue of the frontend program has already implemented validation for the front-end and back-end separation programs, the json input passed by ajax also needs to be validated on the back-end.

Then how can we write request validation elegantly? Laravel official documentation already contains this feature: Form Request Validation

The implementation method is as follows:

Here we can write a JsonRequest:

class JsonRequest extends Illuminate\Foundation\Http\FormRequest{ public function rules() {  $method = $this->method();    assert(in_array($method, [static::METHOD_POST, static::METHOD_PUT, static::METHOD_PATCH], true));    $controller = $this->route()->getController();  $rules  = $controller::RULES;  return ($rules[$this->method()] ?? []) + ($rules['*'] ?? []); } public function authorize() {  return true; }}

In this way, JsonRequest can be used in many Model controllers, such:

use Illuminate\Http\Request;final class AccountController extends \App\Http\Controllers\Controller{ public const RULES = [  Request::METHOD_POST => [   'bank_account' => 'required_if:type,bank',   'loan_account' => 'required_if:type,loan',  ],  Request::METHOD_PUT => [   // ...  ],  '*' => [   // ...  ], ];}

In this way, you can check whether the json input passed in by the front end is legal.

(1) If the json input received by the front-end is:

{ "name": "lx1036", "type": "loan", "bank_account": {  "source": "bank", }}

Then the validation fails and is invalid.

(2) If the json input received by the front-end is:

{ "name": "lx1036", "type": "bank", "loan_account": {  "source": "loan", }}

Then the validation fails and is invalid.

In this way, json input can be verified. If the input is invalid, an HttpException is thrown back, which is no longer used in the next logic. For such nested json input, it is important to use request validation to verify the relationship between objects. It can be seen as a preliminary verification before entering the core business logic .. Of course, there is model validation at the end of writing the table to avoid bad data entering the db.

Last, the laravel document only describes the usage and does not describe the principles. The code is in \ Illuminate \ Foundation \ Providers \ FormRequestServiceProvider: class:

Public function boot () {// \ Illuminate \ Foundation \ Http \ FormRequest use ValidatesWhenResolvedTrait, extends \ Illuminate \ Contracts \ Validation \ ValidatesWhenResolved $ this-> app-> afterResolving (ValidatesWhenResolved: class, function ($ resolved) {$ resolved-> validate ();});//...}

Therefore, after the \ Illuminate \ Foundation \ Http \ FormRequest in the container is resolve, the \ Illuminate \ Foundation \ Http \ FormRequest: validate () method is immediately executed, for more information, see laravel source code.

OK. In short, validation is very important when writing a program and needs to be written, including request validation and model validation...

Summary

The above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.