lithium icon indicating copy to clipboard operation
lithium copied to clipboard

Render select as multiple checkboxes

Open daschl opened this issue 13 years ago • 2 comments

During discussion on IRC with @alkemann, a possibility would be to render a select box (or similar), as a list of multiple checkboxes.

Here is a snipped by @alkemann that does basically the same but not tightly integrated into the form helper:

<?php 
/**
 * Render a special field that uses multiple checkboxes
 *
 * @param string $field
 * @param array $options
 * @return string
 */
public function multiple($field, $options) {
    $ret = '<div class="item checkbox"><label>';
    $ret .= isset($options['display']) ? $options['display'] : \lithium\util\Inflector::humanize($field);
    $ret .= '</label>';
    $fieldOptions = array(
        'wrap' => array('class' => 'sub-item'),
        'type' => 'checkbox',
        'hidden' => false
    );
    foreach ($options['options'] as $value => $label) {
        $optionOptions = array(
            'id' => $field.'-'.$value,
            'value' => $value,
            'label' => $label)
                + $fieldOptions;
        if (isset($options['default']) && in_array($value, $options['default'])) {
            $optionOptions['checked'] = true;
        }
        $ret .= parent::field($field.'[]', $optionOptions);
    }
    $ret .= '</div>';
    return $ret;
}
?>

What do you guys think about that? If we choose a specific implementation, I'll volunteer to implement it with tests.

Regards, daschl

daschl avatar Jul 13 '11 11:07 daschl

Hello @daschl and Team,

My name is Binh and I'm working on a project using Li3. Currently I've found no documentation regarding how to use the Form to display a list of checkboxes / radios. Therefore I made my own function to do so. It work great but I still prefer a official solution.

I wonder such feature is implemented yet?

Thanks,

binhwpo avatar Dec 25 '14 03:12 binhwpo

Would be a nice idea but would be better with better optimization, for example, there's no point making an empty label. Would maybe be a bit better if the variables are named a bit better aswell, calling $options['default'] $options['selected'] for example.

One last, the way this function is structured may want to be reworked slightly as the current code wont allow for a default value to be passed in. Just some suggestions, I would be happy to do this if someone wants?

DrRoach avatar Dec 25 '14 15:12 DrRoach