I noticed that I can't have a field with the same name in two or more different forms. Otherwise rules get merged, that is both fields need to comply with the rules from field A as well as from field B.
A solution might be to simply use two different names which is okay for custom forms, but not if you want to alter existing ones. In my case I have a comment reply form ("comment_form") under each comment on my page which the user can show if he/she clicks on the reply button. The comment_body field of the form for new comments has a rule set which those fields in the reply forms should not have. But they have due to this bug (or feature?).
In clientside_validation.module, rules are keyed by their field name in $js_rules[$name] everywhere which explains this behavior. On the other hand in JavaScript, Drupal.settings.clientsideValidation.forms contains an object with rule configuration for each form. In my case that means for "comment-form", "comment-form--2", "comment-form--3" and so on, but each of these are equal although they shouldn't.
| Comment | File | Size | Author |
|---|---|---|---|
| #3 | clientside_validation-fix-rules-being-merged-1902336-3.patch | 1.04 KB | pivanov |
Comments
Comment #1
attiks commenteddo you have a demo, or can you provide me some code so we can test this?
Comment #2
ronino commentedYes. Put this hook_form_FORM_ID_alter() into one of your modules, duplicating the search form's text field:
If you then clear your cache and open a page with both Drupal's search form and a comment form (e.g. a node page), you then have a search field in both forms. Enter e.g. "a" into both fields and click beside them. Both fields will throw the error "Use only numbers at Another search_block_form field." or something similar.
I just noticed that in Drupal.settings.clientsideValidation.forms in the inline JavaScript, every form has all the rules from all fields of the previous forms.
Comment #3
pivanov commentedIt seems a possible solution is to have the
clientside_validation_webform_after_build()andclientside_validation_form_after_build()use a non-static$js_rulesvariable.All rules get merged in
$js_rulesfor each form built during the request.clientside_validation_add_js_settings()then sends the duplicate rules to the JavaScript front-end side. All forms will have the rules for all form elements on the page. Some will get overridden if the have the same name - rules added later in the request will override existing ones essentially deleting them. This is the issue in a nutshell.Comment #4
jelle_sTested with code from #2 and confirmed the patch worked. Fixed in the latest dev version. Thanks for the patch!