Home > Article > Backend Development > Laravel 5 Basics (11) - Form Validation
When creating an article, if you submit it directly without entering anything, ok, you will get an empty article without any error message, which is wrong. Run php artisan
from the command line and you will see an option make:request
to create a new form request class. Execute from command line
<code>php artisan make:request CreateArticleRequest</code>
The generated files are in the app/http/requests
directory. In the file we can see two methods:
<code> public function authorize() { return false; } public function rules() { return [ // ]; }</code>
authorize
indicates whether the user needs to be an authenticated user when submitting the form. We do not need authentication and return true. rules
is our rules method. Let’s modify this method:
<code> public function authorize() { //修改为 true,表示不需要认证,或者是通过认证 return true; } public function rules() { return [ 'title' => 'required|min:3', 'body' => 'required', 'published_at' => 'required|date' ]; }</code>
For other constraints, please refer to laravel’s documentation. The above constraints indicate that title
must be entered, at least 3 characters, body
is required, published_at
is required and is a date.
In the view, we can always access the $errors
variable to determine whether we have errors and modify the view
<code> @if ($errors->any()) <ul class="alert alert-danger"> @foreach($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> @endif {{--使用我们添加的 illuminate\html 开源库--}} {!! Form::open(['url' => 'articles']) !!}</code>
Modify the controller and introduce our Request class.
<code> public function store(Requests\CreateArticleRequest $request) { Article::create($request->all()); return redirect('articles'); }</code>
Submit the form again without filling in anything and you will see the error message.
Modify the prompt information to Chinese
The error message displayed is in English. In fact, laravel has taken into account the issue of internationalization and first modified config/app.php
,
<code> 'locale' => 'zh',</code>
Set the locale language to Chinese, then create a new folder zh
under resources/lang
, copy the resources/lang/en/validation.php
file to the zh
directory, modify:
<code> "min" => [ "numeric" => "The :attribute must be at least :min.", "file" => "The :attribute must be at least :min kilobytes.", "string" => ":attribute 至少要包含 :min 字符。", "array" => "The :attribute must have at least :min items.", ], "required" => ":attribute 必须填写。",</code>
Others can be translated by yourself. Submit the empty form again and the error message is in Chinese. Moreover, the judgment of min:3
is also at least 3 Chinese characters.
--
laravel also integrates the validate
method in the controller. In other words, we don’t have to generate the request class. We can complete these tasks directly in the controller.
Modify controller:
<code> //注意 Request 的命名空间,不要弄错了 public function store(\Illuminate\Http\Request $request) { $this->validate($request, [ 'title' => 'required|min:3', 'body' => 'required', 'published_at' => 'required|date' ]); Article::create($request->all()); return redirect('articles'); }</code>
The results are the same, so simple verification can be completed faster.
The above has introduced the basics of Laravel 5 (11) - form validation, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.