如何根据另一个元素的值验证一个元素?
我有一个客户端 ID 和一个唯一的客户端哈希值。当我注册这些数据时,它工作正常。 需要明确的是,我不生成哈希值。
我用来验证该唯一哈希是否已存在的代码:
protected function _getValidator($field)
{
return array(
'Db_NoRecordExists',
true,
array(
'table' => 'anunciantes',
'field' => $field,
'messages' => array(
'recordFound' => ucfirst($field) . ' "%value%" is registered'
)
)
);
}
但是当我必须编辑该客户端时,我想验证该哈希是否已存在,以及该哈希是否属于该客户端。
我怎么做呢?我已经尝试通过使用数据库验证器的“排除”选项并传递 $this->getValue('id')
来获取 id 的值,但是该调用返回null
。
I have a Client ID, and a Unique Client hash. When I register these data, it works fine.
Just to be clear, I do not generate the hash.
The code I use to validate if that unique hash already exists:
protected function _getValidator($field)
{
return array(
'Db_NoRecordExists',
true,
array(
'table' => 'anunciantes',
'field' => $field,
'messages' => array(
'recordFound' => ucfirst($field) . ' "%value%" is registered'
)
)
);
}
But when I have to edit that Client, I want to validate if that hash already exists, and if that hash belongs to that Client.
How I do it? I already tried to get the value of the id by using 'exclude' option of db validator and passing $this->getValue('id')
, but that call returns null
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要验证哈希是否已存在并且属于特定用户,您可以使用以下带有自定义排除条件的验证器。我假设哈希是一个隐藏文本字段,其中包含数据库中的哈希。
这将检查以确保元素中包含的哈希值与属于
user_id
的数据库中的哈希值相匹配。我们将覆盖排除选项,以便哈希值必须与为给定用户 ID 设置的值相匹配。该查询应大致如下所示:
在编辑用户与创建用户时,您将需要使用一些逻辑来交换验证器。您可以这样设置:
另外,在您的问题中您提到使用
$this->getValue('id')
;我相信这实际上应该是$this->getElement('id')->getValue()
。希望有帮助。
To validate if the hash already exists and belongs to a specific user, you can use the following validator with a custom exclude condition. I assume that the hash is a hidden text field containing the hash from the database.
This will check to make sure that the hash contained in the element matches the hash in the database that belongs to
user_id
. We are overriding the exclude option to make it so the hash value must match what is set for the given user id.The query should look roughly like this:
You will need to use some logic to swap the validators when editing a user vs creating a user. You might set it like this:
Also, in your question you mentioned using
$this->getValue('id')
; I believe this should actually be$this->getElement('id')->getValue()
.Hope that helps.
您必须将表单中的验证器添加到该字段,例如......
You have to abb a validator in your form to that field like..