gavinajayar
(gavinajayar@yahoo.com)
January 19, 2012, 6:44am
1
Hi everyone
How can i change this one to radio button
<div class="row">
<?php echo $form->labelEx($model,'Gender'); ?>
<?php echo $form->textField($model,'gender'); ?>
<?php echo $form->error($model,'gender'); ?>
</div>
something like this:
Gender
Male Female
thanks…
haykelbj
(Haykelbj)
January 19, 2012, 7:46am
2
Replace the text field with a radio button list. If the ‘gender’ model attribute takes the values ‘m’ and ‘f’ for example, it looks like this:
<?php echo $form->radioButtonList($model, 'gender', array('f' => 'Female', 'm' => 'Male')); ?>
gavinajayar
(gavinajayar@yahoo.com)
January 19, 2012, 8:06am
3
thanks my friend, however the appearance of radio button is vertical… i modified the css and still it does not work…
haykelbj
(Haykelbj)
January 19, 2012, 8:46am
4
Set the separator to a space (or whatever you like):
<?php echo $form->radioButtonList($model, 'gender', array('f' => 'Female', 'm' => 'Male'), array('separator' => ' ')); ?>
API documentation is your friend
Angela88
(Angela Arrie)
January 19, 2012, 8:56am
5
i don’t know better way but i do this
in main.css
div.form .line label
{
display: inline;
}
<div class="row line">
<?php echo $form->labelEx($model,'Gender'); ?>
<?php echo $form->textField($model,'gender'); ?>
<?php echo $form->error($model,'gender'); ?>
</div>
if you don’t want to change main.css, you can use row rememberMe like checkbox in login page.
<div class="row rememberMe">
<?php echo $form->labelEx($model,'Gender'); ?>
<?php echo $form->textField($model,'gender'); ?>
<?php echo $form->error($model,'gender'); ?>
</div>
haykelbj
(Haykelbj)
January 19, 2012, 9:02am
6
You’re right Angela, we also need to make labels display inline. Here is how to do it for radio buttons and checkboxes only if needed:
input[type="radio"] + label, input[type="checkbox"] + label {
display: inline !important;
}
gavinajayar
(gavinajayar@yahoo.com)
January 19, 2012, 9:37am
7
wow this is great, thanks a lot… how about if i like to change the error caption of this
<?php echo $form->error($model,‘dob’); ?>
into "Date of Birth"
because it prompts
"Dob cannot be blank."
Angela88
(Angela Arrie)
January 19, 2012, 9:50am
8
you can change that in your model.
public function attributeLabels()
{
return array(
'dob' => 'Date of Birth',
...........
);
}
but it change your label too.
gavinajayar
(gavinajayar@yahoo.com)
January 19, 2012, 10:14am
10
oh there still something missing that i’d notice, how can i set a default in radio?
haykelbj
(Haykelbj)
January 19, 2012, 10:24am
11
The current value will be taken from the model attribute, so if it is not set, e.g. for a new model, set it to the default value. You can do it either after creating the model, or by adding a public property for the attribute and set its default value. For example if your model class is called ‘User’:
$model = new User();
$model->gender = 'f';
or (the better solution), in ‘User.php’:
public $gender = 'f';