textField to Radio button

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…

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')); ?>



thanks my friend, however the appearance of radio button is vertical… i modified the css and still it does not work…

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 :)

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>

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;

}



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."

you can change that in your model.


public function attributeLabels()

	{

		return array(

			'dob' => 'Date of Birth',

                        ...........

		);

	}

but it change your label too.

:rolleyes: 10 fingers up… thanks…

oh there still something missing that i’d notice, how can i set a default in radio?

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';



nice one…thanks