Laravel5 basics (11)-form verification when creating an article, if you submit without entering anything, OK, you get an empty article without any error prompt, this is not correct. Run phpartisan on the command line to view the make: request option and create a new formrequest class. Run phpartisanmake: requestCreateArtic Laravel 5 Basic (11)-form verification on the command line
When you create an article, if you do not input anything and submit it directly, OK, you get an empty article without any error message. this is wrong. Runphp artisan
You can see an optionmake:request
To create a form request class. Execute
php artisan make:request CreateArticleRequest
The generated file is inapp/http/requests
Directory. In the file, we can see two methods:
public function authorize(){return false;}public function rules(){return [//];}
authorize
Whether the user needs to be authenticated when submitting the form. if authentication is not required, true is returned.rules
Is our rule method. Let's modify this method:
Public function authorize () {// modify to true, indicating that authentication is not required, or return true through authentication;} public function rules () {return ['title' => 'required | min: 3', 'body' => 'required', 'published _ at' => 'required | date'];}
Other constraints can be inserted into laravel's documentation. The constraints above indicatetitle
Required. it must contain at least 3 characters,body
Is required,published_at
It is required and a date.
In the view, we can always access$errors
Variable to determine whether there is an error, modify the View
@ If ($ errors-> any ())
@ Foreach ($ errors-> all () as $ error)
- {$ Error }}
@ Endforeach
@ Endif {-- use the illuminate \ html open source library we added --}}{!! Form: open (['URL' => 'articles '])!}
Modify the controller and introduce our Request class.
public function store(Requests\CreateArticleRequest $request) { Article::create($request->all()); return redirect('articles'); }
If you submit the form again without entering anything, you can see the error message.
Change the prompt to Chinese
The error message is displayed in English. In fact, laravel considers internationalization and first modifiesconfig/app.php
,
'locale' => 'zh',
Set the locale language to Chinese, and thenresources/lang
Create a folderzh
, Copyresources/lang/en/validation.php
Filezh
Directory, modify:
"Min" => ["numeric" => "The: attribute must be at least: min. "," file "=>" The: attribute must be at least: min kilobytes. "," string "=>": attribute must contain at least min characters. "," Array "=>" The: attribute must have at least: min items. ",]," required "=>": attribute is required. ",
Others can be translated by yourself. Submit an empty form again. The error message is in Chinese. Andmin:3
Is also judged to be at least three Chinese characters.
--
Laravel is also integrated in the controllervalidate
METHOD. In other words, we do not have to generate the request class, which can be done directly in the controller.
Modify controller:
// Note the Request namespace. do not mistake public function store (\ Illuminate \ Http \ Request $ request) {$ this-> validate ($ request, ['title' => 'required | min: 3', 'body' => 'requestred', 'hhed _ at' => 'required | date']); Article:: create ($ request-> all (); return redirect ('article ');}
The results are the same, so that simple verification can be completed more quickly.