Recently I had this issue on one of our sites... I have come up with a quick fix
which has been tested with Custom (menu) Lists only. In this instance the Admin wanted to force the Student to select an option from a custom menu field. I have verified this modification works on 1.8 and up. Most of the problems I have seen involving this is not actually a Moodle bug but a matter
of implementing validation using Pear HTML_QuickForm_Rules.
Here is what I came up with for user profiles (./user/edit.php) . This will
force any custom menu field to include a select one option by default and
enable required validation on that field.
File: ./user/edit.php
Affects Validation Behavior for element: profile_field_Otherfields
Affects Validation JScript: validate_user_edit_form_profile_field_Otherfields(element)
Notes:
5/14/2008: Fixed Custom Field List Box Validation (required) behavior.
FileName: ./lib/pear/HTML/QuickForm/RuleRegistry.php (Starting Line 284)
} elseif ($element->getType() == 'select') {
if ($element->getMultiple()) {
$elementName .= '[]';
$value =
" value{$jsIndex} = new Array();\n" .
" var valueIdx = 0;\n" .
" for (var i = 0; i < frm.elements['{$elementName}'].options.length; i++) {\n" .
" if (frm.elements['{$elementName}'].options[i].selected) {\n" .
" value{$jsIndex}[valueIdx++] = frm.elements['{$elementName}'].options[i].value;\n" .
" }\n" .
" }\n";
} else {
$value = " value{$jsIndex} = frm.elements['{$elementName}'].selectedIndex == 0? '': frm.elements['{$elementName}'].options[frm.elements['{$elementName}'].selectedIndex].value;\n";
}
Make sure First Option of Custom Lists are of type Select-One:
FileName: ./usr/profile/field/menu/field.class.php (Line 12-28)
/*
- Make sure Custom Lists are Zero Indexed
- with Select-One list type behavior.
- JTM wink
*/
function profile_field_menu($fieldid=0, $userid=0) {
//first call parent constructor
$this->profile_field_base($fieldid, $userid);
/// Param 1 for menu type is the options
$options = explode("\n", $this->field->param1);
$this->options = array();
$this->options[-1]="Select one"; // Default List option for validation Select-One.
foreach($options as $key => $option) {
$this->options[$key] = format_string($option);//multilang formatting
}
/// Set the data key
if ($this->data !== NULL) {
$this->datakey = (int)array_search($this->data, $this->options);
}
}
See Screenshot: Set as Required. Not required on Edit Advanced Page or Email signup page.