[Solved] Problems With Checkbox Updating Form

hello eveyone, I have a form to create work schedules, I’ve created some checkboxes for each day (I’ve atached an image of the form).

this is the code from the form:


	<div class="checkbox">

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

		<label> <?php echo $form->checkBox($model,'Dia', array('value'=>'Lunes', 'name'=>'Dia[1]'));?> Lunes</label>

		<label> <?php echo $form->checkBox($model,'Dia', array('value'=>'Martes', 'name'=>'Dia[2]'));?> Martes </label>

		<label> <?php echo $form->checkBox($model,'Dia', array('value'=>'Miercoles', 'name'=>'Dia[3]'));?> Miércoles </label>

		<label> <?php echo $form->checkBox($model,'Dia', array('value'=>'Jueves', 'name'=>'Dia[4]'));?> Jueves </label>

		<label> <?php echo $form->checkBox($model,'Dia', array('value'=>'Viernes', 'name'=>'Dia[5]'));?> Viernes </label>

		<label> <?php echo $form->checkBox($model,'Dia', array('value'=>'Sábado', 'name'=>'Dia[6]'));?> Sábado </label>

		<label> <?php echo $form->checkBox($model,'Dia', array('value'=>'Domingo', 'name'=>'Dia[7]'));?> Domingo     	 </label>

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

	</div>

when saving I use ‘implode’ and it saves correctly in the database.

controller:


	public function actionCreate()

	{

		$model=new Horario;


		if(isset($_POST['Horario']) and isset($_POST['Dia']))

		{

			$Dias =implode(", ", $_POST['Dia']);                                          

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

			$model->Dia=$Dias;

			if($model->save())

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

		}

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

			'model'=>$model,

		));

	}

everything works fine but when I try to update or there is an error and the form is loaded again the selected checkboxes are not checked anymore and I haven’t find a way to fix that.

thanks and sorry for my English.

gracias.

In your action update try exploding them before viewing




public function actionUpdate()

{


 if($model->Dia!==array())

		$model->Dia = $Dias;

            	$Dias = explode(',', $model->Dia);


....your other info....

}



thanks skworden

this is the solution:

in controll action update


	public function actionUpdate($id)

	{

            


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

                

               $Dias = explode(',', $model->Dia); //Explode before viewing

               $model->Dia = $Dias; // asign to attribute


		if(isset($_POST['Horario']) and isset($_POST['Dia']))

		{

   

			$Dias =implode(",", $_POST['Dia']); // make a string of days to save

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

                    	$model->Dia=$Dias; //asign the string imploded to the attribute

			if($model->save())

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

		}

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

			'model'=>$model,

                        'Dias'=>$Dias  // ********** ADD THIS *************

		));

	}

then in your update.php file in the line:


<?php $this->renderPartial('_form', array('model'=>$model)); ?>

add this:


<?php $this->renderPartial('_form', array('model'=>$model,'Dias'=>$Dias)); ?>

then in the form we have to change some things so when you create a new model instead of updating one the form works correctly because for update and create the same form is used





	<div class="checkbox">

        <?php    if (isset($Dias)):?> // check if the variable $Dias comes with values

//we add this => 'checked'=>in_array('CheckBoxName',$Array)? 'checked':''

//to see if the name of the checkbox is inside the array $Dias


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

		<label> <?php echo $form->checkBox($model,'Dia', array('value'=>'Lunes', 'name'=>'Dia[1]', 'checked'=>in_array('Lunes',$Dias)? 'checked':''));?> Lunes</label>


		<label> <?php echo $form->checkBox($model,'Dia', array('value'=>'Martes', 'name'=>'Dia[2]','checked'=>in_array('Martes',$Dias)? 'checked':''));?> Martes </label>


		<label> <?php echo $form->checkBox($model,'Dia', array('value'=>'Miercoles', 'name'=>'Dia[3]','checked'=>in_array('Miercoles',$Dias)? 'checked':''));?> Miércoles </label>


		<label> <?php echo $form->checkBox($model,'Dia', array('value'=>'Jueves', 'name'=>'Dia[4]','checked'=>in_array('Jueves',$Dias)? 'checked':''));?> Jueves </label>


		<label> <?php echo $form->checkBox($model,'Dia', array('value'=>'Viernes', 'name'=>'Dia[5]','checked'=>in_array('Viernes',$Dias)? 'checked':''));?> Viernes </label>


		<label> <?php echo $form->checkBox($model,'Dia', array('value'=>'Sabado', 'name'=>'Dia[6]','checked'=>in_array('Sabado',$Dias)? 'checked':''));?> Sábado </label>


		<label> <?php echo $form->checkBox($model,'Dia', array('value'=>'Domingo', 'name'=>'Dia[7]','checked'=>in_array('Domingo',$Dias)? 'checked':''));?> Domingo     	 </label>

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

              

             <?php else: ?> // checkbox for a new model without any of the checkboxes checked


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

		<label> <?php echo $form->checkBox($model,'Dia', array('value'=>'Lunes', 'name'=>'Dia[1]'));?> Lunes</label>


		<label> <?php echo $form->checkBox($model,'Dia', array('value'=>'Martes', 'name'=>'Dia[2]'));?> Martes </label>


		<label> <?php echo $form->checkBox($model,'Dia', array('value'=>'Miercoles', 'name'=>'Dia[3]'));?> Miércoles </label>


		<label> <?php echo $form->checkBox($model,'Dia', array('value'=>'Jueves', 'name'=>'Dia[4]'));?> Jueves </label>


		<label> <?php echo $form->checkBox($model,'Dia', array('value'=>'Viernes', 'name'=>'Dia[5]'));?> Viernes </label>


		<label> <?php echo $form->checkBox($model,'Dia', array('value'=>'Sabado', 'name'=>'Dia[6]'));?> Sábado </label>


		<label> <?php echo $form->checkBox($model,'Dia', array('value'=>'Domingo', 'name'=>'Dia[7]'));?> Domingo     	 </label>


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

             <?php endif; ?>

	</div>




Glad you figured it out. Just remember if you have to do something to an item before you save it you most likely will have to do the opposite when you view it.

i am doing in yii2

in my update_form

<?= $form->field($model, ‘submenuItems’)->checkboxList($submenuItems,

        [


'item' =&gt; function(&#036;index, &#036;label, &#036;name, &#036;checked, &#036;value ){


    &#036;checked = in_array(&#036;value, [color=&quot;#FF0000&quot;]&#036;assignedsubmenuItems[/color])? 'checked' : '';


    &#036;checkbox = Html::checkbox(&#036;name,  ['value' =&gt; &#036;value, 'checked' =&gt; &#036;checked]);


    return Html::tag('div', Html::label(&#036;checkbox . &#036;label), ['class' =&gt; 'checkbox']);


},


       ] ) ?&gt;

here $assignedsubmenuItems is the array of selected submenuItems_id from assigned_submenu table that i have passed while rendering

and $submenuItems is the array of all submenuItems from submenu table

like

<?= $this->render(‘update_form’, [

    'model' =&gt; &#036;model,


    'submenuItems' =&gt; &#036;submenuItems,


   'assignedsubmenuItems'=&gt;&#036;assignedsubmenuItems,


]) ?&gt;

and i get

PHP Notice – yii\base\ErrorException

Undefined variable: assignedsubmenuItems

but if i do print_r($assignedsubmenuItems);

it prints the array.

so where i am doing wrong