Taha Zgued
Taha Zgued

Reputation: 1108

Continue to Controller even if request validation fails

I have this controller with its own Request Class and Rules to validation. I need it to enter the Controller even if the validation fails. I need to create the object even if it's wrong. But also send back the errors to the user.

 /**
 * Ebdn_gnl - Store
 * Store a newly created resource in storage.
 *
 * @param  \Ebdn\Http\Requests\EbdnGnlRequest $request
 * @return \Illuminate\Http\Response
 */
//public function store(Request $request)
public function store(EbdnGnlRequest $request)
{
    dump($request->all());
    return response("Api under construction", 200);
}

Upvotes: 3

Views: 869

Answers (2)

Taha Zgued
Taha Zgued

Reputation: 1108

Found it !!

in my Request Class I need to overwride the failedValidation function to make it merge the errors to the request (So I can access them in my Controller) instead of throwing error.

class EbdnGnlRequest extends FormRequest {

protected function failedValidation(Validator $validator)
{
    $this->merge(['errors' => $validator->errors()]);
}

Upvotes: 4

Mario Inostroza
Mario Inostroza

Reputation: 766

For that you can put a message inside your Request, for each rule:

public function message(){}

Good luck!

Upvotes: 0

Related Questions