diff options
author | Angie Byron <webchick@24967.no-reply.drupal.org> | 2009-11-02 03:00:28 +0000 |
---|---|---|
committer | Angie Byron <webchick@24967.no-reply.drupal.org> | 2009-11-02 03:00:28 +0000 |
commit | bfedf56f9d9e41eee879221adeb302fb09c072ac (patch) | |
tree | 3153308212f7622b9c62e6515d99cd73f8584574 | |
parent | 453569e72190e3c9a510fc00fa215ca88110d51a (diff) | |
download | brdo-bfedf56f9d9e41eee879221adeb302fb09c072ac.tar.gz brdo-bfedf56f9d9e41eee879221adeb302fb09c072ac.tar.bz2 |
#582584 by dww, Jacine, and chx: Move required form element marker into its own theme function.
-rw-r--r-- | includes/common.inc | 3 | ||||
-rw-r--r-- | includes/form.inc | 23 | ||||
-rw-r--r-- | modules/simpletest/tests/form.test | 34 |
3 files changed, 51 insertions, 9 deletions
diff --git a/includes/common.inc b/includes/common.inc index ce76725cf..fc1e71d29 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -5321,6 +5321,9 @@ function drupal_common_theme() { 'form_element' => array( 'render element' => 'element', ), + 'form_required_marker' => array( + 'arguments' => array('element' => NULL), + ), 'text_format_wrapper' => array( 'render element' => 'element', ), diff --git a/includes/form.inc b/includes/form.inc index 06ab7e0a6..5e3895aaa 100644 --- a/includes/form.inc +++ b/includes/form.inc @@ -2774,7 +2774,7 @@ function theme_form_element($variables) { } $output = '<div class="' . implode(' ', $class) . '">' . "\n"; - $required = !empty($element['#required']) ? '<span class="form-required" title="' . $t('This field is required.') . '">*</span>' : ''; + $required = !empty($element['#required']) ? theme('form_required_marker', array('element' => $element)) : ''; if (!empty($element['#title']) && empty($element['#form_element_skip_title'])) { $title = $element['#title']; @@ -2798,6 +2798,27 @@ function theme_form_element($variables) { } /** + * Theme the marker for required form elements. + * + * @param $variables + * An associative array containing: + * - element: An associative array containing the properties of the element. + * @return + * A string representing the marker to identify required form elements. + * + * @ingroup themeable + */ +function theme_form_required_marker($variables) { + // This is also used in the installer, pre-database setup. + $t = get_t(); + $attributes = array( + 'class' => 'form-required', + 'title' => $t('This field is required.'), + ); + return '<span' . drupal_attributes($attributes) . '>*</span>'; +} + +/** * Sets a form element's class attribute. * * Adds 'required' and 'error' classes as needed. diff --git a/modules/simpletest/tests/form.test b/modules/simpletest/tests/form.test index f6dbb85b8..f0302ff47 100644 --- a/modules/simpletest/tests/form.test +++ b/modules/simpletest/tests/form.test @@ -51,6 +51,9 @@ class FormsTestCase extends DrupalWebTestCase { $elements['file']['element'] = array('#title' => $this->randomName(), '#type' => 'file'); $elements['file']['empty_values'] = $empty_strings; + // Regular expression to find the expected marker on required elements. + $required_marker_preg = '@<label.*<span class="form-required" title="This field is required\.">\*</span></label>@'; + // Go through all the elements and all the empty values for them foreach ($elements as $type => $data) { foreach ($data['empty_values'] as $key => $empty) { @@ -69,19 +72,34 @@ class FormsTestCase extends DrupalWebTestCase { drupal_prepare_form($form_id, $form, $form_state); drupal_process_form($form_id, $form, $form_state); $errors = form_get_errors(); + // Form elements of type 'radios' throw all sorts of PHP notices + // when you try to render them like this, so we ignore those for + // testing the required marker. + // @todo Fix this work-around (http://drupal.org/node/588438). + $form_output = ($type == 'radios') ? '' : drupal_render($form); if ($required) { // Make sure we have a form error for this element. $this->assertTrue(isset($errors[$element]), "Check empty($key) '$type' field '$element'"); - } - elseif ($type == 'select') { - // Select elements are going to have validation errors with empty - // input, since those are illegal choices. Just make sure the - // error is not "field is required". - $this->assertTrue((empty($errors[$element]) || strpos('field is required', $errors[$element]) === FALSE), "Optional '$type' field '$element' is not treated as a required element"); + if (!empty($form_output)) { + // Make sure the form element is marked as required. + $this->assertTrue(preg_match($required_marker_preg, $form_output), "Required '$type' field is marked as required"); + } } else { - // Make sure there is *no* form error for this element. - $this->assertTrue(empty($errors[$element]), "Optional '$type' field '$element' has no errors with empty input"); + if (!empty($form_output)) { + // Make sure the form element is *not* marked as required. + $this->assertFalse(preg_match($required_marker_preg, $form_output), "Optional '$type' field is not marked as required"); + } + if ($type == 'select') { + // Select elements are going to have validation errors with empty + // input, since those are illegal choices. Just make sure the + // error is not "field is required". + $this->assertTrue((empty($errors[$element]) || strpos('field is required', $errors[$element]) === FALSE), "Optional '$type' field '$element' is not treated as a required element"); + } + else { + // Make sure there is *no* form error for this element. + $this->assertTrue(empty($errors[$element]), "Optional '$type' field '$element' has no errors with empty input"); + } } } } |