首頁 > 後端開發 > php教程 > laravel 表單驗讓

laravel 表單驗讓

WBOY
發布: 2016-07-29 09:12:00
原創
972 人瀏覽過

使用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>
{
    <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>
登入後複製

控制器只需要RequestsArticleRequest 參數類型用上面的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的方式

控制器參數正常的 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

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
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板