CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • jQuery Form Validation??
Pages: [1]

Author Topic: jQuery Form Validation??  (Read 1463 times)

awasson

  • I post frequently
  • ***
  • Posts: 230
  • Karma: 7
  • Living in a world of Drupal / CiviCRM
    • My Company: Luna Design
  • CiviCRM version: Latest
  • CMS version: Drupal 6/7/8
  • MySQL version: 5.x
  • PHP version: 5.3.x
jQuery Form Validation??
June 04, 2014, 03:10:19 pm
Does anyone know what method of jQuery or scripted validation is being used in CiviCRM 4.3.x, 4.4.x?

I'm tinkering with an extension that has a custom (non-formbuilder) form on it and although I was really happy to see that simply adding required=required to a field adds fancypants client side validation, I would like to add a new method and then add that as a rule.

So for example somewhere in my template I would like to add the custom method:

Code: [Select]
cj.validator.addMethod("isValidDate", function (value, element) {
    . . . A bunch of code
}

Then I want to add the rule:
Code: [Select]
cj('.hasDatepicker').rules("add", "isValidDate");

This would work usually with jQuery.validate but so far I'm not having any success. Any ideas?

Thanks,
Andrew
My CiviCRM Extension Workshop: https://github.com/awasson

mathieu

  • Administrator
  • Ask me questions
  • *****
  • Posts: 620
  • Karma: 36
    • Work
  • CiviCRM version: 4.7
  • CMS version: Drupal
  • MySQL version: MariaDB 10
  • PHP version: 7
Re: jQuery Form Validation??
June 04, 2014, 03:51:16 pm
Hi,

It's using standard jquery-validate. I'm not very familiar with the internals of how it works, I'm not sure about the best practices, but for example, on a contribution page, I use:

Code: [Select]
cj('#is_recur').on('click', function() {
  if (cj(this).is(':checked')) {
    cj('#priceset .other_amount-content input.form-text').rules('add', {
      max: 25,
      messages: {
        max: ts('Donations in a fiscal year may not exceed $300. Monthly donations are therefore limited to $25.')
      }
    });
  }
  else {
    cj('#priceset .other_amount-content input.form-text').rules('remove', 'max');
  }
});

NB: since the contribution page does not enable jquery-validate by default, I do copy part of templates/CRM/Form/validate.tpl in my Main.extra.tpl.
CiviCamp Montréal, 29 septembre 2017 | Co-founder / consultant / turn-key CiviCRM hosting for Quebec/Canada @ SymbioTIC.coop

awasson

  • I post frequently
  • ***
  • Posts: 230
  • Karma: 7
  • Living in a world of Drupal / CiviCRM
    • My Company: Luna Design
  • CiviCRM version: Latest
  • CMS version: Drupal 6/7/8
  • MySQL version: 5.x
  • PHP version: 5.3.x
Re: jQuery Form Validation??
June 04, 2014, 03:58:24 pm
Thanks mathieu,

I'll give that a try. For some reason even though my form doesn't use formbuilder if I include required = required in a field, it is validated and the messaging UI eye-candy is quite nice. Much nicer than the typical out-of-the-box jQuery messaging.

Anyway, it looks like you've added your rule as a result of a click so I think I'll do the same and either use clicking the submit button or maybe a change of a field.

Thanks again.

Cheers,
Andrew
My CiviCRM Extension Workshop: https://github.com/awasson

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: jQuery Form Validation??
June 11, 2014, 02:31:12 pm
CiviCRM's implementation of jQuery validate was done with expansion in mind. If you take a look at templates/CRM/Form/validate.tpl you'll see that extra params and functions may be added to the CRM.validate object. An example of doing so can be found in templates/CRM/Contribute/Form/Contribution/PremiumBlock.tpl.
Try asking your question on the new CiviCRM help site.

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • jQuery Form Validation??

This forum was archived on 2017-11-26.