From b6b300482d1605ce76d002140f78a9d3c4c6ca77 Mon Sep 17 00:00:00 2001 From: Bob Vincent Date: Tue, 2 Aug 2011 17:54:33 -0400 Subject: [PATCH] Issue #811542 by cdale, Nick Lewis, sun, bojanz, mincn, chx, pillarsdotnet: Provide helpful error message when required radios form element is not selected. --- modules/field/modules/list/tests/list.test | 4 ++ modules/simpletest/tests/form.test | 39 ++++++++++++++++++++++++- modules/simpletest/tests/form_test.module | 43 ++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 1 deletions(-) diff --git a/modules/field/modules/list/tests/list.test b/modules/field/modules/list/tests/list.test index dec09560f8761eeccc31b5aa646815ebd618ba65..eade6eb897e4036c46ba2d20f4e887a154302661 100644 --- a/modules/field/modules/list/tests/list.test +++ b/modules/field/modules/list/tests/list.test @@ -341,6 +341,10 @@ class ListFieldUITestCase extends FieldTestCase { 'entity_type' => 'node', 'bundle' => $this->type, ); + if ($type === 'list_boolean') { + $instance['widget']['type'] = 'options_onoff'; + } + field_create_instance($instance); $this->admin_path = 'admin/structure/types/manage/' . $this->hyphen_type . '/fields/' . $this->field_name; diff --git a/modules/simpletest/tests/form.test b/modules/simpletest/tests/form.test index 4d9692906156e391b3c77bc2f826d2402c8a4d32..ef986a558b47f7c91ed497bbcbce1d584f08b45a 100644 --- a/modules/simpletest/tests/form.test +++ b/modules/simpletest/tests/form.test @@ -122,6 +122,43 @@ class FormsTestCase extends DrupalWebTestCase { } /** + * Tests #required checkboxes and radios. + * + * This is a functional test while testRequiredFields above attempts unit. + * + * @see form_test_validate_required_form() + */ + function testRequiredCheckboxesRadio() { + $form = $form_state = array(); + $form = form_test_validate_required_form($form, $form_state); + + // Verify each error message shows when required value not selected. + $edit = array(); + $this->drupalPost('form-test/validate-required', $edit, 'Submit'); + + foreach (array('textfield', 'checkboxes', 'select', 'radios') as $key) { + $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.')); + + // Verify that no error appears with valid values. + $edit = array( + 'textfield' => $this->randomString(), + 'checkboxes[foo]' => TRUE, + 'select' => 'foo', + 'radios' => 'bar', + ); + $this->drupalPost('form-test/validate-required', $edit, 'Submit'); + + 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.')); + } + + /** * Test default value handling for checkboxes. * * @see _form_test_checkbox() @@ -521,7 +558,7 @@ class FormValidationTestCase extends DrupalWebTestCase { */ function testValidateLimitErrors() { $edit = array( - 'test' => 'invalid', + 'test' => 'invalid', 'test_numeric_index[0]' => 'invalid', 'test_substring[foo]' => 'invalid', ); diff --git a/modules/simpletest/tests/form_test.module b/modules/simpletest/tests/form_test.module index 00be7d2e68de9917e7b527364630c8942a630d02..50893c09df3f2c44dab3303ba0bb3f1f07e789be 100644 --- a/modules/simpletest/tests/form_test.module +++ b/modules/simpletest/tests/form_test.module @@ -23,6 +23,12 @@ function form_test_menu() { 'access arguments' => array('access content'), 'type' => MENU_CALLBACK, ); + $items['form-test/validate-required'] = array( + 'title' => 'Form #required validation', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('form_test_validate_required_form'), + 'access callback' => TRUE, + ); $items['form-test/limit-validation-errors'] = array( 'title' => 'Form validation with some error suppression', 'page callback' => 'drupal_get_form', @@ -332,6 +338,43 @@ function form_test_validate_form_validate(&$form, &$form_state) { } /** + * Form constructor for simple #required tests. + */ +function form_test_validate_required_form($form, &$form_state) { + $form['textfield'] = array( + '#type' => 'textfield', + '#title' => 'Textfield', + '#required' => TRUE, + ); + $form['checkboxes'] = array( + '#type' => 'checkboxes', + '#title' => 'Checkboxes', + '#options' => drupal_map_assoc(array('foo', 'bar')), + '#required' => TRUE, + ); + $form['select'] = array( + '#type' => 'select', + '#title' => 'Select', + '#options' => drupal_map_assoc(array('foo', 'bar')), + '#required' => TRUE, + ); + $form['radios'] = array( + '#type' => 'radios', + '#title' => 'Radios', + '#options' => drupal_map_assoc(array('foo', 'bar')), + '#required' => TRUE, + ); + $form['radios_optional'] = array( + '#type' => 'radios', + '#title' => 'Radios (optional)', + '#options' => drupal_map_assoc(array('foo', 'bar')), + ); + $form['actions'] = array('#type' => 'actions'); + $form['actions']['submit'] = array('#type' => 'submit', '#value' => 'Submit'); + return $form; +} + +/** * Builds a simple form with a button triggering partial validation. */ function form_test_limit_validation_errors_form($form, &$form_state) { -- 1.7.4.1