致命错误:调用成员函数isAttributeRequired()

时间:2013-09-20 10:02:57

标签: php yii

我有以下表格

<?php $form = $this->beginWidget('CActiveForm', array(
    'id'=>'change-password',
    'enableAjaxValidation'=>true,
    'enableClientValidation'=>true,
    'focus'=>array($model,'old-password')
    ));
?>
<div class="row">
    <?php echo $form->labelEx($model,'oldPassword'); ?>
    <?php echo $form->passwordField($model,'oldPassword'); ?>
    <?php echo $form->error($model,'oldPassword'); ?>
</div>
<div class="row">
<?php echo CHtml::submitButton('Submit');
?>
</div>
<?php $this->endWidget(); ?>

上面的表单会抛出错误

Fatal error: Call to a member function isAttributeRequired() on a non-object in /framework/web/helpers/CHtml.php on line 1414

我是你的新框架,所以我无法猜出错误在哪里。任何人都可以帮忙

修改1

public static function activeLabelEx($model,$attribute,$htmlOptions=array())
    {
        $realAttribute=$attribute;
        self::resolveName($model,$attribute); // strip off square brackets if any
        $htmlOptions['required']=$model->isAttributeRequired($attribute);
        return self::activeLabel($model,$realAttribute,$htmlOptions);
    }

修改-2

如果删除<?php echo $form->labelEx($model,'oldPassword'); ?>错误

Fatal error: Call to a member function getValidators() on a non-object in CHtml.php on line 2236

2 个答案:

答案 0 :(得分:0)

$ model变量不包含Model对象,检查是否在render函数中传递了正确的对象。

ex for the create page:

/* in the controller class */
public function actionCreate() {
    $model=new YourModel;
    $this->performAjaxValidation($model);

    if(isset($_POST['YourModel']))
    {
        $model->attributes=$_POST['YourModel'];
        if($model->save())
            $this->redirect(array('view','id'=>$model->id));
    }

    $this->render('create',array(
        'model'=>$model,
    ));
}
/* in the create.php view */
....
echo $this->renderPartial('_form', array('model'=>$model));
....

答案 1 :(得分:0)

在渲染到视图时在控制器中 检查模型的定义  $model=new Model name;并检查您的模型是否具有相同的属性(变量)

另一个错误

渲染到视图时,在控制器中

该函数可能需要某些参数,而您还没有给出它

例如下一个函数中的$id

public function actionUpdateTeams($id) {

}