Download & Extend

Rule Event show/hide field A depending on field B value

Project:Rules
Version:6.x-1.x-dev
Component:Forms Support
Category:support request
Priority:normal
Assigned:Unassigned
Status:active

Issue Summary

I am trying to make field A only show if a certain value is selected in field B. Is this currently possible with Rules Form Event?

1) I added Rules Forms condition "Form element has value" using the form element from whats shown on the profile node form for field A
2) I added Rules Forms Action: "Hide an element of the form" and put the form element of field B and typed a value in text field B "Apple"
3) Now when I go to profile node form and change value of text field B (select list) to Apple, field A does NOT hide.

Can anyone assist please?

Comments

#1

Additional info:

I am trying to make field B not show on the node form when the specific value is not selected in field A text select list.
Then for field A to appear when that value in field B is selected.

#2

I'm working on a similar feature. I can hide a CCK file field based on the content of another CCK text field when the rule is triggered by the "ABC node form is being built". It sounds like you want to trigger the rule on field data entry, but I only see '...form being built', '...form being validated' and '...form is submitted' triggers for form events. I'm not sure the Rules module can dynamically show and hide form fields the way you want; you might have to use JQuery to detect and act immediately on that kind of change.

#3

I am not able to get this working even for after form submission. When the user comes back to the node form and the field that should be showing now as the value in another field indicated in the rules condition was previously selected on node form save.

Can you please assist in getting the 'as design' of Rules Forms to work?
I would appreciate it very much! Thank you

#4

Try conditional fields module it will be much easier for this small task. Assuming you're using cck as you never actually stated. This module is an easy way to make one cck field "conditional" of a response to another cck field.

Chris

#5

subscribing

#6

Hi Chris,

yes, I am using cck for the field (sorry for not mentioning this before). I am looking towards Rules module as a workaround against Conditional Fields module as I am needing a field to show/hide with conditions A or B or C. Conditional Fields module currently only allows for A and B and C #719068: show if ANY of controller fields are selected (OR vs AND controllers)

Would you be able to assist?

#7

@rburgundy: Are you sure your condition test is returning the correct result?

I ask because I went wrong in a couple different ways (These may be obvious to you, but they weren't to me.)

1. Form Events will display two IDs for some CCK fields (e.g., Container element ID: field_mytext; Element ID: field_mytext[0]). Either ID will work as the target element in a Hide Form Element action, but only the indexed ID works as the target element in a Form Element Has Value condition.

2. Some CCK fields do not return a value the way Rules expects. FileField is an example, as described here: http://groups.drupal.org/node/25672.

I was able to sort out my FileField issue by using a Custom PHP Code condition to test for the value (e.g., return empty($form['#node']->field_myfile[0]);). I used a Show a Configurable Message action to display and debug my condition test results.

Hope this helps.

#8

Thanks thekenshow,
I appreciate any advice that may help me!

1) By index ID do you mean the Element ID: field_mytext[0] one?
For the field I am trying to check the value of I see: Element ID: field_example

2) Do you have any specific code you use with the show configuration message testing/debugging that may help me?

field_example
type: text
widget: checkboxes/radio
allowed values:
one
two
three
four

I am trying to check if the three was selected, and if so trigger an action to show another field.
With rules debugging ON, I see that the event trigger fails where it checks for value 'three' saying FALSE even if the value three was checked and the node form saved.

Would you be able to assist?

Thanks!

#9

I've been having this same problem with CCK Text fields and Rules Forms. When i use the following widget types...

  • Select list
  • Check boxes/radio buttons
  • Single on/off checkbox

...they all return a form element with an id that looks like this: field_my_field, which cannot be understood by Rules Forms.

On the other hand, using a regular Text box widget results in a form element id that looks like this field_my_field[0] and Rules understands it just fine.

I'm thinking this is a bug either with Rules or CCK, no?

#10

I did a quick test by creating a radio button text field (field_radio) with four options: N/A, Ontario, Manitoba, Saskatchewan. Looking at the structure of the data in the form (using Dev Load), it looks like a normal text field. But the displayed ID lacks the index (i.e., field_radio instead of field_radio[0]) which is suspicious, and doesn't test properly with a simple rule (using [0] or not). I was able to get a proper result using an Execute custom PHP code condition with the following:

return($form['#node']->field_radio[0][value] == 'Ontario');

To see what's happening, you can use the Show a configurable message action with PHP to display the form element value, something like this:

The result is: <?php print_r($form['#node']->field_radio[0][value]); ?>

#11

Just to say that I had no success with the

return($form['#node']->field_radio[0][value] == 'Ontario'); snippet, however

return empty($form['field_ref']['0']['value']); did work!

#12

You can do like suggested above but I found an easier way and you don't need to activate the PHP filter module. You could get the value of the field from the from like this: $form['#node']->field_official_group[0]['value'] but this only works if you have PHP filter active.

If you want to do it without PHP filter, this is where the value from a radio CCK field is found in the form:

<?php
$form
['field_name']['#default_value'][0]['value']
?>

I think what confuses people is that the suggestions made by the Rules module for a radio (with two options in my case) CCK button look like this: field_name[value][1] and field_name[value][0] when in fact it should be like this: field_name[#default_value][0][value]

Update The above code suggested if the users have the permission to edit that particular CCK field which value you will check. I'm not an expert on CCK but it seems that CCK builds the fields diferrent depending on the user access (or other conditions). In the case of a CCK Numeric field, the field for users that did not have the right to edit the field looked something like this: field_name[#value][0][value]

If anyone is more into CCK and has an explanation for this (probably also depends on the type of CCK field) I think it would help a lot of people figuring out how to check the values for CCK fields.

#13

Sorry this might be old but wanted to ask.

Just a thought but why does conditional fields not work?

A and B and C = D shown is one way.
A and B = C shown is one way.
A value = C shown is one way.
A value and B value = C shown is one way.
B value = C shown is one way.

What your looking to do in post #6 can be achieved in this module I think depending on your field. EX

A value = D shown
B value = D shown
C value = D not shown

OR the same difference

A value OR B value = D shown

What is your combination that you can't get that to work? What field is the OR going to be used on that makes this not usable?

POST #1
"I am trying to make field B not show on the node form when the specific value is not selected in field A text select list.
Then for field A to appear when that value in field B is selected."

Post #1 is very odd to say it like this.

Select A I guess is Field A
Option 1 - can be selected: if is/ Select B not shown
Option 2 - can be selected: if is/ Select B not shown
Option 3 - not selected option / Selected

Select B I guess is Field B
Option 1
Option 2
Option 3

Why not say only show Field/Select B when Option 3 is Selected and Field A is always around. Why would you get rid of Select A if a option in B is selected because only certain options allow Select B to show. How would you revers it if you unselected a option on Select B.

I am lost on what you want... can you explain more?

Thanks Anthony

nobody click here