vendredi 19 février 2016

How to checkboxlist checked in Yii 1.6?

I am beginner in Yii Framework.I have created the _form.php with Yii widget CActiveForm.I have made the Checkbox list for week of days.I have serialized checkbox values on actioncreate and save into database table.Actually problem is that When i calling the actionupdate then my checked values are not showing that i have insert on actioncreate.actioncreate and actionupdate using the same form _form.php.

I have written my code below

  1. actioncreate

    public function actionCreate() { $model=new CustomerAccounts;

    if(isset($_POST['CustomerAccounts']))
    {
        $model->attributes=$_POST['CustomerAccounts'];
        $model->DateCreated = date('Y-m-d H:i:s');
                    $model->DeliveryDay = serialize($_POST['CustomerAccounts']['DeliveryDay']);
    
        if($model->save()) {
    
                         Yii::app()->user->setFlash('success', 'Customer Account  create successfully.');
                         $this->redirect(array('admin'));
                    } else {
                        Yii::app()->user->setFlash('danger','An error occurred. Please try again.');
                    }
    }
    
    $this->render('create',array(
        'model'=>$model,
    ));
    
    

    }

  2. actionupdate

    public function actionUpdate($id) { $model=$this->loadModel($id);

    if(isset($_POST['CustomerAccounts']))
    {
        $model->attributes=$_POST['CustomerAccounts'];
                    $model->DeliveryDay = serialize($_POST['CustomerAccounts']['DeliveryDay']);
        $model->DateCreated = date('Y-m-d H:i:s');
    
        if($model->save()) {
    
                         Yii::app()->user->setFlash('success', 'Customer  Account update successfully.');
                         $this->redirect(array('admin'));
                    } else {
                        Yii::app()->user->setFlash('danger','An error occurred. Please try again.');
                    }
    }
    
    $this->render('update',array(
        'model'=>$model,
    ));
    
    

    }

  3. _form.php

<?php $form=$this->beginWidget('CActiveForm', array(
        'id'=>'customer-accounts-form',
        'enableAjaxValidation'=>false,
<div class="row">
        <div class="col-lg-12" style="padding-left: 34px;"> 
        <?php echo $form->labelEx($model, 'DeliveryDay'); ?>
        <?php echo $form->error($model, 'DeliveryDay'); ?> 
        <div id="checkbox" style="padding-left: 90px;">
            <?php
            $htmlOptions = array('template' => '<tr><td >{input}</td>&nbsp;&nbsp;<td> {label}</td>&nbsp;&nbsp;</tr', 'multiple' => true, 'checked' => 'checked');
            echo $form->checkBoxList($model, 'DeliveryDay', Yii::app()->params['WeekDays'], $htmlOptions);
            ?>
        </div></div></div>

<?php $this->endWidget(); ?>

Would anyone Can tell me when i call the actionupdate How the checked values will be shows from database table ?




Aucun commentaire:

Enregistrer un commentaire