3

I am trying to change the states via the the following code:

function my_module_form_alter(&$form, &$form_state, $form_id) {
  ...
  case 'my_form_node_form': {
    $form['my_file_field']['#states'] = array(
      'visible' => array(':input[id="my_id"]' => array('value' => 'my_value'),
    ),
  ); 
 }
}

but, my file field is not becoming visible after my input id is selected, instead is already visible.

3

If this is a file field attached to an entity, and not (for example) a managed_file element type, then the actual <input> element is contained further down the render array. At the moment you're probably attaching the JS to the wrong element (a container rather than the input).

Try something like this instead:

/**
* Implements hook_form_alter()
*/
function my_module_form_alter(&$form, &$form_state, $form_id) {
  ...
  case 'my_form_node_form': {
    $form['my_file_field'][LANGUAGE_NONE][0]['fid']['#states'] = array(
      'visible' => array(
        ':input[id="my_id"]' => array('value' => 'my_value'),
      ),
    ); 
  }
}

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.