phpgridviewyiigridsave

Yii doesn't save editable grid after submit button


unfortunately this solution doesn't save edited data on the grid, may i have some help?

Thanks in advance.

VIEW


<form name="editableGridForm" method="post" action="editableGrid"> <?php
            $this->widget('zii.widgets.grid.CGridView', 
                     array(
                     'id'=>'timesheet-grid',
                     'dataProvider'=>$model->search(),
                     'columns'=>array(
                            array('name'=>'ID','htmlOptions'=>array('width'=>'25px')),
                            array('name'=>"DESCRIZIONE",
                                  'type'=>'raw',
                                  'htmlOptions'=>array('width'=>'150px'),
                                  'value'=>'$data->getInputField(\'DESCRIZIONE\',$row)'
                                   ),
                            )
                       )
              );
              echo CHtml::submitButton('salva',array('class' => 'btn btn-success'));
              ?>
</form>

MODEL


public function getInputField($fieldName, $row, $options = array()) {
     return CActiveForm::textField($this, $fieldName, 
     array_merge(array("name" => "Timesheet[" . $row . "][" . $fieldName . "]"), $options)
     );
}

CONTROLLER


public function actionEditableGrid() {
    $model=new Timesheet('search');
    $notenData = $model->search();

    if (isset($_POST['Timesheet']) ) {
        foreach( $notenData->data as $i=>$item ) {
            if(isset($_POST['Timesheet'][$i])) {
               $item->attributes=$_POST['Timesheet'][$i];
                if ( $item->validate() ) {
                   $item->save();  
                }
            }
        }
        $this->redirect(array('admin'));
    }
}

Solution

  • When you do a mass assignment to $this->attributes, Yii will only change the "safe" attributes. Attributes are safe when they have a validation rule or when they are simply being marked as safe (if no validation rule is suitable), as follows:

    public function rules()
    {
        return array(
            array('DESCRIZIONE', 'required'),
            array('DESCRIZIONE', 'safe'),
        );
    }
    

    Either one of these rules is sufficient.

    More info about doing mass assignments and the "safe" validator: http://www.yiiframework.com/wiki/161/understanding-safe-validation-rules/

    I also noticed that you should replace CActiveForm::textField by CHtml::activeTextField in your model to prevent errors in php strict mode. textField is a non-static method of CActiveForm.

    Edit: in order to debug why the saving is not working, replace this in your controller

    if ( $item->validate() ) {
        $item->save();  
    }
    

    by

    if ( !$item->save() ) {
        var_dump('Could not save, errors: ' . var_export($item->errors, TRUE) . ', attributes: ' . var_export($item->attributes, TRUE));
        return;
    }
    

    save() will call validate() automatically and return false if the validation fails.