I have a fresh install of Drupal 7 with a basic dependency created. The dependent and controlling fields are both term reference fields, using radio select buttons. My goal is to simply make the dependent field visible when "Option A" is selected in the controlling field (otherwise the dependent field is not shown).
This works fine.
However, I also want the value of the dependent field to resort back to the default (N/A - nothing selected) if the user "changes their mind" and selects something other than Option A in the controlling field. I should be able to accomplish this by selecting "Reset the dependent to its default values if the dependency is not triggered" under the "Node edit settings", I believe. However, when I select that option, I get the following error message:
An illegal choice has been detected. Please contact the site administrator.
Warning: array_values() expects parameter 1 to be array, string given in conditional_fields_evaluate_dependency() (line 874 of /srv/www/pixelscrapper/public_html/sites/all/modules/contrib/conditional_fields/conditional_fields.module).
Any help in resolving this is appreciated. Thanks for the great module!
Comment | File | Size | Author |
---|---|---|---|
#27 | condition-2.jpg | 114.92 KB | crutch |
#27 | condition-1.jpg | 73.71 KB | crutch |
#25 | conditional_fields-7.x-3.x--1306096-25--hook_entity_presave.patch | 2.25 KB | capysara |
| |||
#25 | interdiff_1306096-17-25.txt | 1.94 KB | capysara |
#17 | conditional_fields-7.x-3.x--1306096-17--hook_entity_presave.patch | 2.15 KB | donquixote |
Comments
Comment #1
galaxor CreditAttribution: galaxor commentedI just found this as well. I've got a patch.
Comment #2
JordanMagnuson CreditAttribution: JordanMagnuson commentedNice, thanks Ryan! Haven't done a lot of testing, but so far your patch seems to do the trick...
Comment #3
JordanMagnuson CreditAttribution: JordanMagnuson commentedHm... so this was working okay for me in the case of two checkbox/radio fields, but now I have an integer text field that is dependent on a checkbox field (term reference), and when I uncheck the controlling field the integer field does not return to its default value... any ideas?
Comment #4
JordanMagnuson CreditAttribution: JordanMagnuson commentedAh... I see in the documentation:
I suppose there's a particular reason that the other widgets are hard to implement with this functionality?
Comment #5
JordanMagnuson CreditAttribution: JordanMagnuson commentedCan anyone else verify this problem/fix? It would be great to get a fix rolled for this (especially if it's as simple as the submitted patch)...
Comment #6
Jaapx CreditAttribution: Jaapx commentedPatch works for me! Thanks
Comment #7
capellicI am having a problem, too, and it's either the same issue or closely related. I tried the patch and it didn't clear it up for me.
This screencast illustrates the issue I am having: The values being reverted back are the wrong ones. Looks like the last value in all those to be restored is the one that gets written into all of the fields.
http://dl.dropbox.com/u/4770698/Drupal-org/Conditional_Fields_Bug.mov
Comment #8
darrenmothersele CreditAttribution: darrenmothersele commentedI experienced this same warning message when I had two required fields that were both dependent fields. I was not getting the validation error I expected, but this warning was returned.
Making the change in #1 fixed it for me.
Comment #9
letrotteur CreditAttribution: letrotteur commentedPatch in #1 seems to do the job for me, but can we get opinion of the maintainer about this issue. If it can be solve only by changing a line of code, why isn't this been committed since this issue is almost 10 months old?
Comment #10
JordanMagnuson CreditAttribution: JordanMagnuson commentedAny chance on getting maintainer feedback on this? Upping the priority in hopes of getting noticed, since this issue has been sitting here for almost a year, and we just need feedback on one line of code.
Comment #11
JordanMagnuson CreditAttribution: JordanMagnuson commentedHm... actually, it seems like a lot has chanced in the current dev version of the module (the conditional_fields_evaluate_dependency function doesn't seem to exist any more?), and this issue seems to be fixed. Can anyone confirm?
Comment #12
kenorb CreditAttribution: kenorb commentedComment #13
bpleduc CreditAttribution: bpleduc as a volunteer commentedNote: .info file for 7.x-3.x-dev download shows: 7.x-3.0-alpha2+10-dev
I was having this issue of "Reset the dependent to its default values if the dependency is not triggered." not working.
I applied patch with no change.
Function: conditional_fields_evaluate_dependency does still exist.
I found my solution by following:
https://www.drupal.org/node/1299872#comment-6269976
I do not believe this patch does anything at this point. I do not get the initial error which was resolved and it did not do anything I could tell once applied.
Comment #14
donquixote CreditAttribution: donquixote as a volunteer commentedFor me the value does reset to a "default value".
But the "default value" is not the one defined in the field instance settings. Instead it is the value that was previously saved on the entity!
Steps to reproduce:
Create fields:
- field_dependent as a textfield.
- field_dependee as a boolean with single on/off checkbox widget, with default value '' (empty string).
Configure conditional_fields:
- field_dependent is enabled if field_dependee is checked.
- field_dependent is reset to default value if field_dependee is unchecked.
Create an entity, submit:
- set field_dependee as enabled/checked.
- set field_dependent as "abc".
Edit the entity, submit:
- set field_dependent as "def" (while field_dependee is still checked).
- set field_dependee as disabled/unchecked.
Expected behavior:
field_dependent should now be '' (empty string).
Actual behavior:
field_dependent is now "abc".
Tested with 7.x-3.0-alpha2+10-dev.
Comment #15
donquixote CreditAttribution: donquixote as a volunteer commentedI think the correct thing here would be to use hook_entity_presave(), to reset the fields to their real "default value".
Comment #16
donquixote CreditAttribution: donquixote as a volunteer commentedI am trying to write a patch with hook_entity_presave().
But I don't know how I can determine reliably if a condition is "triggered", when looking at the entity itself and not at form values.
All the existing logic seems to be dealing with form values.
I start with field_get_items() for the dependee field. But then what? Is there a preexisting function to determine if the field matches the trigger condition?
Comment #17
donquixote CreditAttribution: donquixote as a volunteer commentedThis is what I came up with so far.
It is sad that this only works for specific types (value, empty, !empty) and not e.g. for "checked". Now I have to convert all my boolean field conditions to "value". Sad!
Comment #18
donquixote CreditAttribution: donquixote as a volunteer commentedDo NOT commit #17! This is just a quick shot, to show how it can be done.
E.g. I just found that a sanity check needs to be added, because $dependencies['dependents'] might be empty or not set.
Comment #19
Pawlus CreditAttribution: Pawlus commentedThanks for your patch, donquixote. It's not perfect, but with couple adjustments it did the trick in my case.
Comment #20
crutch CreditAttribution: crutch commentedConditionally required fields should be cleared of any selection or entry, when hidden, if no default value is chosen.
Example:
Field Location Type: Radio (Dependee,required)
- Location A
- Location B
Field Location A: Radio (Dependent, conditionally required)
- Office A-1
- Office A-2
- Office A-3
Field Location B: Radio (Dependent, conditionally required)
- Office B-1
- Office B-2
- Office B-3
User selects dependee Location A, then selects Office A-1. Oops they made a mistake and then go back and edit and select dependee Location B and Office B-1. In this case Office A-1 is still selected, it should be totally cleared.
Rules seems to be able to work around this issue only when making the fields not required: https://www.drupal.org/node/1933646#comment-12255767
Only when fields are not required can we then set a null value.
This may be okay that they are not required because rules will make sure only one location is selected. If no rules, then the maintainer could inadvertently select more than one location. However, the maintainer could select no location since the conditional fields aren't required, but they will see that when viewing the node and then realize their mistake and re-edit to select one.
Comment #21
vasikeI can confirm that #17 patch works and i also confirm that needs the sanity check, donquixote suggested
However, It seems there are other similar issues open.
For example #596612: Option to delete/reset values in hidden fields on condition change?
provides other approach to fix this.
Maybe we could focus on an issue only
@crutch : i don't think we should have a reset before submit.
The option/settings is:
Comment #24
capysara CreditAttribution: capysara commentedI added the sanity check noted in #21. I believe the short array syntax (not available until php 5.4) was causing the test to fail, so I changed it to the long form. (Also fixed a typo and a coding standard.)
The similar issue mentioned in #21 is based on reverting an earlier commit (Conditional fields not saving). It doesn’t look like the earlier issue was addressed so I didn’t test that approach.
Comment #25
capysara CreditAttribution: capysara commentedThe previous patch #24 reverted something it shouldn't have. Here is the corrected patch and interdiff.
Comment #26
capysara CreditAttribution: capysara commentedFWIW, if the patch is not ready, you can resort to adding a 'disabled' condition. E.g.,
Field x is visible when Field y has value a
Field x is disabled when Field y has none of value a.
It's not elegant, but it works for me.
Comment #27
crutch CreditAttribution: crutch commentedThanks Vasike. Our issue may be too specific or not directly related to this one.
If there is no default value chosen for field then it should be cleared in the case a user selected something by accident or changed their mind to use a different condition. To demonstrate, attached condition 1 image. User selects Main Campus and then selects Alamance. O wait, I meant to select a Remote location not the main campus, attached condition 2 image. In this case, Main Campus Alamance remains selected when the conditional field is hidden and when no default value is given for that field. It needs to be cleared. In our case we resolved with a Rule.
Comment #28
capysara CreditAttribution: capysara commentedcrutch,
Nevermind, I see now that you did clarify in your previous comment.
Just to clarify… are you saying it needs to be cleared when someone changes their mind (before the form is saved)? Or are you saying that it’s not clearing after the form is saved?If it’s the first one, I would say that this is the expected behavior. I would want the value to remain in case someone changes their mind several time and only be cleared at the time of submission so it doesn’t interfere with validation or go into the db.If it’s the second…. this patch is working for me. Maybe we’re using different field types or there’s some other difference in config?(Glad the rule is working for you, though!)Comment #29
capysara CreditAttribution: capysara commentedI had some issues using a boolean field as the dependee. Using Check boxes/radio buttons, it looks like it works, but it throws this Notice:
Notice: Array to string conversion in conditional_fields_form_after_build() (line 535 of /Users/scartee-kennedy/Sites/local-opbeta/sites/all/modules/contrib/conditional_fields/conditional_fields.module).
Single on/off checkbox seem to work with no Notice.
My current workaround is to just use the Form api #states in a custom module in hook_form_alter. I only have one boolean field in my form, so I'm using something like this:
Comment #30
skylord CreditAttribution: skylord commented#25 worked fine, thanks! List dependee field and multiple list/text required dependents without default values.