Hey,

Awesome totally functioning module in D7!

Just wondering if there is a way to have this setup for custom forms that we create ourselves. Rather than creating a new content type I have a custom form that I would like to use with this module. Is it possible to do that in some way? I couldn't find it anywhere in the documentation and could really use your help.

Comments

nevets’s picture

You actually do not need this module for forms produced with the form API.

If you look at the examples module it includes a number of ajax examples including how to hide/unhide form fields based on another form fields value.

You might also read Render Arrays in Drupal 7 and "Franz Heinzmann's session on the Render API in Drupal 7 at Drupalcon Chicago" under resources is worth the time to watch.

shaneonabike’s picture

Ok cool ... thanks for that!

shaneonabike’s picture

PS - I did actually get something working by coping the structure you had b/c I needed to rip a section out of our user form and place that in a popup for anonymous users. But probably not that relevant here.

peterpoe’s picture

Title: Using on non-node forms » Attaching dependencies to Forms API structures
Component: Documentation » Code
Category: bug » feature
Status: Active » Fixed

Here is some example code for an hypothetical "conditional_fields_example" module that you can use to learn how to attach a dependency to any form built with the Forms API. It needs the latest dev version to work.
Of course using this makes sense only if you want to create complex dependencies without going crazy with the States API and/or want to use Conditional Fields built in systems for animation effects, dependency triggering evaluation, and so on.
More documentation is needed, but this is a good start for anyone daring to dig in.

/**
 * Implements hook_menu().
 */
function conditional_fields_example_menu() {
  $items['conditional_fields_example'] = array(
    'title' => 'Conditional Fields form example',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('conditional_fields_example_form'),
    'access arguments' => array('access content'),
  );

  return $items;
}

/**
 * Conditional Fields custom dependency example.
 * Builds a simple form, then attaches a dependency.
 */
function conditional_fields_example_form($form, &$form_state) {
  $form['dependee_item'] = array(
    '#type' => 'select',
    '#title' => t('Dependee select'),
    '#description' => t('Select On or Also On to activate the dependency.'),
    '#options' => array('off' => t('Off'), 'on' => t('On'), 'also_on' => t('Also On')),
    '#default_value' => 'off',
  );

  $form['dependent_item'] = array(
    '#type' => 'textfield',
    '#title' => t('Dependent textfield'),
    '#default_value' => t('Dependency activated!'),
    '#size' => 40,
    '#maxlength' => 255,
  );

  $form['actions'] = array(
    'submit' => array(
      '#type' => 'submit',
      '#value' => t('Test dependency'),
    )
  );
  
  // For available options, see conditional_fields_attach_dependency().
  $options = array('values_set' => CONDITIONAL_FIELDS_DEPENDENCY_VALUES_OR, 'values' => array('on', 'also_on'));

  // Attach the dependency.
  conditional_fields_attach_dependency($form, 'dependee_item', 'dependent_item', $options);

  return $form;
}

/**
 * Validates conditional_fields_example_form.
 * Throws an error if the dependency is not triggered.
 */
function conditional_fields_example_form_validate($form, &$form_state) {
  // Dependency information is stored in the property #conditional_fields.
  $options = $form['#conditional_fields']['dependent_item']['dependents']['dependee_item']['options'];

  $is_triggered = conditional_fields_evaluate_dependency($options, $form_state['values']['dependee_item']);

  if (!$is_triggered) {
    form_error($form['dependee_item'], t('The dependency was not triggered!'));
  }
  else {
    drupal_set_message(t('The dependency was triggered.'));
  }
}

Status: Fixed » Closed (fixed)

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