Chooke Alhassan
Chooke Alhassan

Reputation: 31

Laravel form request messages not appearing when validation fails

my store method

the FormRequest

the validation is working and I get the confirm message in controller but when the validation fails I get no error messages any advice?

Upvotes: 1

Views: 1148

Answers (2)

Chooke Alhassan
Chooke Alhassan

Reputation: 31

 protected function failedValidation(Validator $validator)
{
    throw new HttpResponseException(response()->json([
        'errors' => $validator->errors(),], 403));
}

this worked for me, just needed to return the errors in json format

Upvotes: 1

Atif Mahmood
Atif Mahmood

Reputation: 390

You can use validation in controller like this, hopefully it will work for you

$validator = Validator::make($request->all(), [
        'id' => 'required|string|regex:/(^([A-Z]){2,4}_([0-1]){1}_([0-1]){1}_([0-9]){10})/u'
    ]);
    if ($validator->fails()){
        return (Arr::first(Arr::flatten($validator->messages()->get('*')));
    }
    else{
    //your code
    }

Upvotes: 1

Related Questions