diff --git a/core/modules/simpletest/tests/form.test b/core/modules/simpletest/tests/form.test index 49933c3..222549a 100644 --- a/core/modules/simpletest/tests/form.test +++ b/core/modules/simpletest/tests/form.test @@ -129,6 +129,7 @@ class FormsTestCase extends DrupalWebTestCase { * and each is checked for the proper #required error messages. * * @see form_test_validate_required_form() + * @see form_test_element_validate() */ function testRequiredCheckboxesRadio() { $form = $form_state = array(); @@ -142,10 +143,13 @@ class FormsTestCase extends DrupalWebTestCase { $this->assertText(t('!name field is required.', array('!name' => $form[$key]['#title']))); } - // The unhelpful catch-all generic error should not appear. - $this->assertNoText(t('An illegal choice has been detected. Please contact the site administrator.')); + // No invalid options were submitted, only empty ones. Therefore, the error + // message about disallowed values should not appear, nor should our custom + // validator report that the value is not found. + $this->assertNoText(t('illegal choice')); + $this->assertNoText(t('Value is not allowed')); - // Verify that no error appears with valid values. + // Verify that no errors appear with valid values. $edit = array( 'textfield' => $this->randomString(), 'checkboxes[foo]' => TRUE, @@ -157,7 +161,8 @@ class FormsTestCase extends DrupalWebTestCase { foreach (array('textfield', 'checkboxes', 'select', 'radios') as $key) { $this->assertNoText(t('!name field is required.', array('!name' => $form[$key]['#title']))); } - $this->assertNoText(t('An illegal choice has been detected. Please contact the site administrator.')); + $this->assertNoText(t('illegal choice')); + $this->assertNoText(t('Value is not allowed')); } /** diff --git a/core/modules/simpletest/tests/form_test.module b/core/modules/simpletest/tests/form_test.module index 73bda37..6f57d92 100644 --- a/core/modules/simpletest/tests/form_test.module +++ b/core/modules/simpletest/tests/form_test.module @@ -359,29 +359,34 @@ function form_test_validate_required_form($form, &$form_state) { '#type' => 'textfield', '#title' => 'Textfield', '#required' => TRUE, + '#element_validate' => array('form_test_element_validate'), ); $form['checkboxes'] = array( '#type' => 'checkboxes', '#title' => 'Checkboxes', '#options' => drupal_map_assoc(array('foo', 'bar')), '#required' => TRUE, + '#element_validate' => array('form_test_element_validate'), ); $form['select'] = array( '#type' => 'select', '#title' => 'Select', '#options' => drupal_map_assoc(array('foo', 'bar')), '#required' => TRUE, + '#element_validate' => array('form_test_element_validate'), ); $form['radios'] = array( '#type' => 'radios', '#title' => 'Radios', '#options' => drupal_map_assoc(array('foo', 'bar')), '#required' => TRUE, + '#element_validate' => array('form_test_element_validate'), ); $form['radios_optional'] = array( '#type' => 'radios', '#title' => 'Radios (optional)', '#options' => drupal_map_assoc(array('foo', 'bar')), + '#element_validate' => array('form_test_element_validate'), ); $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array('#type' => 'submit', '#value' => 'Submit'); @@ -389,6 +394,37 @@ function form_test_validate_required_form($form, &$form_state) { } /** + * Form element validation handler for form_test_validate_required_form(). + * + * Checks the form element for values that are not in the allowed values, + * using logic from _form_validate(). + */ +function form_test_element_validate($element, &$form_state, $form) { + if (isset($element['#options']) && isset($element['#value'])) { + if ($element['#type'] == 'select') { + $options = form_options_flatten($element['#options']); + } + else { + $options = $element['#options']; + } + if (is_array($element['#value'])) { + $value = in_array($element['#type'], array('checkboxes', 'tableselect')) ? array_keys($element['#value']) : $element['#value']; + foreach ($value as $v) { + if (!isset($options[$v])) { + drupal_set_message("Value is not allowed for {$element['#name']}."); + } + } + } + elseif ($element['#type'] == 'select' && !$element['#multiple'] && $element['#required'] && !isset($element['#default_value']) && $element['#value'] === $element['#empty_value']) { + drupal_set_message("Value is not allowed for {$element['#name']}."); + } + elseif (!isset($options[$element['#value']])) { + drupal_set_message("Value is not allowed for {$element['#name']}."); + } + } +} + +/** * Builds a simple form with a button triggering partial validation. */ function form_test_limit_validation_errors_form($form, &$form_state) {