久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Laravel 5.5 - 同時驗(yàn)證多個表單請求

Laravel 5.5 - Validate Multiple Form Request - at the same time(Laravel 5.5 - 同時驗(yàn)證多個表單請求)
本文介紹了Laravel 5.5 - 同時驗(yàn)證多個表單請求的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

問題已經(jīng)問過了此處 用于先前版本的 laravel,但尚未回答.

The question is already asked here for a previous version of laravel and not yet answered.

我有一個 html 表單,它使用三種不同的 表單請求驗(yàn)證 進(jìn)行驗(yàn)證.我有能力做到這一點(diǎn).但是,問題是,表單驗(yàn)證是一一進(jìn)行的.不是同時.

I have a html form which is validated using three different Form Request Validations. I am able to do this. But, the problem is, the form validations take place one by one. Not at the same time.

如果第一個表單請求拋出驗(yàn)證錯誤,則該表單將返回到 view,因此不會評估其余兩個表單,因此無法向用戶顯示正確的驗(yàn)證錯誤.

If the first form request throws a validation error the form is returned to the view so rest of the two forms doesn't evaluated, hence a proper validation error can't be displayed to the user.

我想要的是:同時使用三個表單驗(yàn)證請求規(guī)則來驗(yàn)證表單.

What I want is : validate the form with the three form validation requests rules at the same time.

控制器:

public function store(TransportationRequest $request1, PurchaseRequest $request2, SaleRequest $request3)
    {
        //do actions here
    }

我嘗試過一一繼承表單請求,但沒有成功.

I have tried with inheriting the form requests one by one but could not be succeeded.

更具體地說明我的問題:

To be more specific to my question:

我確實(shí)有 purchasetransporataionsale 三個單獨(dú)的表格,它們分別使用 PurchaseRequest 進(jìn)行評估,TransportationRequestSaleRequest 用于單個操作.

I do have three seperate forms for purchase, transporataion and sale which are individually valuated using PurchaseRequest, TransportationRequest and SaleRequest for individual operations.

但是有一個特殊情況,其中一個表單處理purchasetransporataionsale.我想結(jié)合使用三個表單請求規(guī)則來驗(yàn)證表單,因?yàn)槲也幌朐俅尉帉懴嗤尿?yàn)證規(guī)則.

But there is a special case where a single form handles a purchase, transporataion and a sale. I want to validate the form using combining the three form request rules because I didn't want to write the same validation rules again.

這個

注意:單獨(dú)表格和合并表格中的字段相同.

Note : The fields in the seperate forms and combined form are same.

謝謝..

推薦答案

A FormRequest throws an IlluminateValidationValidationException Exception when validation failed which has a redirectTo 方法,然后是 異常 Handler 執(zhí)行重定向.

A FormRequest throws an IlluminateValidationValidationException Exception when validation fails which has a redirectTo method, and from there the Exception Handler performs the redirect.

您可以通過在 try/catch 塊中的控制器中手動運(yùn)行表單請求來實(shí)現(xiàn)您想要的行為,該塊捕獲錯誤并在重定向之前組合錯誤包,或者如果您必須通過 Laravel 將它們注入到您的控制器,那么您需要添加自己的異常處理程序來捕獲所有錯誤,將它們組合起來,然后在最終的表單請求運(yùn)行后重定向.

You can achieve your desired behaviour by running your Form Requests manually in your controller within a try/catch block which captures the errors and combines the error bags before redirecting, or if it's essential that you run them by Laravel injecting them into your controller then you would need to add your own exception handler which captures all of the errors, combines them and then redirects after the final Form Request has ran.

但是,值得注意的是,這兩種方法都不是很好:它們很麻煩,并且會給您帶來比解決的問題更多的問題.如果您想編寫一個可維護(hù)的應(yīng)用程序,您應(yīng)該盡量堅(jiān)持 Laravel 的做事方式.

However, it's worth noting, both of those approaches aren't great: they're cumbersome and are going to cause you more problems than they solve. You should try to adhere to the Laravel way of doing things as best possible if you'd like to write a maintainable application.

存在表單請求以驗(yàn)證表單,因此,每個表單應(yīng)該有一個表單請求,如果您希望從不同的規(guī)則集組成表單請求,那么應(yīng)該在表單請求中完成,例如:

A Form Request exists to validate a form, therefore, each Form should have one Form Request, if you wish to compose a Form Request from different sets of rules then that should be done within the Form Request, e.g:

  1. 為表單定義表單請求 php artisan make:request StoreMultipleForm
  2. StoreMultipleForm 上的 rules 方法獲取每個其他表單請求的 rules,然后將它們一起返回,例如:

  1. Define your Form Request for your form php artisan make:request StoreMultipleForm
  2. From the rules method on StoreMultipleForm fetch the rules for each of the other Form Requests and then return them together, e.g:

/**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules()
{
    $formRequests = [
      TransportationRequest::class,
      PurchaseRequest::class,
      SaleRequest::class
    ];

    $rules = [];

    foreach ($formRequests as $source) {
      $rules = array_merge(
        $rules,
        (new $source)->rules()
      );
    }

    return $rules;
}

  • 在您的控制器中使用新的組合表單請求,例如:

  • Use the new composed Form Request in your controller, e.g:

    public function store(StoreMultipleForm $request)
    {
        // Do actions here.
    }
    

  • 這種方法的優(yōu)點(diǎn)是它是自包含的,它符合一個表單一個表單請求的期望,不需要更改您正在合并的表單請求,如果您需要為此表單添加額外的規(guī)則,您無需創(chuàng)建另一個表單請求.

    The advantages of this method are that it's self-contained, it adheres to the one form one Form Request expectation, it doesn't require changes to the Form Requests you're combining and if you need to add additional rules unique to this form you can do so without creating another Form Request.

    這篇關(guān)于Laravel 5.5 - 同時驗(yàn)證多個表單請求的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

    【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

    相關(guān)文檔推薦

    add new element in laravel collection object(在 Laravel 集合對象中添加新元素)
    Creating an edit modal in Laravel 5(在 Laravel 5 中創(chuàng)建編輯模式)
    Laravel 5.5 API resources for collections (standalone data)(用于集合的 Laravel 5.5 API 資源(獨(dú)立數(shù)據(jù)))
    What is the best practice to create a custom helper function in php Laravel 5?(在 php Laravel 5 中創(chuàng)建自定義輔助函數(shù)的最佳實(shí)踐是什么?)
    No #39;Access-Control-Allow-Origin#39; header - Laravel(沒有“Access-Control-Allow-Origin標(biāo)頭 - Laravel)
    Laravel Passport Route redirects to login page(Laravel Passport Route 重定向到登錄頁面)
    主站蜘蛛池模板: 黄网站免费在线观看 | 国产一区二区三区免费观看视频 | 91精品一区二区三区久久久久 | 国产精品久久国产精品99 gif | 视频一二三区 | 精品1区2区3区 | 老头搡老女人毛片视频在线看 | av免费网站在线观看 | 欧美综合一区二区 | 男人的天堂视频网站 | 色999日韩 | 日本三级网址 | 欧美日在线 | www.99久久.com| 成人午夜精品一区二区三区 | 另类亚洲视频 | 国产中文字幕亚洲 | 国产精品免费观看 | www.99热.com| 欧美1区| 能看的av| 日韩高清黄色 | 欧美日韩在线观看一区二区三区 | 99免费在线观看视频 | 欧美福利三区 | 国产精品久久 | 牛牛热在线视频 | www.中文字幕av | 久久国产精品视频观看 | 久久久久99 | 欧洲视频一区二区 | 亚洲精品一区二区三区蜜桃久 | 免费观看日韩精品 | jizz在线看片 | 成人在线视频网 | 国产乱码精品1区2区3区 | 麻豆精品久久久 | 欧美三级三级三级爽爽爽 | 天天操狠狠操 | 亚洲精品一区二区三区蜜桃久 | 日韩激情一区 |