首页 > 后端开发 > php教程 > laravel 表单验让

laravel 表单验让

WBOY
发布: 2016-07-29 09:12:00
原创
954 人浏览过

使用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教程有兴趣的朋友有所帮助。

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板