Last updated May 16, 2011. Created by danielb on May 16, 2011.
Log in to edit this page.

The Select (or other) module provides a Forms API element, and integrates it with the Fields API. However, developers and coders can make use of this element in a custom form for any purpose. This page outlines the developer usage and available attributes.

Example developer usage in Forms API

<?php
$form
['my_field'] = array(
 
'#type' => 'select_or_other',
 
'#title' => t('Choose an option'),
 
'#default_value' => array('value_1'),
 
'#options' => array(
   
'value_1' => t('One'),
   
'value_2' => t('Two'),
   
'value_3' => t('Three'),
  ),
 
'#other' => t('Other (please type a value)'),   // Text to show as 'other' option
 
'#required' => TRUE,
 
'#multiple' => TRUE,
 
'#other_unknown_defaults' => 'other', // If the #default_value is not a valid choice in #options, what should we do? Possible values 'append', 'ignore', 'other'  (if 'other' specified you can also override #other_delimiter).
 
'#other_delimiter' => ', ', // Delimiter string to delimit multiple 'other' values into the 'other' textfield.  If this is FALSE only the last value will be used.
 
'#select_type' => 'select', // Defaults to 'select'.  Can also be 'radios' or 'checkboxes'.
);
?>

The #properties you give the element that are recognised as FAPI select properties will be automatically assigned to the Select element. Also any #properties assigned as #select_property or #other_property will be passed down to the corresponding form element.

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

I did not see this mentioned anywhere but this is a little bit of CSS that will insert a line of text before/above the other text field when it appears.

#edit-submitted-stateprovincecode-other-wrapper:before {
content: "Please enter the State that is not on the list.";
font-weight:bold;
}

Of course you need to change the field name to whatever you are using.

The CSS :before selector is unsupported in IE7 and below, with only partial support in IE8. So use this with caution. It would be better to use a hook to add this content in instead.

The way I do it is to use either #other_prefix or #other_field_prefix. Simple.

I'm some-what new to Drupal and am familiar with PHP, but just barely. With that being said, can I cut and past the above code into the "allowed values PHP code" on the "field settings" page for it to work? Is that how I can get this module to work, or is it something else?

Thanks.

No. This documentation is for module developers. If using with fields use the 'text' or numeric field types and choose Select (or other) to be the widget.

Very informative. Thank you for thinking of developers :)

If other is selected along with another option, is this counted as valid, invalid, or is it configurable? I need a multiple checkbox widget that allows me to collect at least one selection, including other. The ability to write in that other is icing on the cake. Does this module work for me?

The other Andrew Morton

Yes it should. Think of the whole thing like a textfield where anything goes, except there are a few easy select options to help users out or nudge them in the right direction.

I assume the "other" value does not end up adding the entry to the vocabulary as a new term, but it might be worth clarifying how "other" value is handled.

_____ ____ ___ __ _ _
Laura Scott :: design » blog » tweet

On the field edit page, the "Other value as default value" allows you to select how other will be handled:

  • Add the values to the other textfield-> store the value but don't change the list
  • Append the values to the current list-> store the value and add to the list attached to the node/user/entity
  • Append the values to the available options-> store the value and add the value to the list so that it can be selected on other nodes/users/entities
  • Ignore the values-> do not store the value

Hi,

I'm currently using this element as a select dropdown. Options are filled with a key/value array, with the key being the ID of the list items (which I'll use to submit). But when something is entered in the "other" field instead, I'll be submitting through another method to send the entered text.

So my question is, is there was a way to identify if a value was selected or if something was entered in the "other" field? I could check that if the value is a long then it is an ID, otherwise it's not, but then again someone could enter numbers in the "other" field, and I don't think anyways that it's the best method to verify this...

Thanks in advance,
Bruno

#other_element_validate => array('my_other_element_validation'),

Steve Turnbull
The Drupal7-ish Blog