2

我有ChangeEmailForm扩展CFormModel

<?php

/**
 * LoginForm class.
 * LoginForm is the data structure for keeping
 * user login form data. It is used by the 'login' action of 'SiteController'.
 */
class ChangeEmailForm extends CFormModel
{
    public $newemail;
    public $password;

    /**
     * Declares the validation rules.
     * The rules state that username and password are required,
     * and password needs to be authenticated.
     */
    public function rules()
    {
        return array(
            array('newemail, password', 'required'),
            array('newemail', 'email'),
            array('newemail', 'unique', 'attributeName' => 'User.email'),
            array('password', 'authenticate')
        );
    }

    /**
     * Declares attribute labels.
     */
    public function attributeLabels()
    {
        return array(
            'newemail' => 'Новый Email-адрес',
            'password' => 'Пароль учетной записи'
        );
    }

    /**
     * Authenticates the password.
     * This is the 'authenticate' validator as declared in rules().
     */
    public function authenticate($attribute, $params)
    {
        if (!$this->hasErrors()) {
            $user = User::model()->findByAttributes(array(
                'password' => $this->password,
                'id' => Yii::app()->user->id
            ));
            if ($user === null)
                $this->addError('password', 'Неверный пароль учетной записи.');
        }
    }
}

我也有以下行动ProfileController

public function actionSettings()
{
    $profile = $this->loadModel(Yii::app()->user->id);
    $model = new ChangeEmailForm;

    // if it is ajax validation request
    if(isset($_POST['ajax']) && $_POST['ajax']==='change-email-form')
    {
      echo CActiveForm::validate($model);
      Yii::app()->end();
    }

    // collect user input data
    if(isset($_POST['ChangeEmailForm']))
    {
      $model->attributes=$_POST['ChangeEmailForm'];
                        // validate user input and redirect to the previous page if valid
                        if($model->validate()) {
        $this->redirect('/');
      }

    }

    $this->render('settings',array(
      'model'=>$model,
      'profile'=>$profile,
    ));
}

并查看:

<?php
/* @var $this ProfileController */
/* @var $model ChangeEmailForm */
/* @var $profile User */
/* @var $form CActiveForm */

$this->breadcrumbs=array(
    'Учетная запись',
);
?>

<h1>Настройки учетной записи</h1>

<h2>Смена email-адреса</h2>
<div class="form-register">

    <?php $form=$this->beginWidget('CActiveForm', array(
        'id'=>'change-email-form',
        'enableAjaxValidation'=>true,
    )); ?>

    <div class="row">
        <div class="col col-label"><?php echo $form->labelEx($model,'newemail', array('class'=>'label1')); ?></div>
        <div class="col col-input"><?php echo $form->textField($model,'newemail',array('size'=>32,'maxlength'=>64, 'class'=>'input1')); ?></div>
        <div class="col col-error"><?php echo $form->error($model,'newemail'); ?></div>
    </div>

    <div class="row">
        <div class="col col-label"><?php echo $form->labelEx($model,'password', array('class'=>'label1')); ?></div>
        <div class="col col-input"><?php echo $form->passwordField($model,'password',array('size'=>32,'maxlength'=>128, 'class'=>'input1')); ?></div>
        <div class="col col-error"><?php echo $form->error($model,'password'); ?></div>
    </div>

    <div class="row buttons">
        <div class="col col-label"></div>
        <div class="col col-input"><?php echo CHtml::submitButton('Сменить пароль', array('class'=>'submit1')); ?></div>
    </div>

    <?php $this->endWidget(); ?>

</div><!-- form -->

问题:提交表单后,我的应用程序抛出异常:ChangeEmailForm 及其行为没有名为“tableName”的方法或闭包。

问题:为什么会CFormModel抛出这个异常?为什么一切都在 in 的例子中LoginForm起作用SiteController

PS抱歉,我是 Yii 的初学者。

4

1 回答 1

3

您的 中有以下rules()方法ChangeEmailForm

public function rules()
{
    return array(
        array('newemail, password', 'required'),
        array('newemail', 'email'),
        array('newemail', 'unique', 'attributeName'=>'User.email'),
        array('password', 'authenticate'),
    );
}

正如人们所看到的,用于属性的唯一newemail验证器只能应用于CActiveRecord通过tableName()与某些数据库表链接,但不能应用于CFormModel

验证属性值在相应的数据库表中是唯一的。

相反,您可以为表单模型编写自定义验证方法,以测试输入的电子邮件是否已存在于模型表示的数据库表中User

于 2013-06-20T20:46:38.803 回答