Laravel 5-验证多个请求-一次执行所有请求 [英] Laravel 5 - Validate Multiple Request - do all Requests at the same time

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

问题描述

此处

如果我们有两个请求,例如:

If we have two Requests like:

public function store(FirstRequest $request, SecondRequest $request) { ... }

public function store(FirstRequest $request, SecondRequest $request) { ... }

是否可以同时运行两个请求而不是一个接一个地运行.这样,如果没有通过对FirstRequest的验证,则SecondRequest将不会启动,并且只有在FirstRequest通过且没有任何错误之后,它才会创建错误消息.

is it possible to run both Requests and not one after another. This way if validation doesn't pass for FirstRequest, SecondRequest won't start and it will create error messages only after FirstRequest passes without any errors.

推荐答案

我认为您可以手动创建验证器"

I think that you can "manually creating validators"

http://laravel.com/docs/5.1/validation#other-验证方法

基本上在您的方法中而不是使用Request注入,而是直接在该方法中使用规则,并为每组规则调用$ validator-> fails()方法.

Basically in your method instead of use a a Request injection, use the rules directly in the method and call the $validator->fails() method for every set of rules.

类似这样的东西:

public function store(Request $request){

    $rulesFirstRequest = ['field1' => 'required', 'field2' => 'required'];
    $rulesSecondRequest = ['field12' => 'required', 'field22' => 'required'];

    $validator1 = Validator::make($request->all(), $rulesFirstRequest);
    $validator2 = Validator::make($request->all(), $rulesSecondRequest);

    if ($validator1->fails() && $validator2->fails()) {
      //Do stuff and return with errors
   }
   // return with success
}

希望有帮助

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

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