0

How to remove empty option from the select list in drupal forms.

$form['property'] = [
    '#type'          => 'select',
    '#id'            => 'property',
    '#attributes'    => ['class' => ['form-control']],
    '#title'         => $this->t('Hotel'),
    '#options'       => $properties,
    '#empty_option'  => t('Select Category'),
    '#required'      => TRUE,
    '#validated' => True,
];

I have removed the line '#empty_option' => t('Select Category') from the code but in the front end it shows blank for the first option. I need to select the first element of the $properties array to be selected as default.

  • 1
    set a default value #default_value? – 4k4 Dec 19 '17 at 7:53
  • @4k4 setting '#default_value' => false and removing '#empty_option' => t('Select Category'), was the trick. thanks !! – i am batman Dec 19 '17 at 8:14

Your Answer

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

Browse other questions tagged or ask your own question.