I am validating a form to a route. On success of validation I want to submit the form to another route so as to pull the next page in portal. Is this the correct way of doing it? It doesn't feel right.
I've tried handling the validation on button click, but that immediately submits form even if error.
Currently, I have 'action' & 'method' attributes on form along with a knockout submit attribute to handle the the validation. This is where I feel off.
In the proceeding code you'll see that I am running the .validate() on success whereas before I had ran .submit()
- Validation Method (handleProceed) *
self.handleProceed = function() {
$.ajax({
type: "POST",
url: "/apps/o365/validate_address",
dataType: "json",
cache: false,
}).always( function(jqXHR, textStatus) {
if ( textStatus != "success" || jqXHR.eligible == false ) {
$("#proceedToPurchase").removeClass("disabled processing").text("Proceed to Purchase");
$("#proceedToPurchaseModal").modal("show");
} else if ( textStatus === "timeout" ) {
alert("got timeout");
$('#proceedToPurchase').addClass("disabled");
} else {
$("#proceedToPurchase").addClass("processing");
$("#o365Form").validate();
}
});
};
- How I am handling the Form Attributes *
<form id="o365Form" data-bind="attr: { action : purchaseURL }, submit: handleProceed" method="POST">
- How the button on the Form is running *
<button
id="proceedToPurchase"
type="submit"
class="btn btn-secondary btn-block py-4 font-20"
data-bind="attr: { disabled: !formIsFilled() }"
>
Expected:
- Button is clicked
- handleProceed() runs and validates
- THEN form is submitted to pull next page