Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I have a yes/no select component on a form asking if a user is below a certain age. If they mark "yes", then a number of components relating to the users parent/guardian should be required. If it is no, then those components can be left blank.
Comments
Comment #1
BrightBoldOoh! Yes. I was just looking to see if anyone was doing this - that's exactly what I'm looking for.
Comment #2
BrightBoldOops. Double-submit. Please delete.
Comment #3
svendecabooterThis kind of functionality will be complex to fit into the architecture of this module...
I'll have to investigate if there's an elegant way of doing this...
If your requirements allow you to spread the form elements over multiple pages, you could easily fix this with Conditional Rules in webform 3.x.
Just add the Yes/No select component on page 1, and on page 2 add some required textfields. Make sure to set the Conditional rules for these textfields (or surrounding fieldset) to only show when the value of the component on page 1 "is one of" "no" / "0" (or whatever the key is for "No" on your Yes/No component).
Comment #4
svendecabooterComment #5
BrightBoldThanks @svendecabooter, that's a good tip. I'll try that.
Comment #6
bancarddata CreditAttribution: bancarddata commentedI would be interested in a feature like this in the 7.x branch as well. In my case (and I would use this case on probably every site I ever do), I would like to have a radio button called "Preferred method of contact", with options of "E-mail" and "Phone". Depending on this selection, I would want to make either the "E-mail Address" field or the "Phone number" field required (but not both).
Comment #7
svendecabooterThat'll not be easy to do.
The #ajax functionality in D7 would allow for this, but it's hard to make this generic.
We would need a UI to attach the #ajax behavior to a selected form element, and then a way to wrap the fields that need to change into a wrapper div or fieldset, so they can be reloaded via AJAX with updated 'required' settings.
Even though i'd like to add that, it'll be a lot of work to make this generic (unless i'm missing an easier approach), and somewhat out of scope of the purpose of this module.
Short term you might be easier off creating a custom module with a hook_form_alter adding the proper #ajax property on the element you want to have trigger the change in behavior, and grouping together the elements that need to be dependent on it.
Comment #8
bancarddata CreditAttribution: bancarddata commentedThanks for the explanation. It does indeed sound like much more trouble than it is worth.
Comment #9
Liam MorlandDrupal 6 is no longer supported. If this issue exists in a later version of webform_validation, please re-open and update the version number.