Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
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

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))
  );
share|improve this answer

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"].

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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