I have a controlling field that permits a user to either submit a link or upload a file. This works fine, except that if the user first enters a URL, then changes her mind and uploads a file instead, the originally entered URL remains in the node. To avoid this, the user has to blank out the URL field first, before switching the controlling field to upload a file. This is counter-intuitive--if users no longer see the field, they aren't going to expect to be submitting data in it. Is it possible to reset a field to its default value, or remove its value, when the controlling field is changed?

Comments

capellic’s picture

I had the same problem and solved it by setting up two rules with the Rules module -- one for when the value of the controlling field was "file" and one for when it was "link."

Here are the details for when "file" was selected by the controlling field:

1. Set the event for "Content is going to be saved"

2. Add a "If" condition defined as "Field has value". Leave the fields in the "Arguments configuration" fieldset as they are and select your conditional field in the "Field" drop-down and click Next.

3. You will see the CCK widget on the next page. Select "File" and click Save.

So at this point, we have the rule firing off any time that this form is saved and the controlling field is set to "file".

4. Add an action defined as "CCK, Populate Field" and click Continue.

5. Select the field holding the linked value and click Continue.

6. Yo wile see the Link field. Leave it blank. Click Save.

7. Done.

aac’s picture

Thanks capellic.
Your method works fine. But if the link(url) field is "REQUIRED" then you would not be able to save the action part of the rule.

capellic’s picture

@aac. Yeah, I haven't tried to make dependent fields required. I required the controlling field and, the way my forms are laid out and I only have one controlled field, users get the impression that the controlled field is required, too. And as you've probably assumed, I also use Rules to enforce that the controlled field be required. I first turn on validation for that form. Then I look at the value for the controlling field and if it is X and there isn't at least one value for X (setup for multi-value fields), then thrown an error.

IF NOT:

if ($form_state['values']['field_event_more_info'][0]['value'] == 'link') {
  foreach ($form_state['values']['field_news_event_link'] as $link) {
    if (is_array($link) && $link['url'] != '') {
      return TRUE;
    }
  }
  return FALSE;
}
return TRUE;

DO:

Set form error on element 'field_event_more_info' with message, "You must define a link."

peterpoe’s picture

Version: 6.x-1.0-beta2 » 6.x-2.0-beta1
Status: Active » Closed (works as designed)

From what I undertand, this is a problem with the layout of the form and not of Conditional Fields?

The behavior with the values of controlled fields, is that while editing a node, the values of those untriggered are mantained. This is for user conveniency. But, when you save the node, the values of untriggered controlled fields will be reset to their default values, unless you uncheck the corresponding option in the Conditional Fields options tab.

Feel free to reopen if you think this actually a feature request.

xjm’s picture

Hmm, what you describe in #4 is not what happened when I originally opened this issue. Values were saved regardless. I'll test it again.

capellic’s picture

Just revisiting this after all these years. I have "Reset the dependent to its default values when the form is submitted if the dependency is not triggered." checked. I also see this, "Note: This setting only applies if the condition is "Value" and may not work with some field types. Also, ensure that the default values are valid, since they will not be validated." My condition is a "Value" so it should work, but it does not, in two cases. I have a dependent field that is a file and another that is a link-- neither get cleared out when not visible and the form is saved.

capellic’s picture

Version: 6.x-2.0-beta1 » 7.x-3.0-alpha1
Issue summary: View changes
Status: Closed (works as designed) » Active
doppel’s picture

Same issue here.

I got my taxonomy term reference

Facebook
Twitter
Google Plus
Others

when I tick others field the Others Field is appearing but when I untick the others field, the value of the others field was not cleared even if the reset to default value check box in the dependency config was ticked.

sashken2’s picture

I have same issue too.

gaurav_drupal’s picture

Same issue.

Coupon Code Swap’s picture

I am also having the same issue. It used to work properly, if a value was left in a field that got hidden, it would be reset to default upon saving. Not sure exactly when it stopped working. Now, it maintains the value that is lingering in the hidden field after saving the node instead of resetting it to default so that both the visible and hidden fields have saved values.

Note: This is a problem with 7.x-3.0-alpha2. I just reverted back to 7.x-3.0-alpha1 and reset to default is working properly again.

oo0shiny’s picture

StatusFileSize
new684 bytes

It looks like the update in 7.x-3.0-alpha2 to line 737 in conditional_fields.module is the culprit here. The if statement was changed from

$parents = isset($element['#field_parents']) ? $element['#field_parents'] : $element['#parents'];

to

$parents = !empty($element['#field_parents']) ? $element['#field_parents'] : $element['#parents'];

Reverting this line of code makes everything work again in terms of removing the field on the hidden value. I've added a patch that does just this, let me know if it fixes the issue. I'm not sure why this changed, so I'm curious if it's broken anything else. I can't find any other issues in my brief testing.

oo0shiny’s picture

Status: Active » Needs review
oo0shiny’s picture

Updating patch name with correct syntax based on these guidelines: https://www.drupal.org/patch/submit

peterpoe’s picture

Status: Needs review » Needs work

Can't just revert that change: it would break #1542706: Conditional fields not saving

webbymatt’s picture

Can confirm that #14 works, I've just deployed to a live site without issue. Bit of a lifesaver, actually!

sergei_brill’s picture

The patch works for me.

suneethark’s picture

#14 patch works for me. Thanks for the patch.

toki’s picture

#14 patch works for me too. Reset the dependent (term reference field) after cloning a content and changing a dependency value (term reference field too) now works (the dependent value back to default). No problem with saving.

Coupon Code Swap’s picture

Patch #14 works for me as well. Perhaps it should be committed to next release.

br0ken’s picture

Version: 7.x-3.0-alpha1 » 7.x-3.x-dev
Component: User interface » Code
StatusFileSize
new1.11 KB
new1.87 KB
vasike’s picture

Status: Needs work » Needs review

#14 patch won't apply
But #21 will

I confirm that the #14 approach fix the issue i have with Term reference fields.

@BR0kEN: could you, please, detail what your patch brings to this fix?
Maybe some example to replicate a case for this fix.
Is it about #15 comment?

p.s. It seems there are other similar issues open.
Maybe we could focus on this one or other only
#1306096: Problem with "Reset the dependent to its default values if the dependency is not triggered."
or
#2781493: Reset dependent to default values not working

casperone’s picture

StatusFileSize
new35.82 KB

Hi,

I am pretty new to drupal and having a heck of a time trying to apply patch #21. I attached a picture of the error I am getting, is anyone willing to point me in the direction?

My goal is to use conditional fields to set the value of a field to "Emptied" based on a dependency OR use the "Reset the dependent to its default values if the dependency is not triggered" function, neither of which seem to be working.

Currently running:
Drupal 7.56
MySQL 5.6.33
PHP 5.6.30

Thanks!

capysara’s picture

Has anyone already tested if #21 addresses the fix in #1542706: Conditional fields not saving?

sashken2’s picture

I have same issue in Drupal 8

colan’s picture

Version: 7.x-3.x-dev » 8.x-1.x-dev
Category: Support request » Feature request
Status: Needs review » Needs work

This looks like a feature request. New features go into HEAD, and may then be backported.

paulocs’s picture

Issue summary: View changes

I did a patch for it.
I attached it in the issue #2833343: Re-add reset to default values option. May be you can test it for me. Thanks!

crutch’s picture

+1 #21 Thank you

nicholass’s picture

I accomplished this pretty easily in D9 with https://www.drupal.org/project/eca Similar to #1.

Wish it was in the module itself but all the patches here are for D7