Warn when the specified selected value isn't in the passed options. Don't default to adding id in htmlSelect. Shouldbe passed in behaviours

This commit is contained in:
Isaac Connor 2020-09-03 10:42:33 -04:00
parent fb9129a4d3
commit 94c7708df5
1 changed files with 10 additions and 5 deletions

View File

@ -496,13 +496,13 @@ function htmlSelect($name, $contents, $values, $behaviours=false) {
} }
} }
return "<select name=\"$name\" id=\"$name\"$behaviourText>".htmlOptions($contents, $values).'</select>'; return "<select name=\"$name\" $behaviourText>".htmlOptions($contents, $values).'</select>';
} }
function htmlOptions($contents, $values) { function htmlOptions($options, $values) {
$options_html = ''; $options_html = '';
$has_selected = false;
foreach ( $contents as $value=>$option ) { foreach ( $options as $value=>$option ) {
$disabled = 0; $disabled = 0;
$text = ''; $text = '';
if ( is_array($option) ) { if ( is_array($option) ) {
@ -521,14 +521,19 @@ function htmlOptions($contents, $values) {
$text = $option; $text = $option;
} }
$selected = is_array($values) ? in_array($value, $values) : !strcmp($value, $values); $selected = is_array($values) ? in_array($value, $values) : !strcmp($value, $values);
$has_selected = (!$has_selected) && $selected;
$options_html .= '<option value="'.htmlspecialchars($value, ENT_COMPAT | ENT_HTML401, ini_get('default_charset'), false).'"'. $options_html .= '<option value="'.htmlspecialchars($value, ENT_COMPAT | ENT_HTML401, ini_get('default_charset'), false).'"'.
($selected?' selected="selected"':''). ($selected?' selected="selected"':'').
($disabled?' disabled="disabled"':''). ($disabled?' disabled="disabled"':'').
'>'.htmlspecialchars($text, ENT_COMPAT | ENT_HTML401, ini_get('default_charset'), false).'</option> '>'.htmlspecialchars($text, ENT_COMPAT | ENT_HTML401, ini_get('default_charset'), false).'</option>
'; ';
} # end foreach options
if ( $values and ! $has_selected ) {
ZM\Warning("Specified value $values not in contents: ".print_r($options, true));
} }
return $options_html; return $options_html;
} } # end function htmlOptions
function truncText($text, $length, $deslash=1) { function truncText($text, $length, $deslash=1) {
return preg_replace('/^(.{'.$length.',}?)\b.*$/', '\\1&hellip;', ($deslash?stripslashes($text):$text)); return preg_replace('/^(.{'.$length.',}?)\b.*$/', '\\1&hellip;', ($deslash?stripslashes($text):$text));