Yii Framework form submit button showing error

2019-08-26 18:51发布

问题:

I am getting an error
Fatal error: Call to a member function hasErrors() on a non-object in F:\xampp\yii\framework\web\helpers\CHtml.php on line 2253
when trying to access the url http://localhost/happybox/index.php/site/cart/3. The code of my view file cart.php is:

Hello, <?php echo ucwords(Yii::app()->user->name); ?>,<br/>
This is your cart<br/>
Click on Order to Place and Order.<br/>

<?php 
$Orders = Orders::model()->findAll(array(
    'select'=>'products_id',
    'condition'=>'users_id=:id && type=:type',
    'params'=>array(':id'=>Yii::app()->user->id, ':type'=>'cart'),
));

?>
<?php 
if($Orders){
    foreach ($Orders as $order) {
    echo '<br/>'.$order->products->title.'<br/>'.$order->products->description.'<br/>';

    }
}else{
    echo 'Your Cart is Empty';
}

?><br/><br/>

<?php 
if($Orders){
   $form=$this->beginWidget('CActiveForm', array('id'=>'order-form')); 
   echo $form->hiddenField($model,'users_id',array('value'=>Yii::app()->user->id)); 
   echo CHtml::submitButton('Confirm Your Order'); 
   $this->endWidget(); 
}
?>

The code of my controller function is

public function actionCart($id){
    if(Yii::app()->user->isGuest){
        $this->redirect('login');
    }else{
        $model=$this->loadModel($id);
        if(isset($_POST['Orders']))
        {        
        //do something
        }
        $this->render('cart', array('model'=>$model));
    }
}

Can someone please point out the error in this code, Its very very important for me. Please help.

回答1:

I've encountered the same bug, when I created a hiddenField inside a widget. This way it worked:

echo $form->hiddenField(
    **YourModelName::model()**,
    'users_id',
    array('value' => Yii::app()->user->id)
);


回答2:

$model = $model=$this->loadModel($id); Fix this line in your code and var_dump $model to see what it returned, check if it was null or something not CActiveRecord