开发者

form helper dropdown fine tune

Using form_dropdown in one form with the following options:

<select name="depto">
<option value="">[todos los departamentos]</option>
<option value="0001">First</option>
<option value="01">Second</option>
</select>

When the field has any value different of empty, that is "0001" or "01", in both cases the form_dropdown generates

<select name="depto">
<option value="">[todos los departamentos]</option>
<option selected="selected" value="0001">First</option>
<option selected="selected" value="01">Second</option>
</select>

marking as selected the 2 options.

Looking inside the forms helper, the in_array() function is used to check the value.

I changed the form_helper to call in_array() using exact matching, that is the third parameter as 'true' and started working as spected.

Do you see any problems with that change? The small changes are marked with /// HERE THE ,true ADDED

Here is the code of the function:

/**
 * Drop-down Menu
 *
 * @access  public
 * @param   string
 * @param   array
 * @param   string
 * @param   string
 * @return  string
 */
if ( ! function_exists('form_dropdown'))
{
    function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '')
    {
        if ( ! is_array($selected))
        {
            $selected = array($selected);
        }

        // If no selected state was submitted we will attempt to set it automatically
        if (count($selected) === 0)
        {
            // If the form name appears in the $_POST array we have a winner!
            if (isset($_POST[$name]))
            {
                $selected = array($_POST[$name]);
            }
        }

        i开发者_Go百科f ($extra != '') $extra = ' '.$extra;

        $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';

        $form = '<select name="'.$name.'"'.$extra.$multiple.">\n";

        foreach ($options as $key => $val)
        {
            $key = (string) $key;

            if (is_array($val))
            {
                $form .= '<optgroup label="'.$key.'">'."\n";

                foreach ($val as $optgroup_key => $optgroup_val)
                {
                    $sel = (in_array($optgroup_key, $selected, true)) ? ' selected="selected"' : ''; /// HERE THE ,true added

                    $form .= '<option value="'.$optgroup_key.'"'.$sel.'>'.(string) $optgroup_val."</option>\n";
                }

                $form .= '</optgroup>'."\n";
            }
            else
            {
                $sel = (in_array($key, $selected, true)) ? ' selected="selected"' : '';  /// HERE THE ,true ADDED

                $form .= '<option value="'.$key.'"'.$sel.'>'.(string) $val."</option>\n";
            }
        }

        $form .= '</select>';

        return $form;
    }
}


Try casting your values as strings like the examples below and see if that works.

$deptolist = array( (string)'0001' => 'First', (string)'01' => 'Second' );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜