Checkbox Variable In View

I have a problem with passing the checkbox variable to view and work with it. So basically what I’m trying to do is to use a checkbox field that is not from database, that means I can not fetch it from model. How to pass it to view? I need a checkbox that would tell me if I need to save the date to database or not.

That is how I call the variable in view, but it looks like I need to create it somewhere, but I’m lost, where.


<?php echo $form->checkBox($model, 'SaveDate');?>

You have to define virtual attribute in your model. I think You can also check out :

http://www.yiiframework.com/wiki/167/understanding-virtual-attributes-and-get-set-methods

OK, I managed to do that. This is how it looks now:

Controller:


class PosegController extends Controller

{

	public $ShraniDatum;



actionCreate:


	public function actionCreate()

	{

		$model=new Poseg;


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


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

		{

			$model->attributes=$_POST['Poseg'];

			if($model->save())

#				print_r($_POST);

#				exit();

				$ObvNRedni = new DateTime($_POST['NRedni']);

				$ObvNRedni->modify("-1 month");

				if(($_POST['ShraniDatum'])=='1')

				{

					OsnovnoSredstvo::model()->updateByPk($model->Inventarna,array('NRedni'=>$_POST['NRedni']));

					OsnovnoSredstvo::model()->updateByPk($model->Inventarna,array('ObvNRedni'=>$ObvNRedni->format("Y-m-d")));

				}

				$this->redirect(array('view','id'=>$model->idPoseg));

			

		}

		

		$this->render('create',array(

			'model'=>$model,

		));

	}

View:


	<div class="row">

		<?php echo $form->labelEx($model,'ShraniDatum'); ?>

		<?php echo $form->checkBox($model,'ShraniDatum');?>

	</div>

I can fetch ShraniDatum value if posting print_r($_POST); but when I it in the app, I get error: Undefined index: ShraniDatum. What am I missing?

Found out the sollution:

changed


if(($_POST['ShraniDatum'])=='1')

to


if($model->ShraniDatum==1)