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!

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

galaxor’s picture

I just found this as well. I've got a patch.

JordanMagnuson’s picture

Status: Active » Needs review

Nice, thanks Ryan! Haven't done a lot of testing, but so far your patch seems to do the trick...

JordanMagnuson’s picture

Hm... 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?

JordanMagnuson’s picture

Ah... I see in the documentation:

Reset the dependent to its default values if the dependency is not triggered.
Note: works only with Text and Options widgets. Enabling this options with other types of widgets could lead to unexpected results.

I suppose there's a particular reason that the other widgets are hard to implement with this functionality?

JordanMagnuson’s picture

Can 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)...

Jaapx’s picture

Patch works for me! Thanks

capellic’s picture

I 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

darrenmothersele’s picture

I 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.

letrotteur’s picture

Patch 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?

JordanMagnuson’s picture

Priority: Normal » Major

Any 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.

JordanMagnuson’s picture

Priority: Major » Normal
Status: Needs review » Active

Hm... 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?

kenorb’s picture

Issue summary: View changes
Status: Active » Postponed (maintainer needs more info)
bpleduc’s picture

Status: Postponed (maintainer needs more info) » Closed (cannot reproduce)

Note: .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.

donquixote’s picture

Status: Closed (cannot reproduce) » Active

For 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.

donquixote’s picture

I think the correct thing here would be to use hook_entity_presave(), to reset the fields to their real "default value".

donquixote’s picture

I 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?

donquixote’s picture

This 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!

donquixote’s picture

Do 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.

Pawlus’s picture

Thanks for your patch, donquixote. It's not perfect, but with couple adjustments it did the trick in my case.

crutch’s picture

Conditionally 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.

vasike’s picture

Status: Active » Needs review

I can confirm that #17 patch works and i also confirm that needs the sanity check, donquixote suggested

  $dependencies = conditional_fields_load_dependencies($entity_type, $bundle);
  if (empty($dependencies) || empty($dependencies['dependents'])) {
    return;
  }
... continue with hook_entity_presave()

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:

Reset the dependent to its default values when the form is submitted if the dependency is not triggered.

The last submitted patch, 1: reset_if_not_triggered.diff, failed testing. View results

Status: Needs review » Needs work
capysara’s picture

I 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.

capysara’s picture

The previous patch #24 reverted something it shouldn't have. Here is the corrected patch and interdiff.

capysara’s picture

FWIW, 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.

crutch’s picture

FileSize
73.71 KB
114.92 KB

Thanks 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.

capysara’s picture

crutch,

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!)

capysara’s picture

I 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:

  $form['field_dependent']['#states'] = array(
    'visible' => array(
      ':input[name="field_dependee1[und]"]' => array('value' => t('Something')),
      ':input[name="field_dependee2[und]"]' => array('value' => 1),
    ),
  );
skylord’s picture

#25 worked fine, thanks! List dependee field and multiple list/text required dependents without default values.