Select elements which are required can be submitted without any user input
JStarcher - January 6, 2008 - 05:36
| Project: | Drupal |
| Version: | 7.x-dev |
| Component: | forms system |
| Category: | bug report |
| Priority: | normal |
| Assigned: | sobi3ch |
| Status: | needs work |
Description
If there is a select element on the page which does not have the default value set and the element is set to required, the form will default the the first item in the #options array. This means that the user can skip over the "required" field and submit the field without ever selecting a value, and the result will be whatever value is first in the #options array.
I have created a fix for this. It seems to work great but I am not too fluent with the FAPI yet to know if my fix is the best way of going about it.
Here is what I did:
<?php
function form_builder($form_id, $form) {
global $form_values, $form_submitted, $form_button_counter;
// Initialize as unprocessed.
$form['#processed'] = FALSE;
/* Use element defaults */
if ((!empty($form['#type'])) && ($info = _element_info($form['#type']))) {
// Overlay $info onto $form, retaining preexisting keys in $form.
$form += $info;
}
// This is the part I added
if ($form['#type'] == 'select') {
if ($form['#required'] && !$form['#default_value']) {
$form['#options'] = array_reverse($form['#options'], true);
$form['#options'][''] = '';
$form['#options'] = array_reverse($form['#options'], true);
$form['#default_value'] = '';
}
}
?>Does this look correct?

#1
These sorts of changes need to happen in the current development version, not a stable release.
Moving to normal priority since nothing is fundamentally broken.
#2
I made small code lifting with testing and then I roll a patch.
#3
Thanks for re-rolling and improving. Don't mark your own patches as RTBC -- I'd like to get another person to review this as well.
#4
The last submitted patch failed testing.
#5
For those that need this in Drupal 6.x I made a patch on 6.10 (form.inc version 1.265.2.19).
#6
I tested it.
it works fine with ( | ). But I would expect not to need to use it :(
It also works fine if I get my values by a code in allowed values - PHP code. The only problem is that the value is not showed in field settings default value select list. On the form is it than available.
It does not work with a node reference. There is still no default value available :(
any ideas to get it work with node reference?
I would expect to have the value at each time as an available option that cant be submited if the field is required.
Once again, thx for your contribution, it brought me forward.
#7
Hi,
please can you tell me why the patch is not in 6.12?
thx
#8
I wish someone could tell me that too. It's weird that if the element is set to not being required it starts with a - None - option but as soon as you select required it sets the first item in the list as default.
Please fix this for 7.x, if I need to do more legwork to make it happen I am definitely willing as this is a big issue IMHO.
I will create a Drupal 7 patch this evening.
#9
I'm not sure why this patch is failing testing, can anyone shine some light?
Also I made a small change to the 6.x patch, changed the default option from "" to "- None -" to keep it consistent with the core and CCK.
#10
Lets get this in 7!!!!
What do I need to do to make it happen?????
#11
Patch in #9 has been tested on the latest Drupal 6.14 and confirmed working.
Still no word on how I can push this into D7 before launch?