Fix validation of select2 fields in "On behalf of Organisation" block #17672
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
Found by @jmcclelland via https://lab.civicrm.org/extensions/stripe/-/issues/147#note_38994
Normally a select2 is a hidden select field with another visible "text" field. But it is the hidden select2 field that actually needs validating so we ignore (following #16488) using jquery validate and the pattern
hidden:not(.select2)
. This fails for the "I want to contribution on behalf of organisation" block because the select2 fields get hidden by the parent div and are not required when the block is hidden - but they are required when the block is visible.Before
Form validation fails if you have "on behalf of organisation" option on the form and one of those fields is a select2.
After
Form validation fails if you have "on behalf of organisation" option checked on the form and one of those fields is a select2 but passes if you don't have the option checked
Technical Details
We introduce a new "crm-no-validate" class for jquery validate to ignore. Then we apply that to the select2 fields when hiding the "On behalf of" block.
Comments
The first commit is just whitespace cleanup. Please review the second commit to see the changes made.