15

is it possible, in Laravel 5, to validate multiple Requests in order to insert related models after a form submission?
I know how to validate multiple Model by using Validators but I want to do it with the Request Class.

Laravel 4 :

$validateUser = Validator::make(Input::all(), User::$rules);
$validateRole = Validator::make(Input::all(), Role::$rules);

if ($validateUser->fails() || $validateRole->fails()){
    $validationMessages = array_merge_recursive(
        $validateUser->messages()->toArray(),
        $validateRole->messages()->toArray()
    );
}

Laravel 5 :

Request one

class CreateUserRequest extends Request {

    public function rules()
    {
        //
    }
}

Request two

class CreateRoleRequest extends Request {

    public function rules()
    {
        //
    }
}

Controller Model call :

public function store(CreateUserRequest $request, CreateRoleRequest $request2)
{
    //
}

How can I validate the User input values and the Role input values using the Request approach? (and have a specific feedback if validation fails)

Community
  • 1
  • 1
Charles Follet
  • 827
  • 1
  • 10
  • 28
  • 1
    For sure I did. And as espected it doesnt work at all. I think we can't use 2 Request as parameter. Every input fields are attached to the first Request parameter. – Charles Follet May 25 '15 at 18:12
  • Doesn't work means only one request is validated? – lukasgeiter May 25 '15 at 18:13
  • 1
    Exactly. I think i can validate everything in one Request but I want to do it in a cleaner way by separating the validation. – Charles Follet May 25 '15 at 18:16
  • 3
    I just tried it and it actually worked with type hinting two request classes. (both `validate()` methods were called). I suppose in your real code you don't have the `CreateUserRequest` and `UserRequest` discrepancy? – lukasgeiter May 25 '15 at 20:42
  • Oh, yes sorry just edited. Can i see how you manage everything ? Like what do you have in your form, what do you have in your Requests and what do you have in your controller. A working example or i don't know. How does your Request know what input it has to check and what it hasn't? – Charles Follet May 26 '15 at 10:17
  • I only tested if `validate()` is called on both form requests. Maybe I'll do some more testing and show you my code later... – lukasgeiter May 26 '15 at 11:16
  • Many thanks Lukas, I can't wait for that. – Charles Follet May 26 '15 at 11:59
  • Okay, you were right. It actually works. – Charles Follet May 26 '15 at 16:41
  • Haha yay!! What was the problem? – lukasgeiter May 26 '15 at 16:42
  • I had two input with the same name : like user.name and role.name. So I never saw the second request working. (I had to rename the column in my DB, no other option ?) If you can provide a simple answer so I can mark it as resolved. Again, thank you very much Lukas ! – Charles Follet May 26 '15 at 16:45

1 Answers1

13

First, using multiple form request classes works perfectly fine.


Now of course you can't just have two forms in one. However what you can do to separate your data, is to use the array syntax for field names:

<input type="text" name="user[name]" />
<!-- and later -->
<input type="text" name="role[name]" />

In your validation rules you can then use the dot syntax to reference either the user name or the role name:

public function rules(){
    return [
        'role.name' => 'required'
    ];
}

And for creating the two models, just use this to get all the attributes for user and role:

$request->input('user'); // returns an array like ['name' => 'foo', 'other-user-field' => 'bar']
$request->input('role'); // returns an array like ['name' => 'baz', 'other-role-field' => 'boom']
lukasgeiter
  • 147,337
  • 26
  • 332
  • 270
  • Thanks, how do I change displayed name in attributes in `lang/en/validation.php`. When the name was just `user` I could add `'name' => 'Name',` but with arrays I can't figure out what to do. `'role.name' => 'Name'` does not work – dbr Aug 31 '15 at 10:34
  • 1
    I believe you can just add a nested array. So it would be `'role' => [ 'name' => 'Name' ]` – lukasgeiter Aug 31 '15 at 10:36
  • That did it. Also one more question. When I do validation in `public function rules(MyRequest $request, SecondRequest $request){ ... }` is it possible to triger both Requests at the same time? Now if validation don't pass in both Requests I only get errors for the first one: `MyRequest`, and `SecondRequest` only triggers after `MyRequest` passes. – dbr Aug 31 '15 at 10:59
  • @dbr I don't think that's possible. If you need more help, please ask a new question. – lukasgeiter Aug 31 '15 at 11:03