【问题标题】:CActiveForm and its behaviors do not have a method or closure named "getErrors".CActiveForm 及其行为没有名为“getErrors”的方法或闭包。
【发布时间】:2014-11-20 01:42:33
【问题描述】:

您好,我是 yii 框架的新手,目前正在尝试通过数据库身份验证建立登录。但我反复收到此错误

C异常

CActiveForm 及其行为没有名为的方法或闭包 “getErrors”。

如果这样的话,谁能帮帮我

这是控制器

<?php

class SiteController extends Controller

{

public function actions()
{
    return array(

        'captcha'=>array(
            'class'=>'CCaptchaAction',
            'backColor'=>0xFFFFFF,
        ),

        'page'=>array(
            'class'=>'CViewAction',
        ),
    );
}
public function actionIndex()
{

    $this->render('index');
}


public function actionError()
{
    if($error=Yii::app()->errorHandler->error)
    {
        if(Yii::app()->request->isAjaxRequest)
            echo $error['message'];
        else
            $this->render('error', $error);
    }
}


public function actionContact()
{
    $model=new ContactForm;
    if(isset($_POST['ContactForm']))
    {
        $model->attributes=$_POST['ContactForm'];
        if($model->validate())
        {
            $name='=?UTF-8?B?'.base64_encode($model->name).'?=';
            $subject='=?UTF-8?B?'.base64_encode($model->subject).'?=';
            $headers="From: $name <{$model->email}>\r\n".
                "Reply-To: {$model->email}\r\n".
                "MIME-Version: 1.0\r\n".
                "Content-Type: text/plain; charset=UTF-8";

            mail(Yii::app()->params['adminEmail'],$subject,$model->body,$headers);
            Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');
            $this->refresh();
        }
    }
    $this->render('contact',array('model'=>$model));
}


public function actionLogin()
{
        $form=new LoginForm;
        if(isset($_POST['LoginForm']))
        {
            $form->attributes=$_POST['LoginForm'];
            if($form->validate()  && $form->login()) $this->redirect(Yii::app()->user->returnUrl);
        }

            $this->render('login',array('form'=>$form));
}

public function actionLogout()
{
    Yii::app()->user->logout();
    $this->redirect(Yii::app()->homeUrl);
}

}

继承模型

<?php

class LoginForm extends CFormModel

{
    public $email;
    public $password;


    private $_identity;

public function rules()
    {
        return array(
        array('email, password', 'required'),
        array('email', 'email'),
        array('password', 'authenticate'),
);
    }
public function attributeLabels()
{
            return array('email'=>'Email Address');
}
public function authenticate($attribute,$params)
{
            if(!$this->hasErrors())  // we only want to authenticate when no input errors
                {
                $identity=new UserIdentity($this->email,$this->password);
                $identity->authenticate();
                switch($identity->errorCode)
                {
                    case UserIdentity::ERROR_NONE:
                Yii::app()->user->login($identity);
                break;
                    case UserIdentity::ERROR_USERNAME_INVALID:
                $this->addError('email','Email address is incorrect.');
                break;
        default: // UserIdentity::ERROR_PASSWORD_INVALID
                $this->addError('password','Password is incorrect.');
                break;
                }
            }
    }
public function login()
{
    if($this->_identity===null)
    {
        $this->_identity=new UserIdentity($this->username,$this->password);
        $this->_identity->authenticate();
    }
    if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
    {
        $duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
        Yii::app()->user->login($this->_identity,$duration);
        return true;
    }
    else
        return false;
}

}

这里是风景

<?php
/* @var $this SiteController */
/* @var $model LoginForm */
/* @var $form CActiveForm  */

$this->pageTitle=Yii::app()->name . ' - Login';
$this->breadcrumbs=array(
    'Login',
);
?>
<h1>Login</h1>

<p>Please fill out the following form with your login credentials:</p>

<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'login-form',
    'enableClientValidation'=>true,
    'clientOptions'=>array(
        'validateOnSubmit'=>true,
    ),
)); ?>

    <p class="note">Fields with <span class="required">*</span> are required.</p>
<div>
    <?php echo CHtml::beginForm(); ?>

    <?php echo CHtml::errorSummary($form); ?>

    <div>
    <?php echo CHtml::activeLabel($form,'email'); ?>
    <?php echo CHtml::activeTextField($form,'email') ?>
    </div>

    <div>
    <?php echo CHtml::activeLabel($form,'password'); ?>
    <?php echo CHtml::activePasswordField($form,'password') ?>
    </div>

    <div>
    <?php echo CHtml::submitButton('Login'); ?>
    </div>

    <?php echo CHtml::endForm(); ?>

endWidget(); ?>

【问题讨论】:

    标签: php database yii login


    【解决方案1】:

    如果您包含更多错误会很好 - 例如它发生在哪一行和哪个文件中,或者充其量是整个跟踪.. 从我在您的代码中读到的内容,您似乎覆盖了 $form 变量,该变量实际上保存了您的模型。 在SiteController 中,您使用LoginForm 模型初始化变量$form。 然后在视图中你做了这个错误的调用:

        <?php $form=$this->beginWidget('CActiveForm', array(
            'id'=>'login-form',
            'enableClientValidation'=>true,
            'clientOptions'=>array(
                'validateOnSubmit'=>true,
            ),
        )); 
        ?>
    

    因此,在所有 CHtml 调用中,您将一个小部件而不是模型变量放到函数中

    由于您不以特定方式使用小部件的输出,因此删除 $form = $this-&gt;widget... 并仅将其保留为 $this-&gt;widget.. 或将变量替换为新变量就足够了。

      <?php $myWidget = $this->beginWidget('CActiveForm', array(
                'id'=>'login-form',
                'enableClientValidation'=>true,
                'clientOptions'=>array(
                    'validateOnSubmit'=>true,
                ),
            )); 
      ?>
    

    【讨论】:

    • Yii\base\CComponent.php 是给出错误的脚本......这是引发错误的行 throw new CException(Yii::t('yii','{class } 及其行为没有名为“{name}”的方法或闭包。',
    • 谢谢。但与此同时,我 99% 确定问题与我描述的一样。你用小部件覆盖你的模型变量,当然没有检查错误的方法。请尝试我的解决方案
    猜你喜欢
    • 2015-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-28
    • 1970-01-01
    相关资源
    最近更新 更多