activeRadioButtonList compare validation (yes/no check)


I’m writing a registration form and I’ve just added a couple of radio buttons to let the user decide to agree or not with the privacy policy that will be added later.

Thought it could be useful for others so here is the code ;)


$model=new User;




<div class="frm_center">

<?php	$test =array("yes"=>"Yes", "no"=>"No"); ?>

<?php echo CHtml::activeRadioButtonList($model,'do_agree', $test, array("separator"=>"&nbsp;")); ?>



	public function rules()


		return array(


			// ... (other rules) ...

			array('do_agree', 'required', 'on'=>'register'),

			array('do_agree', 'compare', 'compareValue'=>'yes',

				'message'=>'You must agree!', 'on'=>'register'),



	public function safeAttributes() {

		$base =parent::safeAttributes();

		$base[]="conf_pass"; // (you may have to change or comment this)

		return array(


			'register'=>array_merge($base, array('do_agree')),



Thanks for sharing! :)

I’m curious though, who would ever click no and submit the form? In the light of this, I prefer checkbox instead.

well… this way suits better the italian law, moreover you could have two privacy information and maybe on the second one you can also answer no without preventing you from register… so the example itself doesn’t makes too much sense but could be used in different ways (I needed it in this way but wanted to use it to share this small piece of code as I think that the activeRadioButtonList is a handy helper and it is nice to see that you can also validate it ;))