Select + textfield
John Morahan - June 9, 2008 - 13:09
| Project: | Elements |
| Version: | 5.x-1.x-dev |
| Component: | Code |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | needs review |
Description
This is a select element with an 'Other...' option which pops up a textfield where the user can type in a value directly.
| Attachment | Size |
|---|---|
| elements-comboselect.patch | 3.41 KB |

#1
An excellent, feature rich addition to the elements module.
I've only one issue with the current element. Submitting the form below with the 'Second Option' selected will get me 'Second Option' as form value in the submit handler.
Should it not return the _key_ of the option, similar to the way #type select behaves or, when 'Other' has been selected, the value of the texfield?
The same holds for the behaviour of #default_value; shouldn't it accept keys (but then again, what to do when key is the value of the other option?)?
<?php
$form = array();
$options = array(
'one' => t('First Option'),
'two' => t('Second Option'),
);
$form['test'] = array(
'#type' => 'comboselect',
'#title' => t('Example'),
'#required' => TRUE,
'#default_options' => $options,
'#default_value' => 'two', // won't work, needs to be t('Second Option')
);
?>
To use the element in its current inception, you'd have to pass the #default_options array as #type value to the submit handler so it can reconstruct the key selected . One also has to choose the right translated value for #default_value in the builder.
#2
The trouble is that it's a combination of two elements, one of which naturally returns a key, the other naturally returns a value.
How about something like this? The value is now e.g. array('option' => 'two', 'value' => 'Second option') and the #default_value would be array('option' => 'two') for the select, or array('value' => t('Third option')) for the textfield.
This gives the developer a way to tell the difference between a selected option and a typed value that's identical to an existing key or value.
(I haven't had time to test this version extensively yet)
#3
Uhh, there was supposed to be a patch attached to that, but I guess it didn't upload and now I'm in the office and the patch is at home... I'll upload it this evening.
#4
Seems there have been some upload problems all round, today. Let's try this one again.