Download & Extend

Option to delete/reset values in hidden fields on condition change?

Project:Conditional Fields
Version:6.x-2.0-beta1
Component:User interface
Category:support request
Priority:normal
Assigned:Unassigned
Status:closed (works as designed)

Issue Summary

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

#1

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.

#2

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.

#3

@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."

#4

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.

#5

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.