From cade6b2bdacd39923f2ed1bf581c035b9be92c62 Mon Sep 17 00:00:00 2001 From: Dries Buytaert Date: Fri, 6 Jan 2006 07:15:47 +0000 Subject: - Patch #39179 by chx; make optgroups work with choice checker. --- includes/form.inc | 45 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) (limited to 'includes') diff --git a/includes/form.inc b/includes/form.inc index bd6a5f18f..e4c520c39 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 .= ''; foreach ($choice as $key => $choice) { - $select .= ''; + if (isset($element['#value']) && ($element['#value'] == $key || (is_array($element['#value']) && in_array($key, $element['#value'])))) { + $selected = ' selected="selected"'; + } + else { + $selected = ''; + } + $select .= ''; } $select .= ''; } else { - $select .= ''; + if (isset($element['#value']) && ($element['#value'] == $key || (is_array($element['#value']) && in_array($key, $element['#value'])))) { + $selected = ' selected="selected"'; + } + else { + $selected = ''; + } + $select .= ''; } } return theme('form_element', $element['#title'], '', $element['#description'], $element['#id'], $element['#required'], form_get_error($element)); -- cgit v1.2.3