I'm trying to get laravel error messages by ajax so that i can show it using sweet alert, till now i'm able to do it when i'm having the code in my controller like this :
// basic validation rules
$v = validator($request->all(), [
'title' => 'required|min:1',
'content' => 'required|min:2',
'keywords' => 'required|min:1',
'image_name' => 'required|image|mimes:jpeg,jpg,png,gif|max:20000',
], [
'title.required' => Config::get('app.locale') == 'en' ? 'please enter a title' : 'برجاء ادخال العنوان',
'title.min' => Config::get('app.locale') == 'en' ? 'title must be at least one word' : 'العنوان يجب الا يقل عن كلمه واحده ',
'content.required' => Config::get('app.locale') == 'en' ? 'please enter a content' : 'برجاء ادخال المحتوي',
'content.min' => Config::get('app.locale') == 'en' ? 'content must be at least two words' : 'المحتوي يجب ان يكون كلمتين عالاقل ',
'keywords.required' => Config::get('app.locale') == 'en' ? 'please enter a keyword' : 'برجاء ادخال الكلمه الدلاليه',
'keywords.min' => Config::get('app.locale') == 'en' ? 'please enter at least one keyword' : 'برجاء ادخال كلمه دلاليه واحده علااقل',
'image_name.required' => Config::get('app.locale') == 'en' ? 'please select an image' : 'برجاء اختيار صوره ',
'image_name.mimes' => Config::get('app.locale') == 'en' ? 'image type should be : jpeg,jpg,png,gif ' : 'نوع الصوره يجب ان يكون : jpeg,jpg,png,gif',
'image_name.max' => Config::get('app.locale') == 'en' ? 'image must be smaller than 20MB' : 'الصوره يجب ان لا تزيد عن 20 ميجا بايت'
]);
// if the validation has been failed return the error msgs
if ($v->fails()) {
return ['status' => false, 'data' => implode(PHP_EOL, $v->errors()->all())];
}
but when I move the messages to the request file it responses with 422 http status code and I can't get the response using ajax so can anyone help me to retrieve the response from the request file in ajax instead of using it in controller ?