Reputation: 3327
I am trying to display the error msg but it's not displaying. I can see the error msg in the console (Network tab) but nothing in the view. Thanks in advance.
the output from the API.
{"message":"The given data was invalid.","errors":{"title":["The title field is required."]}}
controller:
public function store(Request $request)
{
$request->validate([
"title"=> "required"
]);
try {
if ($request->ajax()) {
$request->merge(['created_by_user_info_id' => Auth::user()->id]);
SavingTypes::create($request->all());
$data['status'] = true;
$data['title'] = null;
$data['message'] = 'Added Successfully.';
}
} catch (\Exception $e) {
$data['status'] = false;
$data['message'] = 'Something went wrong. please try again';
$data['error'] = $e->getMessage();
}
return $data;
}
View:
<h1>{{ $errors->first('title') }}test</h1> // only the "test" is visible and theres no error msg although I can see the error msg in the console (Network tab)
<input type="text" class="form-control" placeholder="title" name="title" >
Upvotes: 0
Views: 66
Reputation: 2509
If request send through AJAX then you can't access like
$errors->first('title')
You will get errors inside your JavaScript
$.ajax({
// some code
error: function(jqXHR, textStatus, errorThrown) {
// Here you will get errors
console.log(textStatus, errorThrown);
}
});
Upvotes: 1