I'm trying to create a multi-step form using jQuery AJAX and I want to validate each step with an ajax post request.
In the step 1 form, when the "go to step 2" button is clicked if there is a validation error in the console network tab it appears:
This is ok. But even this error appears the user goes to the "step 2" tab. Then, in the next step 2 form if the user clicks in "go back to step 1" the validation errors appear in the "#step1Errors" div.{success: false, errors: {buyer_name: ["The buyer name must be greater than 1 character."]}}
- errors:{buyer_name: ["The buyer name must be greater than 1 characters."]}
- success:false
- $('#goToStep2').on('click', function (event) {
- event.preventDefault();
- var custom_form = $("#" + page_form_id);
- $.ajax({
- method: "POST",
- url: '/product/storeUserInfo',
- data: custom_form.serialize(),
- datatype: 'json',
- success: function (data, textStatus, jqXHR) {
- setTimeout(function () {
- }, 3000);
- },
- error: function (data) {
- console.log(data);
- var errors = data.responseJSON;
- var errorsHtml = '';
- $.each(errors['errors'], function (index, value) {
- errorsHtml +=
- '<ul class="alert alert-danger mt-3">
- <li class=text-danger">' + value + '</li>
- </ul>';
- });
- $('#step1Errors').show().html(errorsHtml);
- }
- });
- });
- });