V
V
VitaliyKaliuzhyn2017-02-06 15:57:26
Yii
VitaliyKaliuzhyn, 2017-02-06 15:57:26

radioList yii2 override?

there is a form

<?=$form->field($model,'created_at')->radioList(['Radio 1', 'Radio 2', 'Radio 3'],['class'=>'mt-radio-list', 'itemOptions'=>['labelOptions'=>['class'=>'mt-radio mt-radio-list-outline']], 'item'=>function($label, $name){return "<label class='mt-radio mt-radio-outline'>$name<input type='radio' value='1' name='optionsRadios'> <span></span></label>";}]); ?>

but it's too bulky. There is a radioList() method that I have overridden
public function radioList($items, $options = [])
{
    $this->adjustLabelFor($options);
    $this->errorOptions = array_merge(['tag' => 'span'], $this->errorOptions);
    $this->parts['{input}'] = Html::activeRadioList($this->model, $this->attribute, $items, $options);
    return $this;
}

how can I define 'item' not in the form itself, but in the radioList() method

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
VitaliyKaliuzhyn, 2017-02-06
@VitaliyKaliuzhyn

I have my ActiveField in which I work, but the question remains open, how can I define 'item'=>function() in the method, so that when passing parameters like myRadioLis(['1','Radio']) my template is displayed ?
That is, I need to have such an output for each parameter
Here I found a solution, who will be interested
instead
of doing

return Html::beginTag('label') .
                Html::Tag('input','',$options).
                Html::beginTag('span') .
                Html::endTag('span') .
                Html::endTag('label');

M
Maxim Timofeev, 2017-02-06
@webinar

There are cases when it's easier to just write html than to use ActiveField
But I don't understand why the bulkiness confuses you? Read the docs, etc. There is no limit on the length of a line of code anywhere.
But you can write your own ActiveField and create the myRadioList method there and put everything there. There will only be:
And you can make a widget, it will be:

<?=$form->field($model,'created_at')->myRadioWidget()?>

M
Maxim Fedorov, 2017-02-06
@qonand

If I understood the question correctly, in the simplest implementation something like this:

public function radioList($items, $options = [])
{
    if (key_exists('item', $options) === false) {
        $options['item'] = function($label, $name){
            return "<label class='mt-radio mt-radio-outline'>$name<input type='radio' value='1' name='optionsRadios'> <span></span></label>"
        }
    }
    return parent::__radioList($items, $options);

}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question