how to set scenario when beforeValidate

i want to know, how can i set scenario beforevalidate

i have attribute like this




public $captcha;



when atrribute captcha is ‘1’, i want to set scenario to ‘usecaptcha’




public function beforeValidate()

	{

		if($this->captcha=='1')

			$this->scenario = 'usecaptcha';

		return parent::beforeValidate();


	}



but i can’t get the value of captcha atribute in beforevalidate.

i want to know, how i can get value of attribute in beforeValidate?

thanks.

Maybe you didn’t declare ‘captcha’ to be safe (a.k.a. defined a rule for it)? Other than that there’s no reason why you should not be able to access this attribute in beforeValidate.

i used this code in controller action




if(isset($_POST['SignupForm']['captcha']))

$model->scenario = 'usecaptcha';



little bit dirty, but it works.

because if captcha exist, will triggered scenario to ‘usecaptcha’

perhaps you have any idea to put it in beforeValidation.

it would be more clean mike.

Basic rule in coding: Anything that works is right - at least partially. :)

I guess you have a chicken-egg problem here: Validation rules (and in last consequence ultimately: scenarios) define, which attributes are safe to be massively assigned. On the other hand you want to decide in beforeValidate, which scenario to use, based on the assigned value for ‘captcha’.

So checking ‘captcha’ manually in POST should be fine here.

Thank you for this really saved me

$model->setScenario(‘name_scenario’);