【问题标题】:Yii 'Unique' Validator in CFormModelCFormModel 中的 Yii 'Unique' 验证器
【发布时间】:2012-02-26 15:29:07
【问题描述】:

我在 Yii 框架中有这么一小行代码:

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'),
        );
    }
}

【问题讨论】:

  • 您的问题是什么?我猜你需要在某个地方告诉 Yii 寻找独特性。
  • 您能准确告诉我必须添加或更改的内容吗?
  • 不,我不能,但我确信这在 Yii 网站上有记录。
  • 感谢您给我无用的、类似猜测的答案

标签: php forms frameworks yii validation


【解决方案1】:

您必须在该验证器中指定一个附加属性className。它应该包含应该检查其表的唯一性的 Active Record 类的名称。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-15
    • 1970-01-01
    • 2012-07-04
    • 2015-11-25
    • 2014-11-07
    • 1970-01-01
    • 2011-11-02
    • 2016-07-01
    相关资源
    最近更新 更多