How to remember value in CheckBox on update?

Hallo everyone!!

I’m looking for some help, I wonder how to remember checkbox value on update. I use an array to get up to three values from my checkboxs. I can save this data into database, I can retrieve the data from database, but on update action this checkboxes have default values, I mean are unchecked. Any ideas how to change my code to remember my checkboxes on update action.

This is my code for _form.php view


<div class="row">

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

		<?php echo 'Type1: ' . $form->checkBox($model,'payment[0]',array('checked'=>false,'value'=>'type1')) . ' '; ?>

		<?php echo 'Type2: ' . $form->checkBox($model,'payment[1]',array('checked'=>false,'value'=>'type2')) . ' '; ?>

		<?php echo 'Type3: ' . $form->checkBox($model,'payment[2]',array('checked'=>false,'value'=>'type3')); ?>

		<?php echo $form->error($model,'payment'); ?>

	</div>

in my model I have this:


	/**

	 * This is invoked before the record is saved.

	 * @return boolean whether the record should be saved.

	 */

	protected function beforeSave()

	{

		if(parent::beforeSave())

		{

			$this->payment = array_filter($this->payment);

			$this->payment = implode(', ', $this->payment);

			$this->shipping = array_filter($this->shipping);

			$this->shipping = implode(', ', $this->shipping);

		

			if($this->isNewRecord)

			{

				

				$this->create_time=$this->update_time=time();

				$this->create_user_id=Yii::app()->user->id;

			}

			else

				

				$this->update_time=time();

				$this->update_user_id=Yii::app()->user->id;

			return true;

		}

		else

			return false;

	}

All tips are welcome :)

Regards lukBB

are you passing $model when rendering view in actionUpdate() ?

also your views default attribute is to be disable : ‘checked’=>false, change it

hi bamdad dashtban thanks for reply, here is my code for action update in model class


/**

	 * Updates a particular model.

	 * If update is successful, the browser will be redirected to the 'view' page.

	 * @param integer $id the ID of the model to be updated

	 */

	public function actionUpdate($id)

	{

		$model=$this->loadModel($id);


		// Uncomment the following line if AJAX validation is needed

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


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

		{

			$folder=Yii::getPathOfAlias('webroot').'/images/ads/';// folder for uploaded files

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

			$model->image=CUploadedFile::getInstance($model,'image');

			if($model->save())

				{

					if(isset($model->image))

						{

							if(!is_dir($folder.$model->id)){

								mkdir($folder.$model->id);

							}

							$model->image->saveAs($folder . $model->id . '/' . $model->id .'_'. $model->image);

						}

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

				}

				

		}


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

			'model'=>$model,

		));

	}

I updated _form.php file to


	<div class="row">

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

		<?php echo 'Type1: ' . $form->checkBox($model,'payment[0]',array('checked'=>$model->payment[0],'value'=>'type1')) . ' '; ?>

		<?php echo 'Type2: ' . $form->checkBox($model,'payment[1]',array('checked'=>$model->payment[1],'value'=>'type2')) . ' '; ?>

		<?php echo 'Type3: ' . $form->checkBox($model,'payment[2]',array('checked'=>$model->payment[2],'value'=>'type3')); ?>

		<?php echo $form->error($model,'payment'); ?>

	</div>

But now all checkboxes are set to true on update even if only one was checked in create. I think I have to extract my array from database somehow. Have you any idea how to do it? thank you

I got this done, but this is just nasty workaround I don’t like, but for now it works. I added some code to _form.php:


$payment = explode(', ', $model->payment);

	

if($payment[0]=='type1')

	{

		$payment0 = true;

	}

if($payment[0]=='type2' || $payment[1]=='type2')

	{

		$payment1 = true;

	}

if($payment[0]=='type3' || $payment[1]=='type3' || $payment[2]=='type3' )

	{

		$payment2 = true;

	}


<div class="row">

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

		<?php echo 'Type1: ' . $form->checkBox($model,'payment[0]',array('checked'=>$payment0,'value'=>'type1')) . ' '; ?>

		<?php echo 'Type2: ' . $form->checkBox($model,'payment[1]',array('checked'=>$payment1,'value'=>'type2')) . ' '; ?>

		<?php echo 'Type3: ' . $form->checkBox($model,'payment[2]',array('checked'=>$payment2,'value'=>'type3')); ?>

		<?php echo $form->error($model,'payment'); ?>

	</div>

If you know better way, I mean Yii way :) of doing it please let me know

regards lukBB