1

How to use #states in add node form? I don't experience any problems when use them with exposed filters (on user side), but cannot do the same when additing new nodes. I tried the following code:

function custom_module_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'article_node_form') {
    $form['field_coordinate']['und'][0]['#default_value'] = t('1');
    $form['field_name_tag']['und']['0']['#states'] = array(
      'invisible' => array('select[id="edit-field-location-tag-und"]' => array('value' => '1')),
    ); 
  };
}

Where is my mistake?

  • 1
    the semicolon ; after the if block? – AjitS Nov 22 '12 at 7:11
  • not, it didn't help – moto Nov 22 '12 at 7:26
  • try using 'invisible' => array(':select[id="edit-field-location-tag-und"]' => array('value' => '1')) ;note the : before select – AjitS Nov 22 '12 at 7:37
1

You need to set the state in the "value" key, so one level deeper as you tried. E.g.:

$form['field_yourfieldname'][LANGUAGE_NONE][0]['value']['#states'] =
  array('visible' => 
    array('input[id="edit-field-otherfieldname"]' => array('checked' => TRUE))
  );
0

The #states array has the following structure.

array(
  STATE1 => CONDITIONS_ARRAY1,
  STATE2 => CONDITIONS_ARRAY2,
  // ...
)

The condition array has the following structure.

array(
  JQUERY_SELECTOR1 => REMOTE_CONDITIONS1,
  JQUERY_SELECTOR2 => REMOTE_CONDITIONS2,
  // ...
)

JQUERY_SELECTOR is a selector that is valid in jQuery, such as :input[name="foo"].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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