Problem/Motivation
I've been banging my head against the wall trying to find out why Clientside Validation works on all my fields except my select lists. I originally thought it had to do with the special handling of "_none" as discussed and fixed here. Then I realized that it has to do instead with the Chosen Plugin For Drupal.
Chosen works by hiding the basic form
element and adding a
The problem is that Clientside Validation ignores hidden fields by default. But in the infinite wisdom of the module creators there is an option to turn on the validation of hidden fields or to process hidden fields on certain forms . So I tried adding a basic node add page to the "Don't ignore hidden fields on the following forms" section. This updates the validate options to:
validate_options.ignore = '';
Which would cause all hidden fields to be validated, which should solve the problem. However, I then receive a general JavaScript error:
Uncaught Error: Syntax error, unrecognized expression: , .horizontal-tab-hidden :input
The error is coming from jQuery, and specifically a Sizzle selector function.
This line ", .horizontal-tab-hidden :input" is being added as an invalid selector which causes the above error and causes all Clientside Validation efforts to fail and the page refreshes. I believe the comma is there to allow for more than one selector being included in the ignore property which is passed to jQuery Validates ignore option. However, if the ignore setting is empty (due to validate hidden fields being set to true) then the comma is the first character encountered, which is an invalid selector.
Proposed resolution
The fix as I see it (please correct me if I'm wrong, I will admit I didn't test every use case), is to check to see if "validate_options.ignore" is equal to an empty string, and if it is, to not include the comma, see below:
validate_options.ignore += (validate_options.ignore == '') ? '.horizontal-tab-hidden :input' : ', .horizontal-tab-hidden :input';
This appears to work under the following conditions:
- Form ID given to "Don't ignore hidden fields on the following forms"
- Validate Hidden Fields Un-Checked
- Validate All Tabs Un-Checked
- Validate Hidden Fields Checked
- Form ID Empty
- Validate All Tabs Un-Checked
- Validate All Tabs Checked
- Form ID Empty
- Validate Hidden Fields Un-Checked
It however fails if both "Validate Hidden Fields" and "Validate All Tabs" are checked.
There is no error in the console, the page simply refreshes, I am not sure if this is expected or not. FWIW, this setting (both boxes checked) also causes Clientside Validation to fail with the original code, so it may be a pre-existing bug, and if so I can file a separate issue.
User interface changes
No UI Changes.
API changes
No API Changes
I have attached with the change to that one line for your review. Please let me know if there is anything else you need to know, or have questions about.
Comment | File | Size | Author |
---|---|---|---|
#13 | interdiff_11-13.txt | 2.16 KB | Suresh Prabhu Parkala |
#13 | 2293259-13.patch | 2.42 KB | Suresh Prabhu Parkala |
#11 | clientside_validation-chosen-compatibility-2293259-11.patch | 2.52 KB | leducvin |
#6 | issues_validating-2293259-6.patch | 614 bytes | undertext |
#5 | issues_validating-2293259-5.patch | 583 bytes | JeroenT |
Comments
Comment #1
skaduAdded patch with issue #.
Comment #2
skaduPrevious patch had inadvertently removed an empty line. This patch re-introduces the empty line.
Comment #3
dvasquez CreditAttribution: dvasquez commentedHello people. Blessing for all you. I want to know if there is a patch for fixing this for "multiple select" widget too.
I have a node with tags option, and there is an optiion for "_none".
Thanks if you can help me!
Comment #4
JeroenTComment #5
JeroenTCreated a new patch for 7.x-1.x
Comment #6
undertext CreditAttribution: undertext as a volunteer commentedUpdated patch for current 7.x-1.x.
Ideally should be picked up to 2.x branch too.
Comment #8
Jelle_SComment #9
leducvin CreditAttribution: leducvin commentedComment #10
leducvin CreditAttribution: leducvin commentedHow about this patch for 7.x-2.x-dev? I've included the modifications made in #6, so that when ones to validate all hidden fields, the javascript error mentioned in the issue summary does not occur.
I also included some chosen-specific fixes that worked for me.
Namely, I wanted the default validation behavior to ignore elements hidden by chosen only. And only when the option to validate hidden elements is checked in clientside_validation are ALL hidden elements validated.
There was also a problem for me where the chosen fields were not being highlighted when in error (and, vice-versa, unhighlighted). Furthermore the error message on the form would not hide or show dynamically upon changing the value of the chosen field. I added some javascript to fix those problems.
I hard-coded the classes used by the Drupal chosen module ("chosen-processed" and "chosen-container"). I don't know if it's an acceptable approach. I'm just a beginner at this, so I'd appreciate some review.
Comment #11
leducvin CreditAttribution: leducvin commentedOops wrong comment number in patch name. Re-uploading.
Comment #12
lokapujyaRan into this (or similar) issue and wrote custom validation code and then code to append the validation error list item to the list of validation errors. But, enabling the validation on the hidden fields seems like a better solution.
Also, it might be a good idea to create a new issue to make the hidden fields work automatically (or have a way for it to work with chosen) like the proposed 7.2 patch.
Regarding the 7.2 latest patch:
Comments need a period.
Can remove this comment.
Comment #13
Suresh Prabhu Parkala CreditAttribution: Suresh Prabhu Parkala at Specbee commentedTried to update the patch as per mentioned in #12. Please review.