【问题标题】:Yii doesn't save editable grid after submit buttonYii 在提交按钮后不保存可编辑的网格
【发布时间】:2015-08-25 10:03:18
【问题描述】:

很遗憾,这个解决方案没有在网格上保存编辑过的数据,我可以帮忙吗?

  • 查看名称:admin
  • 型号名称:时间表
  • 控制器名称:TimesheetController

提前致谢。

查看


<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>

型号


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

控制器


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'));
    }
}

【问题讨论】:

    标签: gridview yii grid save edit


    【解决方案1】:

    当你对$this-&gt;attributes 进行批量赋值时,Yii 只会改变“安全”属性。当属性具有验证规则或仅被标记为安全时(如果没有合适的验证规则),属性是安全的,如下所示:

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

    这些规则中的任何一个都足够了。

    有关执行批量分配和“安全”验证器的更多信息:http://www.yiiframework.com/wiki/161/understanding-safe-validation-rules/

    我还注意到您应该在模型中将CActiveForm::textField 替换为CHtml::activeTextField 以防止php 严格模式下的错误。 textFieldCActiveForm 的非静态方法。

    编辑:为了调试保存不起作用的原因,请在您的控制器中替换它

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

    通过

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

    save() 会自动调用validate(),如果验证失败则返回false

    【讨论】:

    • 谢谢乔纳森,但它还没有保存。按照您的建议,我已经在 Model 上编写了必要且安全的规则,但仍然没有...
    • 我添加了一些信息来调试验证。我假设另一个字段导致验证失败。 ModelNameBehavior 不包含可能干扰验证/保存的方法?
    • 嗨乔纳森,我刚刚在控制器 search 操作上评论了这些行,现在网格让我保存数据:CONTROLLER //$model->unsetAttributes() ; // 清除所有默认值 //$model->RISORSA = Yii::app()->user->getState('profile')->cn;感谢您的宝贵支持
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多