radio button name attribute

I am using this radio button below.

http://www.yiiframework.com/doc-2.0/yii-helpers-basehtml.html#radioList()-detail

I create code as below and the radio list show up just fine.


echo $form->field($model, 'attribute1')

    ->radioList(    

    ArrayHelper::map(\app\models\MyList::find()->where(['groupid' => $groupid])->all(), 'id', 'itemname'),

    ['separator' => '<br>']);

But i do this for multiple instance of the same model in the same form. So the radio list being created all has same value for name attribute for example ‘mymodel-attribute1’ resulting in one group of radio list for all instance. So i want to change the radio list name attribute to make one radio list for each instance.

from the doc, radioList function parameter are as below


public static string radioList ( $name, $selection = null, $items = [], $options = [] )

But i get an error when doing any of these


echo $form->field($model, 'attribute1')

    ->radioList(

    'radioname',

    ArrayHelper::map(\app\models\MyList::find()->where(['groupid' => $groupid])->all(), 'id', 'itemname'),

    ['separator' => '<br>']);


echo $form->field($model, 'attribute1')

    ->radioList(

    'radioname',

    null,

    ArrayHelper::map(\app\models\MyList::find()->where(['groupid' => $groupid])->all(), 'id', 'itemname'),

    ['separator' => '<br>']);


echo $form->field($model, 'attribute1')

    ->radioList(

    'radioname',

    [],

    ArrayHelper::map(\app\models\MyList::find()->where(['groupid' => $groupid])->all(), 'id', 'itemname'),

    ['separator' => '<br>']);

so how to set the name attribute properly?

while searching the doc, i found bootstrap-activefield radiolist. Should i use this one instead?

You can also add function to your model like this


    public static function getMyList($groupid)

    {

        $options = MyList::find()->where(['groupid' => $groupid])->asArray()->all();

        return ArrayHelper::map($options, 'id', 'itemname');

    }

and in form use


<?= $form->field($model, 'attribute1')->radioList($model->getMyList($groupid), ['separator' => '<br>']); ?>

I have not tested, but you can play with it…

I think u might has misunderstood my question. Problem is not with retrieving data to create radio list.

Let me give more detail of the situation.

Let say i has Model1.

In my page i create an array of Model1. For example :


arrayModel1 = [new \app\models\Model1(), new \app\models\Model1(), new \app\models\Model1(), new \app\models\Model1()];

Now i want user to input value to Model1->property1 for each of Model1 instance.

User can select value from a list of radio button assign for each Model1 instance.

So then in my form i do something like


foreach(arrayModel1 as $model){

    echo $form->field($model, 'property1')

    ->radioList(    

    ArrayHelper::map(\app\models\MyList::find()->where(['groupid' => $model->id])->all(), 'id', 'itemname'),

    ['separator' => '<br>']);

}

From code above, i get 4 radio list somewhat like below. The full html code is too long, so i make a shorter version manually here to show 2 radio list.

radio list 1


<div id = "model1-property1">

<label>

<input type="radio" value="somevalue1" name="Model1[property1]"></input>

Somelabel2

</label>

<br><br>

<label>

<input type="radio" value="somevalue2" name="Model1[property1]"></input>

Somelabel2

</label>

<br><br>

<label>

<input type="radio" value="somevalue3" name="Model1[property1]"></input>

Somelabel3

</label>

<br><br>

<label>

<input type="radio" value="somevalue4" name="Model1[property1]"></input>

Somelabel4

</label>

</div

radio list 2


<div id = "model1-property1">

<label>

<input type="radio" value="somevalue5" name="Model1[property1]"></input>

Somelabel5

</label>

<br><br>

<label>

<input type="radio" value="somevalue6" name="Model1[property1]"></input>

Somelabel6

</label>

<br><br>

<label>

<input type="radio" value="somevalue7" name="Model1[property1]"></input>

Somelabel7

</label>

<br><br>

<label>

<input type="radio" value="somevalue8" name="Model1[property1]"></input>

Somelabel8

</label>

</div

So as u can see radio button in radio list 1 has same name as radio button in radio list 2 which is Model1[property1]

So selecting radio button in any radio list will deselect any selected radio button in other radio list as well.

So i want to change the name attribute using $name parameter/argument in radioList function, but encounter an error when i do so.


public static string radioList ( $name, $selection = null, $items = [], $options = [] )

I think i didnt pass value for $selection properly, resulting an error, so i want to know what value should i pass.

If my attempt to change the name attribute of radio button is wrong, or my implementation to get input for multiple instance of same model is wrong, please give some advice.

Thx

1 Like