I am trying to create a custom template for a CCK form that includes conditional fields. When I do the following in my form template:

print drupal_render($form['controlling_field']);
print drupal_render($form['controlled_field']);

The fields are both rendered just like normal CCK fields--the controlling_field doesn't actually control the visibility of the controlled_field. Is there any way to make this work? THanks for any help.

Comments

beauz’s picture

got the same problem... any answers?

mrthumpz’s picture

I am having the same issue. I played around with the weights of the modules, but that didn't help. I can get the #conditional-fields info to show in the $form array, but the form is always rendered with all fields visible when using hook_theme() with theme_[contenttype]_node_form()

dinis’s picture

You will need to add some logic into your output, an if statement should do.

As a precaution, make sure you save your template to disk before experimenting. If you WSOD your template, you'll have to delete it and start over :)

Hope this helps,
Dani

rburgundy’s picture

Hi Dinis,

Could you please share an example of adding logic to the rendering of a field on a custom form.
Simply drupal_rendering form fields shows the conditional fields elements in devel but doesn't seem to properly show/hide the field depending on the control field.

Thank you

YK85’s picture

subscribing

peterpoe’s picture

I tested this using the method outlined here http://drupal.org/node/601646, and this works:

  print drupal_render($form['field_controlling']);
  print drupal_render($form['field_controlled']);

However, if you render the individual elements of the fields separately (like, say, each checkbox of field_controlled), you will have to wrap the elements in the wrapper div. You can use theme_conditional_fields_wrapper for this. An example with a checkboxes controlled field:

  print drupal_render($form['field_controlling']); // No change to the controlling field

  // Render the controlled field, each checkbox with a separator in between
  $controlled_field = '';
  foreach (element_children($form['field_controlled']['value']) as $value) {
    $controlled_field .= drupal_render($form['field_controlled']['value'][$value]) . '<hr />'; 
  }

  // Theme the wrapper. The 3rd argument is the id of the wrapper, the 4th is an array of classes. These are required for js to work correctly
  print theme('conditional_fields_wrapper', $controlled_field, 'conditional-field-controlled', array('conditional-field', ' controlled-field'));

  // Important: remove what's left of the field (title, etc), or else conditional fields will create a wrapper also around this and mess things up.
  unset($form['field_controlled']);

  // Output the rest of the form
  print drupal_render($form);

Note tha there is no need to alter the logic of the dependency: this only changes presentation.

peterpoe’s picture

Version: 6.x-1.0-beta1 » 6.x-2.x-dev
Component: Code » Miscellaneous

I forgot to mention: this only applies to version 6.x-2.x

peterpoe’s picture

Status: Active » Fixed

I added a page with this example to the documentation: http://drupal.org/node/1151380

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.