check box

Hello,

In my view file i define static check box, my question is, when I put condition in controller, it’s not respond.

view file

<?php echo CHtml::activeLabel($form,‘test’); ?>

<?php echo CHtml::checkBox($form->test); ?>

I need this condition in my controller

if ($form->test){ //if checkbox checked then

$this->redirect(array(‘test1’));

} else {

$this->redirect(array(‘index’));

}

but check box value is not working .

test is defined in the model?




class ExampleForm extends CFormModel

{

    public $test;

...



(in 1.1 declare safe the field)




public function rules()

    {

        return array(

            array('test', 'safe'),



try





    $form=new ExampleForm;

    if(isset($_POST['ExampleForm'])) {

    $form->attributes=$_POST['ExampleForm'];

    var_dump($form);

    }



mmm

change




<?php echo CHtml::checkBox($form->test); ?>



for




<?php echo CHtml::activeCheckBoxx($form,'test'); ?>



Yes, I already declare in the model. but condition is not working.

this is my controller

if ($form->test){ //if checkbox checked then

$this->redirect(array(‘test1’));

} else {

$this->redirect(array(‘index’));

}

Hello,

I have a static checkbox like this

<div class="simple">

<?php echo CHtml::activeLabel($form,‘test’); ?>

<?php echo CHtml::activeCheckBox($form,‘test’); ?>

</div>

and in my CActiveRecord form , I declare public $test;

and in controller

public function actionIndex()

{


	&#036;form=new Test;


	if(isset(&#036;_POST['Test']))


	{


		&#036;form-&gt;attributes=&#036;_POST['Test'];


		


		if(&#036;form-&gt;validate())


		{


			if(isset(&#036;_POST[&#036;form-&gt;test]) &amp;&amp; 


				&#036;_POST[&#036;form-&gt;test] == 'Yes') 


			{


    				&#036;this-&gt;redirect(array('index'));





			}


			else


			{


			   &#036;this-&gt;redirect(array('test1'));





			}	





			


						


		}


	}





	&#036;this-&gt;render('index',array('form'=&gt;&#036;form));


}

This is not working. Have any idea.

try




public function actionIndex()

{

 $form=new Test;


 if(isset($_POST['Test']))

 {

   $form->attributes=$_POST['Test'];


   if($form->validate()) 

      if($form->test==1)$this->redirect(array('index'));

      elseif ($form->test==0) $this->redirect(array('test1'));

      else throw new CHttpException(400,'test is not set');

 }

}



it’s not redirecting to test1 page if checkbox value is 0. :( .

public function actionIndex()

{


	&#036;form=new Test;


	 if(isset(&#036;_POST['Test']))


	{


	   &#036;form-&gt;attributes=&#036;_POST['Test'];


		   if(&#036;form-&gt;validate()) 


			{


		      		if(&#036;form-&gt;test==1) &#036;this-&gt;redirect(array('index'));


				elseif (&#036;form-&gt;test==0) &#036;this-&gt;redirect(array('test1'));


				else throw new CHttpException(400,'test is not set');


				//var_dump(&#036;form);


				


			}


	}


&#036;this-&gt;render('index',array('form'=&gt;&#036;form));


}

For massive assignment of attributes to work, in 1.0 you have to declare ‘test’ in safeAttributes(). As already mentioned, in 1.1 you declare it in rules(). If you don’t, ‘test’ will not be massively assigned. Of course, for a single attribute, you can call setAttribute(attribute, value) or just assign to the attribute name directly.

If ‘test’ doesnt get assigned nothing gets validated. I think what you see is the same page being re-rendered (no redirection whatsoever).

/Tommy

Hi Tommy,

Can you send me an example, that will help me to understand where is my problem.