This feature request is to conditionally set other component values in a general way. It is a generalization of the original request below.
Original Issue:
Similar to #1885056: Conditional actions: required / optional
It would be nice to have the option to have a conditional for checking/unchecking checkbox fields depending on the value of one or more other fields on the webform.
I haven't looked into the code of the existing conditionals show/hide yet, but i know that the Drupal #states attribute allows checked/unchecked so it would seem to me that this is something quite similar.
I'm assuming patches are welcome ;)
Comments
Comment #1
danchadwick commentedYes, patches are welcome on this topic. #states would not be the way to do it. Except for show/hide (which you already have), they require a trip to the server, which definitely want to avoid. There is a very small amount of existing code that anticipates conditionals that do things other than show/hide.
Conditionals are surprisingly complicated. They interact with both save-as-draft and multi-page. In addition, there is an open issue about performance problems related to having a lot of conditional. I believe it related to the definition, not the use. I haven't looked into it closely.
Comment #2
oenie commentedI've looked into the code and it does look pretty complicated, mostly because the conditional code seems really geared toward the show/hide conditionals.
Even the function title that does the checking actually returns a showComponent variable, as well as mentions 'show/hide component' in it's documentation.
I'm not sure wether this functions needs to be rewritten to handle extra conditional actions, or that another function needs to be made.
The show/hide function is used on several occasions to actually show/hide a component, so rewriting it would interfere with a lot of calls.
I will see if i can find a solution for it (time and code-wise), taking your comments in consideration, Dan.
However it might be that for the time being i will have to introduce some custom alter code since i currently only have one case that needs these extra conditionals.
But i can see potential extra cases in the future, and i can hardly imagine that no one else has come across this issue.
Thanks for the reply !
Comment #3
danchadwick commentedWith the completion of the conditional engine (WebformConditionals class), this feature request is much more doable. I would like to know more about the use case. Setting a component would effectively disable it for as long as the input conditions are true. For example:
IF Color = 'green' THEN SET 'Blood Color' to 'Vulcan'
So when Color is green, any attempt by the user to change the Blood Color would be defeated. To avoid frustration, the field should probably also be disabled when it is being set. Only when Color isn't green could the use change the Blood Color field.
Would this work for your use case?
Comment #4
danchadwick commentedComment #5
danchadwick commentedThis patch implements a "Set to" conditional action. It allows values to be set for the following components:
Notable it does not implement date or time, although patches are welcome for those.
When set, the target component cannot be changed by the user (disabled or readonly, as appropriate). If the component is also hidden ("isn't shown"), that trump the any value set. More than one conditional can set the same conditional. If more than one is active at a time, the result in undetermined.
For select components, multiple values may be specified separated by commas. The key is given, not the user-facing text.
This is fairly complicated code and deserves testing.
Comment #6
danchadwick commentedHere an update that add support for email components.
Comment #8
danchadwick commentedTest failure -- if genuine -- is related to tests not being updated for new token substitution rules.
Comment #9
danchadwick commentedThose 4 fails pass when run locally with drush.
Comment #10
danchadwick commented#6 committed to 7.x-4.x.
Comment #11
danchadwick commentedNeeds port to D8. Of the four conditional patches to be ported, apply 5th.
Comment #13
fenstratCommitted and pushed to 8.x-4.x. Thanks!
Comment #17
vakeesan26 commentedis it possible to conditionally set one component value to other component (like delivery address same as billing)
(currently only able to set a static text value)
Comment #18
danchadwick commentedRe #17. No, and not likely to be implemented by me.
Comment #19
gregori.goossens commentedHi,
i have some trouble when i conditionally set value to a radio component.
The problem seems to be in webform.js in $.fn.webformVal function.
When we use radio webform component input type radio are created and safe_key store in value attribute.
If i use condition on first component to set second radio component, the webformVal function select (with $this.val(...) ) safe_key target in condition but also set value to false for the other choice.
Then if you change first component (so change condition) the second component safe_key store in value attribute have been erased to false.
Maybe we should set the value only if on is true :
What do you think about it ?
Thanks
Comment #20
Sam77 commentedHi,
Info in #15 really helped me with my case.
but it also give me error while submitting.
Basically, I have a checkbox for label1 and 3 checkboxes for label2.
I have a "conditional" that when, label1 checkbox is selected, check all the checkboxes of label2.
so I have "set to" to 1,2,3
when I submit, I get an error
"an illegal choice has been detected.."
after all experiments, I know that its due to the comma separated values that I am using.
if I use 1 instead of 1,2,3 then only 1 checkbox is getting selected and submission goes through without any issue.
Please help.
Thanks.
Comment #21
rakun commented#20 I have same problem. My workaround is ..
in _webform_client_form_validate function instead of
I used
code handles value in conditional field as array for checkboxes and select component.