laravel 表单验让

WBOY
Lepaskan: 2016-07-29 09:12:00
asal
960 orang telah melayarinya

使用request的方式

<code>E:<span>\Laravel</span><span>\blog</span>>php artisan make:request ArticleRequest</code>
Salin selepas log masuk

在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>
Salin selepas log masuk

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>
Salin selepas log masuk

控制器只需要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>
Salin selepas log masuk

验证通过后执行方法,不通过重定向表单页面

使用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>
Salin selepas log masuk

中文问题

config/app.php

locale 改成中文

<code><span>'locale'</span> => <span>'zh'</span>,</code>
Salin selepas log masuk

将项目中resources/lang/en 文件夹,复制一份改成zh
对应着修改成中文
如字段必面修改

resources/lang/zh/validation.php中的

<code><span>'<strong>require</strong>d'</span>             => <span>'The :attribute field 必须.'</span>,</code>
Salin selepas log masuk
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介绍了laravel 表单验让,包括了require方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan