TbHtml::radioButtonList PHP Method

radioButtonList() public static method

Generates a radio button list.
public static radioButtonList ( string $name, mixed $select, array $data, array $htmlOptions = [] ) : string
$name string name of the radio button list.
$select mixed selection of the radio buttons.
$data array $data value-label pairs used to generate the radio button list.
$htmlOptions array additional HTML attributes.
return string the generated list.
    public static function radioButtonList($name, $select, $data, $htmlOptions = array())
    {
        $inline = TbArray::popValue('inline', $htmlOptions, false);
        $separator = TbArray::popValue('separator', $htmlOptions, ' ');
        $container = TbArray::popValue('container', $htmlOptions, 'div');
        $containerOptions = TbArray::popValue('containerOptions', $htmlOptions, array());
        $labelOptions = TbArray::popValue('labelOptions', $htmlOptions, array());
        $empty = TbArray::popValue('empty', $htmlOptions);
        if (isset($empty)) {
            $empty = !is_array($empty) ? array('' => $empty) : $empty;
            $data = TbArray::merge($empty, $data);
        }
        $items = array();
        $baseID = $containerOptions['id'] = TbArray::popValue('baseID', $htmlOptions, parent::getIdByName($name));
        $id = 0;
        foreach ($data as $value => $label) {
            $checked = !strcmp($value, $select);
            $htmlOptions['value'] = $value;
            $htmlOptions['id'] = $baseID . '_' . $id++;
            if ($inline) {
                $htmlOptions['label'] = $label;
                self::addCssClass('radio-inline', $labelOptions);
                $htmlOptions['labelOptions'] = $labelOptions;
                $items[] = self::radioButton($name, $checked, $htmlOptions);
            } else {
                $option = self::radioButton($name, $checked, $htmlOptions);
                $items[] = self::tag('div', array('class' => 'radio'), self::label($option . ' ' . $label, false, $labelOptions));
            }
        }
        $inputs = implode($separator, $items);
        return !empty($container) ? self::tag($container, $containerOptions, $inputs) : $inputs;
    }

Usage Example

Exemplo n.º 1
0
 public function testRadioButtonList()
 {
     $I = $this->codeGuy;
     $html = TbHtml::radioButtonList('radioList', null, array('Option 1', 'Option 2', 'Option 3'), array('separator' => '<br>', 'container' => 'div', 'containerOptions' => array('class' => 'container')));
     $container = $I->createNode($html, 'div.container');
     $I->seeNodeChildren($container, array('label.radio', 'br', 'label.radio', 'br', 'label.radio'));
     $label = $container->filter('label')->first();
     $I->seeNodePattern($label, '/> Option 1$/');
     $input = $label->filter('input[type=radio]');
     $I->seeNodeAttributes($input, array('id' => 'radioList_0', 'name' => 'radioList', 'value' => '0'));
 }
All Usage Examples Of TbHtml::radioButtonList
TbHtml