0

Hi I have a controller that inserts multiple values into 2 tables from 1 form, I can validate them but only one at a time. Only If I click on the button to insert it shows the erros of the second validation.

public function store(CreateRequest $request, RegisterRequest $request2)
{
$input = Input::all();
$validation = Validator::make($input, $request->rules());
$validation2 = Validator::make($input, $request2->rules());
if ($validation->passes() and $validation2->passes())
{
  Users::create($input);
  Register::create($input2);
  return Redirect::route('users.main')
  ->withSuccess('Created');
 }

 return Redirect::route('users.create')
 ->withInput()
 ->withErrors();  
}
5
  • And what is your question? Commented Dec 19, 2016 at 12:48
  • withErrors would only return the latest errors. Commented Dec 19, 2016 at 12:52
  • My question is how do I show the multiple erros, since I have 2 different request and rules. Commented Dec 19, 2016 at 12:57
  • If I hit the button create it show the first validation erros if I pass the first validation the second error message show's and I want it to show at the same time Commented Dec 19, 2016 at 13:03
  • brother, you are totally on a wrong path Commented Dec 19, 2016 at 13:09

2 Answers 2

1

you are doing it totally wrong. a request is a single request. though laravel supports request inside request in this scenario what i see is that you are trying to register a user into the system. do it like this.

create one request. say just RegistrationRequest and put all the form fields and the rules in it. use that request only to store the user like this.

public function store(RegisterRequest $request)
{
    $data = $request->all();
    $validation = Validator::make($data, $request->rules());

    if ($validation->passes()) {
       $user = User::create([
          'username' => $request->username,
          'name' => $request->name
       ]);
       $register = Register::create([
         'blabla' => $request->name,
         'username' => $user->username
       ]);
       return Redirect::route('users.main')->withSuccess('Created');
    }
    return Redirect::route('users.create')->withInput()->withErrors();  
}

this is just a demonstration as per the actual problem you are facing is that you are trying a wrong approach, there are another ways to do this.and also validation errors are thrown and redirected to the create view like automatically. so you don't have to do it on your own

Sign up to request clarification or add additional context in comments.

2 Comments

Yep I thought of this aproach and maybe it's the best one. I was going with 2 Request because I thought I could reuse when I want to insert at one table, so I would have the validation of table 1 on the request 1 and the validation of the table 2 at the 2 request.
I agree, with the Laravel Validator facade, you have some greatful validation like required_if to make your validations depending on more that one table :)
0

You can merge the messages of validators and send them back as:

$errors = $validation->errors()->merge($validation2->errors())

return Redirect::route('users.create')->withInput()->withErrors($errors);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.