2

The code

     $form['type'] = array(
      '#type' => 'select', 
      '#title' => t('Type of new field'),
      '#options' => $field_type_options,
      '#empty_option' => t('- Select a field type -'),
      // '#empty_value' => 0,
      '#description' => t('Type of data to store.'),
      '#required' => TRUE,
    );

    $form['save'] = array (
     '#type' => 'submit',
     '#value' => t ('Save field'),
     '#states' => array(
       'disabled' => array(
         ':input[name="type"]' => array('value' => 0),
         // Tried value = NULL, value = FALSE - no luck
)),

);

The goal is to disable submit button if none selected from drop-down list. Which "value" should I provide? Tried "0", "FALSE", "NULL"

  • 1
    i think you can't catch the select's changed event, so you need to write some custom javascript attachted via a drupal behavoir or a module hoooking AJAX Form – rémy Apr 3 '12 at 22:25
4

The "#empty_option" will not give a value, which is needed for #states, a workaround is to add the default as an actual option:

$field_type_options = array( 0 => '- Select a field type -');

$form['type'] = array(
  '#type' => 'select',
  '#title' => t('Type of new field'),
  '#options' => $field_type_options,
  '#description' => t('Type of data to store.'),
  '#default_value' => isset( $form_state['values']['type'] ) ?  $form_state['values']['type'] : 0,
  '#required' => TRUE,
  );

  $form['save'] = array (
   '#type' => 'submit',
   '#value' => t ('Save field'),
   '#states' => array(
     'disabled' => array(
       ':input[name="type"]' => array('value' => 0),
      )
    )
  );

This allows #states to test the value and disable the item

|improve this answer|||||
1

The proper way to do this (no #empty_option workaround) is to check for the empty string ('') value.

'#states' => array(
  'disabled' => array(
    ':input[name="type"]' => array('value' => ''),
  ),
),
|improve this answer|||||
0

You should add the #empty_value at the same time.

empty_option is the text empty_value is the value

 $form['type'] = array(        
'#type' => 'select',
'#title' => t('Type of new field'),
'#options' => $field_type_options,
'#empty_option' => t('- Select a field type -'),
'#empty_value' => 'blank',
'#description' => t('Type of data to store.'),
'#required' => TRUE,
);

Hope it works for you

|improve this answer|||||

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.