Drupal - Removing the default "-Select-" option from select form elements

I needed to remove the default "-None-"/"-Select-" option that Drupal's Form API adds to "select" form elements. Let's take an example. Consider the following "select" element definition for a form:

 

$element = array (
			'#type' => 'select',
			'#options' => array (
							1 => 'Very poor',
							2 => 'Not that bad',
							3 => 'Average',
							4 => 'Good',
							5 => 'Perfect',
						),
			'#required' => TRUE,
			'#title' => 'Rating',
		);

 

Drupal Form SelectIt looks like the image on the right when rendered. Notice the additional "-Select-" option that Drupal added for data entry. I did not want that option to appear on the data entry form.

Some studying of Drupal docs and searching did not threw up anything. But I was able to achieve what I needed with some testing. Here's what I did.

I added an '#after_build' method for the form element like below:

 

$element = array (
			'#type' => 'select',
			'#options' => array (
							1 => 'Very poor',
							2 => 'Not that bad',
							3 => 'Average',
							4 => 'Good',
							5 => 'Perfect',
						),
			'#required' => TRUE,
			'#title' => 'Rating',
			'#after_build' => array('_eventbook_get_rating_widget_after_build'),
		);

 

And in the '#after_build' method, unsetted the non-desired option from the '#options' array:

 

{syntaxhighlighter brush: php;fontsize: 100; first-line: 1; }function _eventbook_get_rating_widget_after_build ($element, &$form_state) { //Remove the default option. unset($element['#options']['']); return ($element); }{/syntaxhighlighter}

It was easy in the end, but would have been more useful if Drupal provided an attribute to prevent this default option from being added automatically.

 

Comments

thank you so much!

I'd like to put the callback function within an include file, and use module_load_include() in the form. However, doing so throws up a Call to undefined function error after pressing submit. Is there a way of doing that so it works?

rahul's picture

Hi Catherine, module_load_include returns FALSE if the load is unsuccessful. You would want to verify 2 things:

  1. You are specifying the parameters for this function correctly, and that it is not returning FALSE.
  2. You are invoking this method in the correct place, normally you would call it in your module's init hook. Note if you are calling it in hook_form_alter, there might be issues as you can read in this blog post.

Thanks for the reply. The parameters are correct, and the the function is being called inside the function that is building the form.

You got me thinking though and I ended up using form_load_include, rather than module_load_include, as i'm using Drupal 7. Seems to work well so I'm happy. Thanks for the post, my Select fields look much better now.

rahul's picture

There you have it, if the form is cached, your form building function would not be called and hence you have that undefined function error on form submission. form_load_include is definitely the right option to choose in this case.

Hi,

I am trying your solution, but its not coming through. I hve the following code in my hook_form_alter:

$form['field_tuv_interview_question'] = array (
'#type' => 'select',
'#options' => $options,
'#required' => TRUE,
'#title' => 'Questions',
'#after_build' => array('_field_tuv_interview_question_after_build'),
);

I get the select list on the form. Then I have the function :

function _field_tuv_interview_question_after_build($element, &$form_state) {
//Remove the default option.
unset($element['#options']['']);
return ($element);
}

Thanks,
Viv

rahul's picture

Hmmm... strange. After the form renders, I would check if the default option's value is an empty string (''). Then I would try to do a print_r on $element to see how does options look like.

Thank you so much, works perfect !