From 0a313c877ebb63231ae2e0eaedcb8dcd39226303 Mon Sep 17 00:00:00 2001 From: Bob Vincent Date: Fri, 27 May 2011 19:27:06 -0400 Subject: [PATCH] Issue #811542 by cdale, Nick Lewis, sun, bojanz, mincn, chx: Provide helpful error message when required radios form element is not selected. --- includes/form.inc | 33 +++++++++++++ modules/simpletest/tests/form.test | 74 +++++++++++++++++++++++++++++ modules/simpletest/tests/form_test.module | 43 +++++++++++++++++ 3 files changed, 150 insertions(+), 0 deletions(-) diff --git a/includes/form.inc b/includes/form.inc index c0e2ec7c1556098585935ecb579fd0dc203d6d73..c1e34eb74a6dacf6deb2b1e4ce65b157df597bd6 100644 --- a/includes/form.inc +++ b/includes/form.inc @@ -2190,6 +2190,39 @@ function form_type_checkbox_value($element, $input = FALSE) { } /** + * Helper function to determine the value for a radios form element. + * + * @param $element + * The form element whose value is being populated. + * @param $input + * The incoming input to populate the form element. If this is FALSE, + * the element's default value is returned. + * @return + * The data that will appear in the $element_state['values'] collection + * for this element. Return nothing to use the default. + */ +function form_type_radios_value(&$element, $input = FALSE) { + // User submitted. + if ($input !== FALSE) { + // User submission and nothing selected. + if (!isset($input)) { + // We do not want form builder to override our NULL with an empty string. + $element['#has_garbage_value'] = TRUE; + // There was a user submission so validation is a must. This way, the + // required checker will produce an error as necessary. + $element['#needs_validation'] = TRUE; + } + // Whatever the user submission was, that's the value. + return $input; + } + // Default value now. + if (isset($element['#default_value'])) { + return $element['#default_value']; + } + // NULL. +} + +/** * Helper function to determine the value for a checkboxes form element. * * @param $element diff --git a/modules/simpletest/tests/form.test b/modules/simpletest/tests/form.test index e7ae9de3f9e058065d3272a1ce9d4cb1335b1b70..0eb56e1e2050005f4b4a0b123ce4f8e67e97eaf0 100644 --- a/modules/simpletest/tests/form.test +++ b/modules/simpletest/tests/form.test @@ -122,6 +122,80 @@ 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.')); + } + + /** + * 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() 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