Home >Backend Development >PHP Tutorial >laravel form validation
Use request method
<code>E:<span>\Laravel</span><span>\blog</span>>php artisan make:request ArticleRequest</code>
Generate ArticleRequest.php under app/http/request and modify it as follows
<code><?php namespace App\Http\Requests; use App\Http\Requests\Request; <span><span>class</span><span>ArticleRequest</span><span>extends</span><span>Request</span></span> { <span>/** * Determine if the user is authorized to make this request. * * <span>@return</span> bool */</span> public function authorize() { <span>return</span><span>true</span>; } <span>/** * Get the validation rules that apply to the request. * * <span>@return</span> array */</span> public function rules() { <span>return</span> [ <span>//</span><span>"name"</span>=><span>"<strong>require</strong>d"</span>, ]; } } </code>
rules are verification rules
authorize To return true
<code>{<span>!</span><span>!</span><span>Form</span>::<span>open</span>([<span>"url"</span>=><span>"/site/store"</span>]) <span>!</span><span>!</span>} {<span>!</span><span>!</span><span>Form</span>::<span>text</span>(<span>"name"</span>,<span>"aa"</span>,[<span>"class"</span>=><span>"form-controller"</span>,<span>"id"</span>=><span>"t"</span>]) <span>!</span><span>!</span>} {<span>!</span><span>!</span><span>Form</span>::<span>submit</span>() <span>!</span><span>!</span>} {<span>!</span><span>!</span><span>Form</span>::<span>close</span>() <span>!</span><span>!</span>} {{--{{$<span>errors</span>}}--}} {{--{<span>!</span><span>!</span> $<span>errors</span><span>!</span><span>!</span>}--}} //用于显示验证不通过的错误信息 @<span>if</span>($<span>errors</span><span>-></span><span>any</span>()) @<span>foreach</span>($<span>errors</span><span>-></span><span>all</span>() <span>as</span> $<span>err</span>) {<span>!</span><span>!</span> $<span>err</span><span>!</span><span>!</span>} @<span>endforeach</span> @<span>endif</span></code>
The controller only needs RequestsArticleRequest. The parameter type can be the request above
<code><span>public</span><span><span>function</span><span>store</span><span>(Requests\ArticleRequest <span>$req</span>)</span>{</span> dd(<span>"fdsaf"</span>); }</code>
Execute the method after the verification is passed, without redirecting the form page
Use validate method
The request with normal controller parameters
The second parameter of validate is the same as the rules above
<code><span>public</span><span><span>function</span><span>store</span><span>(Request <span>$req</span>)</span>{</span><span>$this</span>->validate(<span>$req</span>,[<span>"name"</span>=><span>"<strong>require</strong>d"</span>]); dd(<span>"fdsaf"</span>); }</code>
Chinese problem
config/app.php
locale changed to Chinese
<code><span>'locale'</span> => <span>'zh'</span>,</code>
Copy the resources/lang/en folder in the project and change it to zh
Correspondingly modified to Chinese
If the field must be modified,
<code><span>'<strong>require</strong>d'</span> => <span>'The :attribute field 必须.'</span>,</code>
').addClass('pre-numbering').hide(); in
resources/lang/zh/validation.php; $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });The above introduces laravel form validation, including require aspects. I hope it will be helpful to friends who are interested in PHP tutorials.