Cakephp isUnique 验证不会在一个字段上触发,但会在另一个字段上触发
当我保存注册表中的信息时,我在用户名和电子邮件字段上有一些验证规则(我已粘贴下面的规则)。使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
“isUnique”规则查找其他行中的重复值。如果“isUnique”测试也是主键,这将产生问题。因此,根据上面的规则/模型,如果您在运行规则时查看 SQL 转储,您可能会看到类似这样的内容,
这显然在测试中返回 0 或“唯一”。修复方法是将主键设置为等于您正在测试规则的字段之外的其他值。当您这样做时,SQL 转储应该显示类似这样的内容:
当字段不唯一时,这将返回 >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
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
This will return the >0 when the field is not unique and the desired functionality will be restored.
我仍然找不到问题所在,因此作为临时解决方案,我编写了一个自定义规则。如果其他人需要这个:
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:
以下是在 cakephp 2.3 中如何使用 isUnique 验证规则。假设您有一个“用户名”字段想要唯一,下面的代码就可以实现这一点。
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.
看了我的用户模型,它看起来像这样:
Looked at my user model, and it looks like this:
将 $this->Form->hidden('id') 放入表单中。它应该有效。
Place $this->Form->hidden('id') in your form. It should work.