Yii在CFormModel中的“独特”验证器

时间:2012-02-26 15:29:07

标签: php forms frameworks yii validation

我在Yii Framework中有这么小的代码:

array('username', 'unique', 'attributeName'=> 'username', 'caseSensitive' => 'false'),

这给了我一个错误:

RegisterForm and its behaviors do not have a method or closure named "tableName".

完整的RegisterForm.php模型:

<?php
class RegisterForm extends CFormModel
{
    public $username;
    public $password;
    public $password2;
    public $email;
    public $fullname;
    public $birth;

    public function rules()
    {
        return array(
            array('username, password, password2, email, fullname, birth', 'required'),

            array('username','length','min'=>3),
            array('username','length','max'=>16),
            array('username', 'filter', 'filter'=>'strtolower'),
            array('username', 'ext.alpha', 'allowSpaces'=>'flase', 'allAccentedLetters'=>'false'),
            array('username', 'unique', 'attributeName'=> 'username', 'caseSensitive' => 'false'),

            array('password', 'length', 'min' =>6),
            array('password', 'length', 'max' =>32),
            array('password', 'compare', 'allowEmpty' => 'false', 'compareAttribute' => 'password2', 'strict' => 'true'),

            array('email', 'length', 'min' =>8),
            array('email', 'length', 'max' =>128),
            array('email', 'email'),

            array('fullname','length','min'=>6),
            array('fullname','length','max'=>64),
            array('fullname', 'ext.alpha', 'allowNumbers'=>'false', 'allAccentedLetters'=>'false'),

            array('birth', 'date', 'allowEmpty'=>'false', 'format'=>'dd/MM/yyyy'),
        );
    }
}

1 个答案:

答案 0 :(得分:7)

您必须在该验证程序中指定其他属性className。它应该包含Active Record类的名称,应该检查其表的唯一性。

http://www.yiiframework.com/doc/api/1.1/CUniqueValidator#className-detail