Maison >développement back-end >tutoriel php >laravel 表单验让
使用request的方式
<code>E:<span>\Laravel</span><span>\blog</span>>php artisan make:request ArticleRequest</code>
在app/http/request下生成ArticleRequest.php修改如下
<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>/** * 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 为验证规则
authorize 要返回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>
控制器只需要Requests\ArticleRequest 参数类型用上面的request即可
<code><span>public</span><span><span>function</span><span>store</span><span>(Requests\ArticleRequest <span>$req</span>)</span>{</span> dd(<span>"fdsaf"</span>); }</code>
验证通过后执行方法,不通过重定向表单页面
使用validate的方式
控制器参数正常 的request
validate 第二个参数与上面的rules相同
<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>
中文问题
config/app.php
locale 改成中文
<code><span>'locale'</span> => <span>'zh'</span>,</code>
将项目中resources/lang/en 文件夹,复制一份改成zh
对应着修改成中文
如字段必面修改
resources/lang/zh/validation.php中的
<code><span>'<strong>require</strong>d'</span> => <span>'The :attribute field 必须.'</span>,</code>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });
以上就介绍了laravel 表单验让,包括了require方面的内容,希望对PHP教程有兴趣的朋友有所帮助。