I am making a request using ajax and laravel 9 to check the input of a field, but laravel returns the field error as error 422, which is not a big problem because I can work with it in ajax, but even using the error
function it shows up in the console. How can I make the error not show up in the console?
Laravel:
public static function changeUsername(Request $request){
$request->validate([
'username' => 'required|string|max:255|unique:users',
], [
'username.required' => 'O username não pode estar vazio',
'username.string' => 'O username não pode conter apenas números',
'username.max' => 'O username não pode ter mais de 255 caracteres',
'username.unique' => 'O username já está em uso',
]);
$user = User::find(Auth::user()->id);
$user->username = $request->username;
$user->save();
return response()->json([
'message' => 'Username alterado com sucesso!',
'user' => $user
], 200);
}
Ajax:
function saveUsername(username) {
$.ajax({
type: "POST",
url: "{{ route('user.updateUsername') }}",
data: {
username: username,
_token: "{{ csrf_token() }}"
},
success: function(response) {
$('#username').children().text(username);
showToast("Sucesso", "Username Alterado com Sucesso");
$('.usrChangeBtn[function="load"]').hide();
$('.usrChangeBtn[function="edit"]').show();
},
error: function(response) {
showToast("Erro", response.responseJSON.message);
$('.usrChangeBtn[function="load"]').hide();
$('.usrChangeBtn[function="edit"]').show();
},
beforeSend: function() {
$('.changeUsername').hide();
$('#username').show();
$('.usrChangeBtn[function="load"]').show();
}
});
}