問題描述
問題已經(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í)有 purchase
、transporataion
和 sale
三個單獨(dú)的表格,它們分別使用 PurchaseRequest
進(jìn)行評估,TransportationRequest
和 SaleRequest
用于單個操作.
I do have three seperate forms for purchase
, transporataion
and sale
which are individually valuated using PurchaseRequest
, TransportationRequest
and SaleRequest
for individual operations.
但是有一個特殊情況,其中一個表單處理purchase
、transporataion
和sale
.我想結(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:
- 為表單定義表單請求
php artisan make:request StoreMultipleForm
從
StoreMultipleForm
上的rules
方法獲取每個其他表單請求的rules
,然后將它們一起返回,例如:
- Define your Form Request for your form
php artisan make:request StoreMultipleForm
From the
rules
method onStoreMultipleForm
fetch therules
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)!