-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add checkout form validation to subscriptions.
- Loading branch information
1 parent
6491146
commit 7c98f1d
Showing
2 changed files
with
56 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
modules/ppcp-button/resources/js/modules/Helper/CheckoutFormValidation.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import Spinner from "./Spinner"; | ||
import FormValidator from "./FormValidator"; | ||
import ErrorHandler from "../ErrorHandler"; | ||
|
||
const validateCheckoutForm = function (config) { | ||
return new Promise(async (resolve, reject) => { | ||
try { | ||
const spinner = new Spinner(); | ||
const errorHandler = new ErrorHandler( | ||
config.labels.error.generic, | ||
document.querySelector('.woocommerce-notices-wrapper') | ||
); | ||
|
||
const formSelector = config.context === 'checkout' ? 'form.checkout' : 'form#order_review'; | ||
const formValidator = config.early_checkout_validation_enabled ? | ||
new FormValidator( | ||
config.ajax.validate_checkout.endpoint, | ||
config.ajax.validate_checkout.nonce, | ||
) : null; | ||
|
||
if (!formValidator) { | ||
resolve(); | ||
return; | ||
} | ||
|
||
formValidator.validate(document.querySelector(formSelector)).then((errors) => { | ||
if (errors.length > 0) { | ||
spinner.unblock(); | ||
errorHandler.clear(); | ||
errorHandler.messages(errors); | ||
|
||
// fire WC event for other plugins | ||
jQuery( document.body ).trigger( 'checkout_error' , [ errorHandler.currentHtml() ] ); | ||
|
||
reject(); | ||
} else { | ||
resolve(); | ||
} | ||
}); | ||
|
||
} catch (error) { | ||
console.error(error); | ||
reject(); | ||
} | ||
}); | ||
} | ||
|
||
export default validateCheckoutForm; |