Skip to Content Skip to Menu

Documentation for client-side validation API?

1 hour 21 minutes ago - 1 hour 8 minutes ago #343823 by BobBriscoe
Replied by BobBriscoe on topic Documentation for client-side validation API?
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.


 
Last edit: 1 hour 8 minutes ago by BobBriscoe. Reason: Reformatted code snippets (yet again)

Please Log in or Create an account to join the conversation.

  • krileon
  • krileon
  • ONLINE
  • Posts: 50374
  • Thanks: 8622
  • Karma: 1472
27 minutes ago #343825 by krileon
Replied by krileon on topic Documentation for client-side validation API?
What you keep linking isn't just validation. It's an entire field type with a custom dropdown, styling, and other JavaScript unrelated to validation. You should ideally just be making a new field type plugin for either Joomla or CB that includes everything you need and then just use that field type. Some examples of field type plugins are CB Code Field and CB Query Field.

Below is a fully working example of injecting a new validation rule into CB, but that's just a validation rule. It just takes an inputs value and compares it returning true/false based off whatever conditions are required. That's what a validation rule is.

Global
Triggers: onAfterRegisterFormDisplay, onAfterUserProfileEditDisplay
Type: Code
User: Automatic
Access: Everybody
Action
Method: jQuery
Code:
Code:
$.validator.addMethod( 'isTea', function( value, element, params ) { return this.optional( element ) || ( value === 'Tea' ); }, $.validator.format( 'Please enter: Tea' ) ); $( 'input#username' ).attr( 'data-rule-istea', 'true' );

You can confirm this yourself by creating that auto action then editing the username field and it will error if the username isn't set to "Tea". That's all the validation is meant to do. You can even add is-valid here if you like by adding it to element if the value is valid.

The only additional validate options that I need are shown here (using my code that doesn't work in the jQuery environment):

You're overriding the initialization of the form validation that CB itself does. So you've lost all of the behavior CB Validate provides. We provide a dozen different jQuery events to hook into CB Validate for you to add whatever customized display logic you like. Example as follows.

Global
Triggers: onAfterRegisterFormDisplay, onAfterUserProfileEditDisplay
Type: Code
User: Automatic
Access: Everybody
Action
Method: jQuery
Code:
Code:
$( '.cbValidation' ).on( 'cbvalidate.highlight', ( e, cbvalidate, element ) => { $( element ).removeClass( 'is-valid' ); }).on( 'cbvalidate.unhighlight', ( e, cbvalidate, element ) => { $( element ).addClass( 'is-valid' ); });

The above will work for both registration and profile edit on frontend. This is a working example. So you should see is-valid added to any input that passes validation check. You can find those events and more in the below file.

/components/com_comprofiler/js/jquery/jquery.cbvalidate.js

They're all triggered on the root form element so you just need to bind to them from there.


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in or Create an account to join the conversation.

17 minutes ago - 6 minutes ago #343826 by BobBriscoe
Replied by BobBriscoe on topic Documentation for client-side validation API?
Please reread the "why I'm trying to do this" at the end of my previous post.

I would not have spent 3 weeks trying to do this, unless I really wanted to do it. The work that went into the intlTelInput is immense, so I want to integrate it into CB. I have got it all working with CB except for the need to add one class. It is not useful to be told to do something else instead of what I am trying to do - especially something I have nearly completed except for one final step. Please try to answer my question.

[EDIT]: It's possible that the second half of your response does nearly answer my question. I'll try to integrate that into the rest of my code, and let you know.
 
Last edit: 6 minutes ago by BobBriscoe.

Please Log in or Create an account to join the conversation.

Moderators: beatnantkrileon
Powered by Kunena Forum