ActiveForm Radio Buttons

Hi hi,

I’m quite new to Yii and need help with the ActiveForm and Radio Buttons.

Question 1: Do I need to use "radioList" for Radio Buttons, so far "radio" is working for me.

The thing is, every radio button is releated to a div. When I click a radio button another part of the form gets visible by JavaScript. Here my raw code structure (yes I know this is not valid HTML)













so I think, for me a radioList won’t work. Any ideas?

Question 2: Is there a way to check a radio button by default? All answers here won’t do the job for me.

Question 3: How to remove the auto generated hidden radio field or how to pass it a value?

Thanks for any help!

Q1: use radioList


Since activeField radiolist() is attached to $model, it is enough to set model attribute to desidered value.

For example:

$model->myattribute = 1;

echo $form->field($model, 'myattribute')->radioList( [0=>'zero', 1 => 'one', 2 => 'two'] );


Use options field, passing proper unselect value:

echo $form->field($model, 'myattribute')->radioList( [0=>'zero', 1 => 'one', 2 => 'two'], ['unselect' => null] );

Thank you Fabrizio for your fast answers.

Q1:Now something strange happens, why is there Delivery Methode. How to remove it? Yes css would do it, but that is not a clean way.


Bildschirmfoto 2018-03-08 um 14.44.20.png

Q2: Is there a trick to pass every radio in the radioList a separate id. That would make my work a lot easier. Thanks for any help!

Q1: Probably the label "Delivery Methode" is the label of attribute. So set label to false to hide the label.

echo $form->field($model, 'myattribute')->label(false)->radioList( [0=>'zero', 1 => 'one', 2 => 'two'] );

Q2: Check radiolist options:

Maybe you can use item callback to customize them.

Great, thank you so much Fabrizio. Your tips did the job perfect!

my bk experience

Did it work for you, I still facing some issue.

The solution also works for me perfectly. Thanks all