Laravel form request messages not appearing when validation fails

1k views Asked by At

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?

2

There are 2 answers

2
Atif Mahmood On

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
    }
1
Chooke Alhassan On
 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