Create a request verification file for Laravel form verification
php artisan make:request ArticlesRequest Request created successfully.
The file content is as follows:
App/Http/Requests/ArticlesRequest. php <? Phpnamespace App \ Http \ Requests; use App \ Http \ Requests \ Request; class ArticlesRequest extends Request {/*** Determine if the user is authorized to make this request. ** @ return bool */public function authorize () {return true; // This is a management permission. it is set to true first, all permissions are available}/*** Get the validation rules that apply to the request. ** @ return array */public function rules () {return ['title' => 'required', // The Project to be verified and the verification rules are specified here, compile 'content' => 'requestred'] in array mode;}
Verification rules refer to the official website: https://laravel.com/docs/5.2/validation
Modify the controller's store method
App/Http/Requests/ArticlesRequest. phpuse App \ Http \ Requests \ ArticlesRequest; // public function store (Request $ requests) {public function store (ArticlesRequest $ requests) {// Here, the created ArticlesRequest instance Articles: create ($ requests-> all (); return redirect ('/articles ');}
Because verification is now added, the request for entering the store method will be filtered, but the direct filtering does not know the success or failure, so an additional prompt should be added.
Resources/views/articles/create. blade. php @ extends ('layout. app') @ section ('content') create an article {!! Form: open (['URL' => '/articles/store'])!}
{!! Form: label ('title', 'Title :')!!} {!! Form: text ('title', null, ['class' => 'form-control'])!}
{!! Form: label ('content', 'content :')!!} {!! Form: textarea ('content', null, ['class' => 'form-control'])!}
{!! Form: label ('Publish _ at', 'publish _ :')!!} {!! Form: date ('Publish _ at', date ('Y-m-D'), ['class' => 'form-control'])!}
{!! Form: submit ('post analytics', ['class' => 'btn btn-primary form-control'])!} {!! Form: close ()!!} @ If ($ errors-> any () // add here. The $ errors variable is provided by laravel, which is the variable saved when these errors are captured. it is an array.
@ Foreach ($ errors-> all () as $ error) // You Need to loop through the variable array to obtain the final error message.
- {$ Error }}
@ Endforeach
@ Endif @ stop
In laravel 5.2, the $ errors variable must exist in the middleware web. if not, laravel reports an error, Undefined variable: errors.
Normally, an error message is displayed on the webpage, for example
The title field is required.The content field is required.
Rewrite the error message. for example, if you want to change it to a Chinese prompt, you need to rewrite the messages method in the request file, which is not in the file by default. Therefore, you need to add
App/Http/Requests/ArticlesRequest. php public function messages () {return ['Title. required' => 'this title is required! ', // For example, this will match the rule of the title required, and then trigger your prompt 'Body. required' =>' this body is required! ',];}