Yii Framework Forum: ActiveForm Radio Buttons - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

ActiveForm Radio Buttons

#1 User is offline   JanF. 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 08-March 18

Posted 08 March 2018 - 05:26 AM

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)
<form>
<input>
<input>
<input>
<hr>
<radio>
<div><input><input><input></div>
<hr>
<radio>
<div><input><input><input></div>
<hr>
</form>


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!
0

#2 User is offline   Fabrizio Caldarelli 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,073
  • Joined: 21-March 13
  • Location:Rome, Italy

Posted 08 March 2018 - 06:12 AM

Q1: use radioList
http://www.yiiframew...html#radioList()-detail

Q2:
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'] );


Q3:
Use options field, passing proper unselect value:
http://www.yiiframew...ctiveRadioList()-detail

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

Yii2 by example: my book about Yii2
https://www.packtpub...nt/yii2-example
0

#3 User is offline   JanF. 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 08-March 18

Posted 08 March 2018 - 08:52 AM

 Fabrizio Caldarelli, on 08 March 2018 - 06:12 AM, said:

Q1: use radioList
http://www.yiiframew...html#radioList()-detail

Q2:
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'] );


Q3:
Use options field, passing proper unselect value:
http://www.yiiframew...ctiveRadioList()-detail

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.
Attached File  Bildschirmfoto 2018-03-08 um 14.44.20.png (10.08K)
Number of downloads: 2

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!
0

#4 User is offline   Fabrizio Caldarelli 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,073
  • Joined: 21-March 13
  • Location:Rome, Italy

Posted 08 March 2018 - 08:57 AM

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: http://www.yiiframew...ctiveRadioList()-detail
Maybe you can use item callback to customize them.
Yii2 by example: my book about Yii2
https://www.packtpub...nt/yii2-example
0

#5 User is offline   JanF. 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 08-March 18

Posted 09 March 2018 - 03:20 AM

 Fabrizio Caldarelli, on 08 March 2018 - 08:57 AM, said:

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: http://www.yiiframew...ctiveRadioList()-detail
Maybe you can use item callback to customize them.



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

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users