密码确认无效,未定义

时间:2015-06-01 04:06:48

标签: php yii

如标题所述。我收到以下错误消息

Property "User.repeat_password" is not defined.

虽然我已经在我的用户模型中定义了它。

class User extends CActiveRecord
{

    public $repeat_password;

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'sys_user';
    }


    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('password, repeat_password', 'required', 'on'=>'insert'),
            array('password, repeat_password', 'length', 'min'=>6, 'max'=>40),
            array('repeat_password', 'compare', 'compareAttribute'=>'password', 'on'=>'create'),

            array('username', 'length', 'max'=>10),
            array('isactive', 'safe'),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('userid, username, password, isactive, role', 'safe', 'on'=>'search'),
        );
    }

这是视图代码

<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'variable-form',
    'htmlOptions'=>array(
        'role'=>'form',
    ),
    'enableAjaxValidation'=>false,
)); ?>

    ...

    <div class="form-group">
        <?php echo $form->labelEx($model,'password'); ?>
        <?php echo $form->passwordField($model,'password', array('class'=>'form-control input-sm')); ?>
        <?php echo $form->passwordField($model,'repeat_password', array('class'=>'form-control input-sm')); ?>
        <?php echo $form->error($model,'password'); ?>
    </div>

    ...


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

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

可能是什么问题?请帮助,我是Yii的新手。我应该在数据库中添加新列吗?

1 个答案:

答案 0 :(得分:0)

这是因为您从CActiveRecord延伸。我认为在你的表中sys_user不存在列repeat_password。我理解你想要创建一个包含任何验证,字段和处理的表单。您需要创建自定义类并从Form扩展。 我的表格示例:

class RegistrationForm extends CFormModel
{

/**
 * @var string
 */
public $password;

/**
 * @var string
 */
public $repeat_password;

/**
 * @inheritdoc
 */
public function rules()
{
    return array(
        array('password, repeat_password', 'required'),
        array('password', 'compare', 'compareAttribute' => 'repeat_password'),
    );
   }
} 

使用方法:

 // in controller 
    $form = new RegistrationForm();
    if (isset($_POST['RegistrationForm'])) {
        $form->attributes = $_POST['RegistrationForm'];
        $form->validate();
     }
    $this->render('registration', array('model' => $form));
    //in view
    ...
    <?php echo $form->passwordField($model,'password', array('class'=>'form-control input-sm')); ?>
    <?php echo $form->error($model,'password'); ?>
    <?php echo $form->passwordField($model,'repeat_password', array('class'=>'form-control input-sm')); ?>
    <?php echo $form->error($model,'repeat_password'); ?>
    <?php $this->endWidget(); ?>