I am submitting a user registration form to a controller method using Ajax. The errors returned are displayed on the page using JQuery in Ajax. Every other validation error is being displayed fine but the error for unique:users
doesn't display. When the same email is entered for the second time, the form doesn't submit indicating that validation works but the error message doesn't get displayed.
Here is the controller method that receives the data from ajax:
public function store(Request $request){
$rules = [
'name' => 'required',
'email' => 'required|unique:users|min:8',
];
$messages = [
'name.required' => 'name is required',
'email.required' => 'Email is required',
'email.unique' => 'That email address is already taken',
];
$this->validate($request, $rules, $messages);
$user = User::create([
'name' => $request->input('name'),
'email' => $request->input('email'),
]);
Auth::login($user);
}
Here is the ajax code that submits data from form:
$('#registration_form').submit(function (e) {
e.preventDefault();
var name = $('#name').val();
var email = $('#email').val();
$.ajax({
url: 'store',
type: 'post',
datatype: 'json',
data: {
'name' : name,
'email' : email,
},
success: function(msg){
notify('Your account was successfully created.');
},
error: function(data){
$('#name_error').html(data.responseJSON.name);
$('#email_error').html(data.responseJSON.email);
}
});
Code for the form and the route is not included in my question 'cause I have checked it again and again and everything (ids, name attributes etc) is correct so i see no need boring you with a lengthy question.
I expect to see the error message That email address is already taken
on a section with id email_error
as specified in my validation messages but it doesn't display.
Please what am I doing wrong?
Thanks for help..
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire