Cakephp isUnique 验证不会在一个字段上触发,但会在另一个字段上触发

发布于 2024-09-09 09:28:47 字数 1212 浏览 2 评论 0原文

当我保存注册表中的信息时,我在用户名和电子邮件字段上有一些验证规则(我已粘贴下面的规则)。使用 saveAll() 函数自动调用验证。

问题是用户名字段上的 isUnique 规则根本不起作用(不返回任何错误)。该字段的其他规则工作得很好,电子邮件字段的唯一验证也有效。当这两个规则基本相同时,我似乎无法弄清楚为什么会发生这种情况。

var $validate = array(

    'username' => array(

        'isUnique' => array (

            'rule' => 'isUnique',

            'message' => 'This username already exists.'),

        'custom' => array (

            'rule' => array('custom', '/^[A-Za-z0-9,\.-_]*$/i'), 

            'message' => 'The username can only contain letters, numbers, _, - and .'),

        'minLength' => array(

            'rule' => VALID_NOT_EMPTY,

            'message' => 'You must fill in the username.')          
    ),

    'email' => array(

        'isUnique' => array (

            'rule' => 'isUnique',

            'message' => 'This email address already exists in our database.'),

        'valid' => array (

            'rule' => array('email', false),

            'message' => 'Invalid email.'),

        'minLength' => array(

            'rule' => VALID_NOT_EMPTY,

             'message' => 'You must fill in the email address.')

    )

);

When I save the information from a registration form I have some validation rules on the username and email fields (I've pasted the rules below). Validation is called automatically with the saveAll() function.

The problem is that the isUnique rule on the username field doesn't work at all (doesn't return any error). The other rules for this field work just fine, and the unique validation for the email field also works. I can't seem to figure out why this is happening, when the two rules are basically the same.

var $validate = array(

    'username' => array(

        'isUnique' => array (

            'rule' => 'isUnique',

            'message' => 'This username already exists.'),

        'custom' => array (

            'rule' => array('custom', '/^[A-Za-z0-9,\.-_]*$/i'), 

            'message' => 'The username can only contain letters, numbers, _, - and .'),

        'minLength' => array(

            'rule' => VALID_NOT_EMPTY,

            'message' => 'You must fill in the username.')          
    ),

    'email' => array(

        'isUnique' => array (

            'rule' => 'isUnique',

            'message' => 'This email address already exists in our database.'),

        'valid' => array (

            'rule' => array('email', false),

            'message' => 'Invalid email.'),

        'minLength' => array(

            'rule' => VALID_NOT_EMPTY,

             'message' => 'You must fill in the email address.')

    )

);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

陌伤ぢ 2024-09-16 09:28:47

“isUnique”规则查找其他行中的重复值。如果“isUnique”测试也是主键,这将产生问题。因此,根据上面的规则/模型,如果您在运行规则时查看 SQL 转储,您可能会看到类似这样的内容,

SELECT COUNT(*) FROM users WHERE username = 'x' AND username != 'x'

这显然在测试中返回 0 或“唯一”。修复方法是将主键设置为等于您正在测试规则的字段之外的其他值。当您这样做时,SQL 转储应该显示类似这样的内容:

SELECT COUNT(*) FROM users WHERE username = 'x' AND key != 'y'

当字段不唯一时,这将返回 >0,并且将恢复所需的功能。

The "isUnique" rule looks for repeating values in other rows. Which will create a problem if the "isUnique" test is also the primary key. So, based on your rules/model above, if you look at the SQL dump when the rule is run, you will likely see something like

SELECT COUNT(*) FROM users WHERE username = 'x' AND username != 'x'

This obviously returns 0 or "unique" in the test. Fix would be to set the primary key equal to something other than the field you are testing the rule on. When you do that, the SQL dump should show something like

SELECT COUNT(*) FROM users WHERE username = 'x' AND key != 'y'

This will return the >0 when the field is not unique and the desired functionality will be restored.

是你 2024-09-16 09:28:47

我仍然找不到问题所在,因此作为临时解决方案,我编写了一个自定义规则。如果其他人需要这个:

var $validate = array(
    'username' => array(
        'isUnique' => array (
            'rule' => array('checkUniqueUser'),
            'message' => 'This username already exists.'
        )
    )
)

function checkUniqueUser() {
    return ($this->find('count', array('conditions' => array('User.username' => $this->data['User']['username']))) == 0);
}

I still can't find what the problem is, so as a temporary solution I've written a custom rule. In case somebody else needs this:

var $validate = array(
    'username' => array(
        'isUnique' => array (
            'rule' => array('checkUniqueUser'),
            'message' => 'This username already exists.'
        )
    )
)

function checkUniqueUser() {
    return ($this->find('count', array('conditions' => array('User.username' => $this->data['User']['username']))) == 0);
}
怕倦 2024-09-16 09:28:47

以下是在 cakephp 2.3 中如何使用 isUnique 验证规则。假设您有一个“用户名”字段想要唯一,下面的代码就可以实现这一点。

'username' => array(
'notempty' => array(
'rule' => array('notempty'),
'update' operations
),
'rule' => 'isUnique',
'message' => 'User alread exists!'
),

Here is how to use isUnique valdiation rule in cakephp 2.3. let say you have a field 'username' wants to be unique, the following code will do the trick.

'username' => array(
'notempty' => array(
'rule' => array('notempty'),
'update' operations
),
'rule' => 'isUnique',
'message' => 'User alread exists!'
),
深居我梦 2024-09-16 09:28:47

看了我的用户模型,它看起来像这样:

var $validate = array(
    'username' => array(
        'inUse' => array (
            'rule' => array('isUnique', 'username')
        )
    )
)

Looked at my user model, and it looks like this:

var $validate = array(
    'username' => array(
        'inUse' => array (
            'rule' => array('isUnique', 'username')
        )
    )
)
给妤﹃绝世温柔 2024-09-16 09:28:47

将 $this->Form->hidden('id') 放入表单中。它应该有效。

Place $this->Form->hidden('id') in your form. It should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文