1

I have a altered registration form with a check box that when checked adds few fields. The fields are wrapped in a specific div.

The callback function of check box is supposed to hide the dive when the check box is unchecked.

The adding works well but the unchecking doesn't. If I uncheck the checkbox the added fields remain there as nothing has happened. Checking it again doesn't change anything.

The weird thing is that the div is there (at least Firefox inspector sees it) and the callback function is probably fired as well (the POST is send).

I have even tried to debug it using dsm() but it doesn't show any message (no idea why).

    function fm_users_more_form_user_register_form_alter(&$form, &$form_state, $form_id) {

  $form['fm_first_in_firm'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show form.'),
    '#ajax' => array(
      'callback' => '_fm_ajax_new_firm_form',
      'wrapper' => 'firm-reg-container-div',
      'method' => 'replace',
      'effect' => 'fade',
    ),
  );

  $form['firm_registration_container'] = array(
    '#type' => 'hidden',
    '#attributes' => array('id' => array('firm-reg-container-div')),
  );
}


function _fm_ajax_new_firm_form($form, &$form_state) {
  if ($form['fm_first_in_firm']['#value'] == 1) {    
    dsm('tst1');

    ...

    $firm_form = drupal_get_form('fm_firm_node_form', $node);

    $form_wrapped = array(
      '#prefix' => '<div class="firm-reg-container-div">',
      '#suffix' => '</div>',
    );  

    $form_wrapped['register_form'] = $firm_form;
    return $form_wrapped;
  }
  else {
    dsm('tst2');
    return array(
      '#type' => 'hidden',
      '#attributes' => array('id' => array('firm-reg-container-div')),
    );
  }
}

1 Answer 1

3

To show or hide some form elements you can use #states attribute.

$form['fm_first_in_firm'] = array(
  '#type' => 'checkbox',
  '#title' => t('Show form.')
);

$form['firm_registration_container'] = array(
  '#type' => 'item',
  '#attributes' => array('id' => array('firm-reg-container-div')),
  // make this form item visible only if "fm_first_in_firm" is checked
  '#states' => array(
    'visible' => array(
      ':input[name="fm_first_in_firm"]' => array('checked' => TRUE)
    )
  )
);
2
  • Ok, I see that this should be better approach and I'll definitely try it but still what is wrong with my approach? I mean the whole div with the form should be replaced and therefore hiden, shouldn't it? //Because of that I'll not accept your answer for now even though it's good and I thank you for it.
    – Petrroll
    Commented Aug 31, 2013 at 11:18
  • 1
    @Petrroll Take a look at the AJAX module included with the Examples module, it's very helpful when learning the AJAX API. Just a hint - you should never be calling drupal_get_form from within an AJAX callback. Change what you need to in the form definition itself, based on the state context, and use the callback function to simply return the form element
    – Clive
    Commented Aug 31, 2013 at 12:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.