I have now been trying to get my validation script to integrate with CB for nearly 3 weeks without success. It took a few hours to write the original JS code and get it working (without any integration into CB), so this is extremely frustrating.
I think I've worked out what the problem is, but after more hours of digging, I still can't find a solution: I have been trying to define options using validate() on my form, but the
jquery.cbvalidate.js script has already initialized
validate() on the form, and I can't initialize it a second time. This appears to be working from the JS environment, because my script usually wins the race against cbvalidate to initialize the form first. However, from the CB Auto Actions/jQuery method, cbvalidate always wins, which is why my efforts never work in that environment.
What is the proper way to extend a few options of validate() that have already been initialized by the
jquery.cbvalidate.js script?
The only additional validate options that I need are shown here (using my code that doesn't work in the jQuery environment):
Code:
const formElement = document.querySelector('form.cbValidation');
const validator = $(formElement).validate({
errorElement: "div",
errorClass: "invalid-feedback",
validClass: "is-valid"
});
I have tried
Code:
$.extend(validator.settings, { ... });
And I have tried setting the
errorElement and extending the class options by manipulating
validator.settings.errorClass or
validator.settings.validClass manually. But all to no avail.
Background (why I need to do this)
I am trying to use the validation of
intlTelInput
on fields for phone number input. It validates phone numbers against 240 country-specific standards, and it allows me to configure it so that the user can enter a phone number in the various national formats that most people in any particular country recognize (with parentheses, spaces, dashes, etc), rather than requiring them to understand international phone number formatting. It also gives country-specific placeholders in each national format. I want this because a correct phone number is essential for subsequent stages of the site's booking process.
cbvalidate toggles the
is-invalid class, whereas intlTelInput needs both
is-valid and
is-invalid to toggle. When I successfully register
validClass: "is-valid", the border of any field containing a valid value goes green and a big green tick appears within the field. But this never happens when using the CB Auto Actions jQuery Method (I believe for the reason given above).
The rendering of invalid fields and their error messages all works correctly, whether or not my validate options register. Except, if my registration of
validClass: "is-valid" succeeds, I need the other two options to keep any error messages in the correct location and colour.