Struggling with Checkbox

Hi,

I am struggling with the checkBoxList. I am able to save ok but once saved the checkboxes remain unchecked. Not sure where to start with this. Should I be passing this off to jquery. Also, if unchecked how would I go about running delete(). Here is my checkBoxList view and update method controller:




public function actionUpdate($id)

{

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


        $preSelectedItems = array();

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

        {

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

	if($model->save())

        {

        if(isset($_POST['Client']['serviceId']))

        {

	$message = "You selected: ";

        $loopCount = 0;

        $preSelectedItems = array();


	foreach($_POST['Client']['serviceId'] as $selectedItem) //If nothing checked this is throwing an error

	{

	$clientServices = new ClientServices();

        $clientServices->client_id = $model->id;

        $clientServices->service_id = $selectedItem;

        $clientServices->save();


        $message .= strval($selectedItem);

        if(++$loopCount < count($_POST['Client']['serviceId']))

        $message .=  " & ";

	$preSelectedItems[] = $selectedItem;

        }

	}

        

        else

        { 

        //should I delete here if !isset (unchecked) 

        }


        Yii::app()->user->setFlash('selectedValues',$message);

       

        }

        }

        

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

        'model'=>$model, 'select'=>$preSelectedItems,

	));

}






<?php

	echo $form->labelEx($model,'service');

	echo $form->checkBoxList($model, 'serviceId', CHtml::listData(ServiceType::model()->findAll(), 'id', 'name'),

        array_keys($model->serviceId), $select);

	echo $form->error($model,'service');       

?>



Would really appreciate some help/guidance…

Thanks,

Erik

This is not exactly the same situation but it might help you grok the text box thing.

Similar post

doodle

It might be useful to you: here’s a link to a full-working code example with checkboxlist; the view, the controller and the model.

Working with a checkbox list