Commit bfedf56f authored by webchick's avatar webchick

#582584 by dww, Jacine, and chx: Move required form element marker into its own theme function.

parent 453569e7
......@@ -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',
),
......
......@@ -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'];
......@@ -2797,6 +2797,27 @@ function theme_form_element($variables) {
return $output;
}
/**
* 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.
*
......
......@@ -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");
}
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment