Laravel 5 - Validate Multiple Request - do all Req

2019-07-07 19:38发布

Continuing discussion from here

If we have two Requests like:

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

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.

1条回答
神经病院院长
2楼-- · 2019-07-07 20:07

I think that you can "manually creating validators"

http://laravel.com/docs/5.1/validation#other-validation-approaches

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.

Something like this:

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
}

Hope it helps

查看更多
登录 后发表回答