沃梦达 / 编程问答 / php问题 / 正文

Laravel 5.5 - 同时验证多个表单请求

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

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

问题描述

问题已经问过了此处 用于先前版本的 laravel,但尚未回答.

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

我有一个 html 表单,它使用三种不同的 表单请求验证 进行验证.我有能力做到这一点.但是,问题是,表单验证是一一进行的.不是同时.

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.

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

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 三个单独的表格,它们分别使用 PurchaseRequest 进行评估,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.我想结合使用三个表单请求规则来验证表单,因为我不想再次编写相同的验证规则.

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.

谢谢..

推荐答案

A FormRequest throws an IlluminateValidationValidationException Exception when validation failed which has a redirectTo 方法,然后是 异常 Handler 执行重定向.

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 块中的控制器中手动运行表单请求来实现您想要的行为,该块捕获错误并在重定向之前组合错误包,或者如果您必须通过 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. 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.
    }
    

  • 这种方法的优点是它是自包含的,它符合一个表单一个表单请求的期望,不需要更改您正在合并的表单请求,如果您需要为此表单添加额外的规则,您无需创建另一个表单请求.

    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 - 同时验证多个表单请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

    本文标题为:Laravel 5.5 - 同时验证多个表单请求

    基础教程推荐