Is it possible to share validation failures between nested controllers?

Asked

Viewed 25 times

0

I have two models, one child of the other, User and Address where User>hasOne>Address and I’m about to do the following:

Request:

[
  'User' => [ 
    'name' => 'fulano', 
    'etc' => 'etc' 
  ],

  'Address' => [ 
    'street' => 'rua x', 
    'etc' => 'etc' 
  ]
]

Usercontroller:

  • Validates the data in ['User']
  • Dá create no User
  • Calls the AddressController and passing on the data of ['Address']

Addresscontroller:

  • Validates the data in ['Address']
  • Dá create no Address

But I’m not finding a way in case the validation fails in both controllers i manage to return to the user all fields that failed.

I did it once with try-catch, but the problem is that if the validation fails in UserController it closes there and does not validate the address, not showing if there is something wrong, making the user may have to correct what he wrote twice, one to correct his data and another later to correct the address because the system did not inform that the address was also wrong.

So what happens to me:

  • Is there any way I don’t need to use the try-catch and be able to pick up all the address gaps and move on to somewhere to treat?
    (I heard something about dispatcher or something like that, but I don’t know if it’s something to use here)

I could validate everything on UserController, but I’d have a gut and in case I needed to make a update Address would need to have address validation on AddressControler;

How to get out of this?

Edit: An example of how I was doing (I couldn’t find the original)

// User Controller
public function store($data){
    try {
        $this->validate($data['user']);
        $user = User::create($data);

        // Chama o Store de Address e passa os valores
        $address = (new AddressController())->store($user, $data);

        return $user;
    } catch(ValidationFailException $e) { 
        // Aqui captura a primeira validação que falhar
        Session::flash('fails', $ex->getFails());
        return Redirect::back();
    }
}

// Address Controller
public function store($data){
    try {
        $this->validate($data['address']);
        $address = new Address($data);
        $address->user()->associate($user);
        $address->save();

        return $address;
}

// Ambos os controladores tem um método validador nesse formato
public function validate($data)  {
    $validator = Validator::make($data, [
        'campo' => 'required',
    ]);

    if($validator->fails()){
        throw new ValidationFailException('As seguinter validações falharam', $validator->messages()->toArray());
    }
}
  • Well I would do everything in the same controller and in the form would use the respective fields validate all and then save separately. I don’t know what the difficulty is, but I think the controller is unnecessary. Your implementation can post on the question?

  • It was just a simple example, I need the controller because I have to edit in separate parts, pick up some patterns with lat/long and some stops with photos, I separated more so that it doesn’t become a giant file. Worst that I have no implementation, I have only one old as said above (this was unnecessary) with Try-catch, put as soon as I find...

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.