Laravel Form Validation create "Request Form" to implement a custom request class

  Create a form document in accordance with the request since the definition of class, always call 403 pages, consulting chiefs said:

public  function the authorize () 
{ 
     // To return to the method of this class form validation true, the default returns false, this method is used to determine whether the user has permission to use    
     return  to true ; 
}

  Then use within the controller can (remember the introduction of advance):

/**
 * 存储输入的博客文章
 *
 * @param  StoreBlogPostRequest  $request
 * @return Response
 */
public function store(StoreBlogPost $request){
    // The incoming request is valid...

    // Retrieve the validated input data...
    $validated = $request->validated();
}

 

  Reprinted from the following: https: //laravelacademy.org/post/19465.html

  

 

  Command line:

php artisan make:request StoreBlogPost

  The generated class is located in app/Http/Requeststhe directory, if the directory does not exist, run make:requestwill generate for us command.

  Validation rules to add a little class rulesmethod:

/ * * 
 * Get the validation rule applied to the request 
 * 
 * @return Array 
 * / 
public  function the rules () {
     return [
         'title' => 'required | UNIQUE: Posts | max: 255', 
        'body' => 'required ' 
    ]; 
}

  So, how do validation rules take effect? You have to do is type in the controller method in class prompted the request. Such a form input request is validated before the controller method is called, which means that you do not have a method and validation logic controller mixed together with:

/**
 * 存储输入的博客文章
 *
 * @param  StoreBlogPostRequest  $request
 * @return Response
 */
public function store(StoreBlogPost $request){
    // The incoming request is valid...

    // Retrieve the validated input data...
    $validated = $request->validated();
}

  If the verification fails, the redirect response and the user will be generated on a retracted position, an error message will be stored into a disposable Session for display in the view. If AJAX request with 422an HTTP status code will be returned to the user response, the response also contains the data validation errors JSON format.

Guess you like

Origin www.cnblogs.com/xiaqiuchu/p/11414320.html