I have a generic AJAX form submit JS class:
$(function() {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$('.ajax-form').submit(function() {
var form = $(this);
var url = form.attr('action');
var data = form.serialize();
if (!form.hasClass('valid')) {
$.ajax({
url: url,
type: 'post',
data: data,
dataType: 'json',
success: function(result) {
$('.field').removeClass('has-error');
form.addClass('valid');
form.submit();
},
error: function(result) {
var errors = result.responseJSON;
$('.field').removeClass('has-error');
$.each(errors, function(key, value) {
var field = $('.field.' + key);
field.addClass('has-error').children('.error-message').text(value[0]);
});
}
});
return false;
}
});
});
This works great for AJAX validation on all my forms. I am now trying to implement this on the default "forgot password" form that is generated with the auth scaffold (resources/views/auth/passwords/email.blade.php
).
<form class="ajax-form" method="POST" action="{{ route('password.email') }}">
...
</form>
Although the AJAX validation is working here, the problem I am having is that when the validation passes, it is also performing the "forgot password" functionality. So basically it sends the reset password email twice (once during the AJAX submit and once during normal form submit).
I only want the AJAX submit to perform validation. If validation is successful it should (as it currently does) perform a normal form submit which will send the email.