Download & Extend

Is it possible to use this module with a custom form template?

Project:Conditional Fields
Version:6.x-2.x-dev
Component:Miscellaneous
Category:support request
Priority:normal
Assigned:Unassigned
Status:closed (fixed)

Issue Summary

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

#1

got the same problem... any answers?

#2

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

#3

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

#4

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

#5

subscribing

#6

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

<?php
 
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:
<?php
 
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.

#7

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

#8

Status:active» fixed

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

#9

Status:fixed» closed (fixed)

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

nobody click here