I'm working on updating data with file upload using ajax and every time validation error says that all fields are required although the fields are not empty.
this is my js Code:
function save() {
var data = new FormData($('.form'));
$.ajax({
url: ajaxUrl,
method : ajaxMethod,
data: data,
processData: false,
contentType: false,
}).done(...).fail(function(response) {
console.log(response.responseText);
});
}
and laravel controller method is:
public function saveCountry(CountriesRequest $request)
{
if ($request->ajax()) {
$id = $request->id ?? null;
// updating
if ($id) {
$country = Country::find($id);
// if the user didn't update the icon
if ($request->icon == ''){
$country->update($request->except(['icon']));
}
else {
$countryIcon = $country->icon;
$country->update([
'name_en' => $request->name_en,
'name_ar' => $request->name_ar,
'code' => $request->code,
'symbol' => $request->symbol,
'icon' => Up::imageUpload($request, 'icon', 'public/countries/icon', true),
]);
// dlete old icon
Storage::delete($countryIcon);
}
}
// new country
else {
Country::create([
'name_en' => $request->name_en,
'name_ar' => $request->name_ar,
'code' => $request->code,
'symbol' => $request->symbol,
'icon' => Up::imageUpload($request, 'icon', 'public/countries/icon', true),
]
);
}
}
}
Notice : Up: is a custom class for uploading files
the error message is:
"{"message":"The given data was invalid.","errors":{"name_en":["The Name in English field is required."],"name_ar":["The Name in Arabic field is required."],"code":["The Code field is required."],"symbol":["The Symbol field is required."]}}"
the creating request is working very well but the update request not working, I checked laravel code and I think it's ok