From cade6b2bdacd39923f2ed1bf581c035b9be92c62 Mon Sep 17 00:00:00 2001 From: Dries Buytaert <dries@buytaert.net> Date: Fri, 6 Jan 2006 07:15:47 +0000 Subject: [PATCH] - Patch #39179 by chx; make optgroups work with choice checker. --- includes/form.inc | 45 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/includes/form.inc b/includes/form.inc index bd6a5f18f5d9..e4c520c39d6e 100644 --- a/includes/form.inc +++ b/includes/form.inc @@ -149,15 +149,21 @@ function _form_validate($elements, $form_id = NULL) { // Add legal choice check if element has #options. if (isset($elements['#options']) && isset($elements['#value'])) { $message = t('Illegal choice in %title.', array('%title' => theme('placeholder', $elements['#title']))); + if ($elements['#type'] == 'select') { + $options = form_options_flatten($elements['#options']); + } + else { + $options = $elements['#options']; + } if (is_array($elements['#value'])) { $value = $elements['#type'] == 'checkboxes' ? array_keys(array_filter($elements['#value'])) : $elements['#value']; foreach ($value as $v) { - if (!isset($elements['#options'][$v])) { + if (!isset($options[$v])) { form_error($elements, $message); } } } - elseif (!isset($elements['#options'][$elements['#value']])) { + elseif (!isset($options[$elements['#value']])) { form_error($elements, $message); } } @@ -429,6 +435,25 @@ function _element_info($type, $refresh = null) { return $cache[$type]; } +function form_options_flatten($array, $reset = TRUE) { + static $return; + + if ($reset) { + $return = array(); + } + + foreach ($array as $key => $value) { + if (is_array($value)) { + form_options_flatten($value, FALSE); + } + else { + $return[$key] = 1; + } + } + + return $return; +} + /** * Format a dropdown menu or scrolling selection box. * @@ -449,12 +474,24 @@ function theme_select($element) { if (is_array($choice)) { $select .= '<optgroup label="'. $key .'">'; foreach ($choice as $key => $choice) { - $select .= '<option value="'. $key .'"'. (is_array($element['#value']) ? (in_array($key, $element['#value']) ? ' selected="selected"' : '') : ($element['#value'] == $key ? ' selected="selected"' : '')) .'>'. check_plain($choice) .'</option>'; + if (isset($element['#value']) && ($element['#value'] == $key || (is_array($element['#value']) && in_array($key, $element['#value'])))) { + $selected = ' selected="selected"'; + } + else { + $selected = ''; + } + $select .= '<option value="'. $key .'"'. $selected .'>'. check_plain($choice) .'</option>'; } $select .= '</optgroup>'; } else { - $select .= '<option value="'. $key .'"'. (is_array($element['#value']) ? (in_array($key, $element['#value']) ? ' selected="selected"' : '') : ($element['#value'] == $key ? ' selected="selected"' : '')) .'>'. check_plain($choice) .'</option>'; + if (isset($element['#value']) && ($element['#value'] == $key || (is_array($element['#value']) && in_array($key, $element['#value'])))) { + $selected = ' selected="selected"'; + } + else { + $selected = ''; + } + $select .= '<option value="'. $key .'"'. $selected .'>'. check_plain($choice) .'</option>'; } } return theme('form_element', $element['#title'], '<select name="'. $element['#name'] .''. ($element['#multiple'] ? '[]' : '') .'"'. ($element['#multiple'] ? ' multiple="multiple" ' : '') . drupal_attributes($element['#attributes']) .' id="' . $element['#id'] .'" '. $size .'>'. $select .'</select>', $element['#description'], $element['#id'], $element['#required'], form_get_error($element)); -- GitLab