Laravel 5.5-同时验证多个表单请求 [英] Laravel 5.5 - Validate Multiple Form Request - at the same time

查看:254
本文介绍了Laravel 5.5-同时验证多个表单请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该问题已经被问过此处是laravel的先前版本,尚未得到答复.

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

我有一个html form,它使用三个不同的Form Request Validations进行了验证.我能够做到这一点.但是,问题是,表单验证是一个接一个地进行的.不在同一时间.

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.

如果第一个表单请求引发验证错误,则该表单将返回到view,因此不会评估其余两个表单,因此无法向用户显示适当的验证错误.

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.

我想要的是:同时使用三个表单验证请求rules验证表单.

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:

对于purchasetransporataionsale,我确实有三种单独的形式,分别使用PurchaseRequestTransportationRequestSaleRequest对其进行单独计算.

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

但是在特殊情况中,单个表单处理purchasetransporataionsale. 我想通过合并三个表单请求规则来验证表单,因为我不想再次编写相同的验证规则.

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.

注意:单独形式和组合形式的字段相同.

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

谢谢..

推荐答案

当验证失败时,FormRequest引发Illuminate\Validation\ValidationException异常,该异常具有

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

您可以通过在try/catch块中的控制器中手动运行Form Requests来实现所需的行为,该try/catch块可捕获错误并在重定向之前合并错误包,或者是否有必要通过Laravel将其注入到您的设备中来运行它们控制器,那么您将需要添加自己的异常处理程序,该异常处理程序将捕获所有错误,将其合并,然后在最终的表单请求运行后进行重定向.

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.

但是,值得注意的是,这两种方法都不是很好:它们很麻烦,并且会给您带来比解决方案更多的问题.如果您想编写一个可维护的应用程序,则应尽量遵循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.

存在用于验证表单的表单请求,因此,每个表单都应有一个表单请求,如果您希望根据不同的规则集来构成表单请求,则应在表单请求中完成,例如:

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. 定义表单请求
  3. StoreMultipleForm上的rules方法获取每个其他Form Requests的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.
    }
    

  • 此方法的优点是它是独立的,符合一个表单请求的要求,不需要更改要合并的表单请求,如果您您需要添加此表单唯一的其他规则,而无需创建另一个表单请求.

    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.

    这篇关于Laravel 5.5-同时验证多个表单请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆