Yii Framework Forum: activeRadioButtonList compare validation (yes/no check) - Yii Framework Forum

Jump to content

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

activeRadioButtonList compare validation (yes/no check) Rate Topic: -----

#1 User is offline   Giovanni D. 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 237
  • Joined: 23-December 08
  • Location:London (UK)

Posted 20 August 2009 - 04:37 AM

Hello,
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 ;)


controller:
$model=new User;
$model->setScenario('register');
...



view:
<div class="frm_center">
<?php	$test =array("yes"=>"Yes", "no"=>"No"); ?>
<?php echo CHtml::activeRadioButtonList($model,'do_agree', $test, array("separator"=>"&nbsp;")); ?>
</div>



model:

	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(
			$base,
			'register'=>array_merge($base, array('do_agree')),
		);
	}

- Yii Playground: collaborative demo app with small examples to play with.. join us ;) [Fork on github]

- DeploYii: Yii powered task runner and deployment solution

- Social: LinkedIn · Twitter
0

#2 User is offline   pestaa 

  • past Yii dev member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 705
  • Joined: 07-May 09
  • Location:Hungary

Posted 20 August 2009 - 08:30 AM

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

#3 User is offline   Giovanni D. 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 237
  • Joined: 23-December 08
  • Location:London (UK)

Posted 20 August 2009 - 09:36 AM

View Postpestaa, on 20 August 2009 - 08:30 AM, said:

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

bye,
Giovanni.
- Yii Playground: collaborative demo app with small examples to play with.. join us ;) [Fork on github]

- DeploYii: Yii powered task runner and deployment solution

- Social: LinkedIn · Twitter
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