Skip to content

Documentation is missing how to reset the form validation #25626

@burzum

Description

@burzum

I haven't found a solution... What annoys me the most is that the documentation does not show how to reset or disable validation. I shouldn't have to search the web for this common use case.

I want to clear the form after submission but the validation immediately jumps in and validates the now empty form fields again, resulting in showing errors.

	function formValidation() {
		// Validation -> https://getbootstrap.com/docs/4.0/components/forms/#validation
		// Fetch all the forms we want to apply custom Bootstrap validation styles to
		var forms = document.getElementsByClassName('needs-validation');
		// Loop over them and prevent submission
		var validation = Array.prototype.filter.call(forms, function (form) {
			form.addEventListener('submit', function (event) {
				if (form.checkValidity() === false) {
					event.preventDefault();
					event.stopPropagation();
					$(form).addClass('is-invalid');
				} else {
					$(form).addClass('is-valid');
				}
				form.classList.add('was-validated');
			}, false);
		});
	}

	function contactForm() {
		// Honeypot
		let honeypot = $('#contact-website');
		let contactForm = $('#contact-form');

		honeypot.parent('.form-group').hide();
		honeypot.removeAttr('required');

		contactForm.on('submit', (event) => {
			event.preventDefault();

			if (!contactForm.hasClass('is-valid')) {
				return;
			}

			$.ajax({
				type: 'POST',
				url: 'contact.php',
				data: contactForm.serialize(),
				success: () => {
					$('#contact-form input').val('');
					$('#contact-form textarea').val('');
					$('#modal').modal('show');
				}
			});
		});
	}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions