0

I've two forms in a page, one of the field name is same in both forms.

<form id="form1">
   <div class="div1">
        <select id="field_a" name="field_a_name"> ... </select>
    </div>
</form>

<form id="form2">
   <div class="div2">
        <select id="field_a" name="field_a_name"> ... </select>
    </div>
</form>

In hook form_alter, i assign the states to the field:

 $field_state = array( 'visible' => array(':input[name="field_a_name"]'  
 => array( array('value' => 'H') ))       );

 $form['field_a_name']['#states']   = $field_state;

The state does not work as expected, i believe it's caused by the same field name, most likely because of the jquery selector confused with the two fields with same name.

It works fine if the field name are different.

How to solve this?

  • 2
    hmm, you can't have double names and ids on page :-/ – QQQ Dec 22 '13 at 13:02
  • "one of the field name is same" - how? Drupal with it's drupal_html_id function and similar fail-safes tries to prevent that – Mołot Dec 22 '13 at 22:31
  • yes, i understand that the double ids issue. This is actually a two exposed filter forms from a view. That's why the id are same. Without to change the field identifier, i wonder is there a workaround for that to avoid States conflict. – Kent Dec 23 '13 at 0:14
0

In states you can use jQuery selectors. For example add form id to state selector:

$field_state = array(
  'visible' => array(
    '#form1 :input[name="field_a_name"]' => array(
      array('value' => 'H'),
    ),
  ),
);

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.