### Eclipse Workspace Patch 1.0 #P Drupal-7-HEAD Index: modules/field/modules/options/options.module =================================================================== RCS file: /cvs/drupal/drupal/modules/field/modules/options/options.module,v retrieving revision 1.26 diff -u -r1.26 options.module --- modules/field/modules/options/options.module 13 Apr 2010 15:23:02 -0000 1.26 +++ modules/field/modules/options/options.module 9 Jul 2010 08:31:35 -0000 @@ -137,6 +137,10 @@ * Form element validation handler for options element. */ function options_field_widget_validate($element, &$form_state) { + if ($element['#required'] && $element['#value'] == '_none') { + form_error($element, t('!name field is required.', array('!name' => $element['#title']))); + } + // Transpose selections from field => delta to delta => field, turning // multiple selected options into multiple parent elements. $items = _options_form_to_storage($element); @@ -162,7 +166,8 @@ $properties = array( // Select boxes do not support any HTML tag. 'strip_tags' => TRUE, - 'empty_value' => !$required, + // Add a "none" choice on single-value and optional select lists. + 'empty_value' => !$multiple || !$required, 'optgroups' => TRUE, ); break; Index: modules/field/modules/options/options.test =================================================================== RCS file: /cvs/drupal/drupal/modules/field/modules/options/options.test,v retrieving revision 1.13 diff -u -r1.13 options.test --- modules/field/modules/options/options.test 7 Apr 2010 17:30:43 -0000 1.13 +++ modules/field/modules/options/options.test 9 Jul 2010 08:31:36 -0000 @@ -222,6 +222,8 @@ // Display form: with no field data, nothing is selected. $this->drupalGet('test-entity/' . $entity->ftid .'/edit'); + $this->assertTrue($this->xpath('//select[@id=:id]//option[@value="_none"]', array(':id' => 'edit-card-1-' . $langcode)), t('An optional select list has a "none" choice.')); + $this->assertNoOptionSelected("edit-card-1-$langcode", '_none'); $this->assertNoOptionSelected("edit-card-1-$langcode", 0); $this->assertNoOptionSelected("edit-card-1-$langcode", 1); $this->assertNoOptionSelected("edit-card-1-$langcode", 2); @@ -243,11 +245,17 @@ $this->drupalPost('test-entity/' . $entity->ftid .'/edit', $edit, t('Save')); $this->assertFieldValues($entity_init, 'card_1', $langcode, array()); - // A required select list does not have an empty key. + // A required select list has a "none" choice. $instance['required'] = TRUE; field_update_instance($instance); $this->drupalGet('test-entity/' . $entity->ftid .'/edit'); - $this->assertFalse($this->xpath('//select[@id=:id]//option[@value=""]', array(':id' => 'edit-card-1-' . $langcode)), t('A required select list does not have an empty key.')); + $this->assertTrue($this->xpath('//select[@id=:id]//option[@value="_none"]', array(':id' => 'edit-card-1-' . $langcode)), t('A required select list has a "none" choice.')); + + // Saving a required field with the "none" option selected causes a + // validation error. + $edit = array("card_1[$langcode]" => '_none'); + $this->drupalPost(NULL, $edit, t('Save')); + $this->assertRaw(t('!title field is required.', array('!title' => $instance['field_name'])), t('Cannot save a required field when selecting "none" from the select list.')); // We do not have to test that a required select list with one option is // auto-selected because the browser does it for us.