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) »
  • in-browser validation
Pages: [1]

Author Topic: in-browser validation  (Read 1180 times)

JoeMurray

  • Administrator
  • Ask me questions
  • *****
  • Posts: 578
  • Karma: 24
    • JMA Consulting
  • CiviCRM version: 4.4 and 4.5 (as of Nov 2014)
  • CMS version: Drupal, WordPress, Joomla
  • MySQL version: MySQL 5.5, 5.6, MariaDB 10.0 (as of Nov 2014)
in-browser validation
May 07, 2013, 11:38:09 am
I was recently looking to see if a site used CiviCRM - which it didn't - and came across some slick looking html for in-browser validation. I'm not sure if this is standard WordPress stuff or not (see view-source:https://contribute.ontarioliberal.ca/Home/Donate). It seems like it might be something that we could define for fields in xml/schema and then not only use on server-side validation but also on the client-side.

For example:

<div class="element">
            <label for="prefix">Salutation</label>
            <select data-val="true" data-val-regex="Valid Prefix Required" data-val-regex-pattern="(Mrs.|Ms.|Mr.|Dr.)" data-val-required="The prefix field is required." id="prefix" name="prefix"><option value="">select</option>
<option value="Mr.">Mr.</option>
<option value="Ms.">Ms.</option>
<option value="Mrs.">Mrs.</option>
<option value="Dr.">Dr.</option>
</select>
            <span class="field-validation-valid" data-valmsg-for="prefix" data-valmsg-replace="true"></span>
        </div>
        <div class="element">
            <label for="first_name">First Name</label>
            <input class="text-box single-line" data-val="true" data-val-required="First Name Required" id="first_name" name="first_name" type="text" value="" />
            <span class="field-validation-valid" data-valmsg-for="first_name" data-valmsg-replace="true"></span>
        </div>
....
<div class="element">
            <label for="postal_code">Postal Code</label>
            <input data-val="true" data-val-regex="Valid Canadian Postal Code Required (No Extra Whitespaces)" data-val-regex-pattern="^[KkLlMmNnPp]\d[A-Za-z]\s?\d[A-Za-z]\d$" data-val-required="Postal Code Required" id="postal_code" name="postal_code" type="text" value="" />
            <span class="field-validation-valid" data-valmsg-for="postal_code" data-valmsg-replace="true"></span>
        </div>
        <div class="element">
            <label for="telephone">Primary Telephone</label>
            <input data-val="true" data-val-regex="Valid Format Required. Ex. 555-555-5555" data-val-regex-pattern="(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}" data-val-required="Telephone NumberRequired" id="telephone" name="telephone" type="text" value="" />
            <span class="field-validation-valid" data-valmsg-for="telephone" data-valmsg-replace="true"></span>
        </div>
        <div class="element">
            <label for="email">Email Address</label>
            <input data-val="true" data-val-regex="Valid Email Format Required (no caps)" data-val-regex-pattern="((([a-z]|\d|[!#\$%&amp;&#39;\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&amp;&#39;\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$" data-val-required="Email Address Required" id="email" name="email" type="text" value="" />
            <span class="field-validation-valid" data-valmsg-for="email" data-valmsg-replace="true"></span>
        </div>
Co-author of Using CiviCRM https://www.packtpub.com/using-civicrm/book

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: in-browser validation
May 07, 2013, 01:04:01 pm
Well, I like the idea of data-xxx to specify better the type of field, but html 5 has it covered in a nicer way, eg. <input type="email">

http://diveintohtml5.info/forms.html

The regex in the html ain't going to convert me... and now you have two problems ;)

I've used jquery plugins before that have mostly the same approach, but use classes instead of data. I think I prefer data, but the class has the benefit of making it easier to style (eg. different color for mandatory or emails or...)

http://docs.jquery.com/Plugins/Validation

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: in-browser validation
May 07, 2013, 01:42:57 pm
As of 4.2 we ship with the jQuery validate plugin. As of 4.3 we actually use it :)
Still haven't enabled it on every form, but doing so is fairly trivial.
Try asking your question on the new CiviCRM help site.

JoeMurray

  • Administrator
  • Ask me questions
  • *****
  • Posts: 578
  • Karma: 24
    • JMA Consulting
  • CiviCRM version: 4.4 and 4.5 (as of Nov 2014)
  • CMS version: Drupal, WordPress, Joomla
  • MySQL version: MySQL 5.5, 5.6, MariaDB 10.0 (as of Nov 2014)
Re: in-browser validation
May 07, 2013, 02:07:35 pm
It would be amazing if we could specify validations beyond just required and field type as part of custom field definitions in the browser. Would be very cool if these could work on both server and client side automatically.

It was actually the regex, X+, that was most exciting for me ;). The reason was it suggested that it would be possible to write once and run on both client and server. I imagine there is a library that translates something like validEmail into that regex.

Coleman, is the jQuery stuff being configured to run (somehow via a few layers) based on field definitions in the xml schema spec? If so, then we might be able to provide nice validation functionality easily to extension authors assuming we start running GenCode to generate DAO for extensions as well.
Co-author of Using CiviCRM https://www.packtpub.com/using-civicrm/book

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: in-browser validation
May 07, 2013, 02:13:15 pm
Quote
Coleman, is the jQuery stuff being configured to run (somehow via a few layers) based on field definitions in the xml schema spec?
No, but adding validation rules into the schema metadata is a very good idea.
PS To see the current behavior in action, try editing a contact's email or website (via inline edit) and enter an invalid address.
Try asking your question on the new CiviCRM help site.

JoeMurray

  • Administrator
  • Ask me questions
  • *****
  • Posts: 578
  • Karma: 24
    • JMA Consulting
  • CiviCRM version: 4.4 and 4.5 (as of Nov 2014)
  • CMS version: Drupal, WordPress, Joomla
  • MySQL version: MySQL 5.5, 5.6, MariaDB 10.0 (as of Nov 2014)
Re: in-browser validation
May 07, 2013, 04:40:18 pm
This is very good.

Assuming we can come up with reasonable server side validation that replicates jQuery validate functionality then it would make sense to put the additional validations in the metadata for the field.
« Last Edit: May 07, 2013, 07:48:06 pm by JoeMurray »
Co-author of Using CiviCRM https://www.packtpub.com/using-civicrm/book

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: in-browser validation
May 07, 2013, 04:50:58 pm
There are already quite a few rules that we have in php that also exist in jQuery validate. So if our metadata looked something like this then the form builder could add both the proper css class (for jQuery validate to pick up) and the form rule.
<validationRule>email</validationRule>
Try asking your question on the new CiviCRM help site.

JoeMurray

  • Administrator
  • Ask me questions
  • *****
  • Posts: 578
  • Karma: 24
    • JMA Consulting
  • CiviCRM version: 4.4 and 4.5 (as of Nov 2014)
  • CMS version: Drupal, WordPress, Joomla
  • MySQL version: MySQL 5.5, 5.6, MariaDB 10.0 (as of Nov 2014)
Re: in-browser validation
May 07, 2013, 07:51:04 pm
Nice approach for simpler rules without parameters, which is the place to start. If you create a wiki page listing the supported rules I will try to help fill it out.
Co-author of Using CiviCRM https://www.packtpub.com/using-civicrm/book

andersiversen

  • I post occasionally
  • **
  • Posts: 76
  • Karma: 1
  • CiviCRM version: 4.4.4
  • CMS version: Drupal 7.26
  • MySQL version: 5.5.32
  • PHP version: 5.3.10
Re: in-browser validation
September 23, 2013, 07:23:30 am
It would be good if it also was possiple to add validation to custom made edit in-place fields.
For example if I have a view displaying e-mail fields for contacts, and I enable edit in-place for this field (as explained in the wiki), there's currently no validation for that field - I can enter whatever I want in that field.

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • in-browser validation

This forum was archived on 2017-11-26.