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:
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?
Comments
Comment #1
drummThese sorts of changes need to happen in the current development version, not a stable release.
Moving to normal priority since nothing is fundamentally broken.
Comment #2
sobi3ch commentedI made small code lifting with testing and then I roll a patch.
Comment #3
dries commentedThanks 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.
Comment #4
Anonymous (not verified) commentedThe last submitted patch failed testing.
Comment #5
JStarcher commentedFor those that need this in Drupal 6.x I made a patch on 6.10 (form.inc version 1.265.2.19).
Comment #6
HoKe commentedI 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.
Comment #7
HoKe commentedHi,
please can you tell me why the patch is not in 6.12?
thx
Comment #8
JStarcher commentedI 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.
Comment #9
JStarcher commentedI'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.
Comment #10
JStarcher commentedLets get this in 7!!!!
What do I need to do to make it happen?????
Comment #11
JStarcher commentedPatch 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?
Comment #12
DeeLay commentedIs there anything further to do on this, it is marked as needs work, but the patch in #9 works. Presumably too late to get it in 7, but has it made it into 6 yet??
Comment #13
sobi3ch commented@DeeLay: #9 was ignored by simpletest so it can't go forward..
Comment #14
sobi3ch commentedOk so it's done for d7dev, please check..
Comment #16
sobi3ch commentednew approach
WOW! it's seems it's bigger issue then I thought in the begging :)
40 fail(s), and 36,738 exception(es).Comment #17
sobi3ch commentedsure
Comment #19
sobi3ch commented2nd approach
Comment #21
sobi3ch commentedI'm proposing new sub-select-#options element
#unselected
Example:
Comment #22
JStarcher commentedVery nice! Probably too late to make it into 7.x?
Comment #23
sobi3ch commentedsomeone can make a review?! please. I think it's worth...
Comment #24
retester2010 commentedtrailing white space
Powered by Dreditor.
Comment #25
sun1) I'm relatively sure that this bug does not exist in Drupal 7.
2) The patch implements something entirely different than this issue is about.
Most likely won't fix.
Comment #26
sobi3ch commented#9 was ignored because:
see more: http://drupal.org/patch/create
so I create this again from drupal root folder
@sun:
I agree > changing
yes to much coffee :)
Comment #27
sobi3ch commentedComment #28
sobi3ch commentedComment #30
sunThis is technically a duplicate of #140783: A select list without #default_value always passes form validation and cannot be fixed for D6, as it has a potential impact on every select element in all forms throughout Drupal. To understand what I'm saying, make sure to read the entire aforementioned issue.
For D6, you need to implement a custom workaround for this problem in your module; i.e., manually validate that a user selected something on your own.
Tricky hint for how to achieve this in a simple way:
Comment #31
sobi3ch commented@sun: ok I understand now. Sorry for hassle...