如何根据另一个元素的值验证一个元素?

发布于 2024-12-23 10:23:10 字数 642 浏览 4 评论 0原文

我有一个客户端 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 技术交流群。

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

发布评论

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

评论(2

眼前雾蒙蒙 2024-12-30 10:23:10

要验证哈希是否已存在并且属于特定用户,您可以使用以下带有自定义排除条件的验证器。我假设哈希是一个隐藏文本字段,其中包含数据库中的哈希。

protected function _getEditValidator($field, $userId, $db = null)
{
    if ($db == null) $db = Zend_Db_Table::getDefaultAdapter();

    array(
        'Db_RecordExists',
        true,
        array(
            'table' => 'anunciantes',
            'field' => $field,
            'exclude' => $db->quoteInto('user_id = ?', $userId)
        )
    );
}

这将检查以确保元素中包含的哈希值与属于 user_id 的数据库中的哈希值相匹配。我们将覆盖排除选项,以便哈希值必须与为给定用户 ID 设置的值相匹配。

该查询应大致如下所示:

SELECT `users`.`db_hash_col` FROM `users` WHERE (`db_hash_col` = :value) AND (user_id = 1234) LIMIT 1

在编辑用户与创建用户时,您将需要使用一些逻辑来交换验证器。您可以这样设置:

if ($editing) {
    $this->getElement('hash')
         ->setValidators(
             array($this->_getEditValidator('db_hash_col',
                                            $this->getElement('id')->getValue());
}

另外,在您的问题中您提到使用 $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.

protected function _getEditValidator($field, $userId, $db = null)
{
    if ($db == null) $db = Zend_Db_Table::getDefaultAdapter();

    array(
        'Db_RecordExists',
        true,
        array(
            'table' => 'anunciantes',
            'field' => $field,
            'exclude' => $db->quoteInto('user_id = ?', $userId)
        )
    );
}

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:

SELECT `users`.`db_hash_col` FROM `users` WHERE (`db_hash_col` = :value) AND (user_id = 1234) LIMIT 1

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:

if ($editing) {
    $this->getElement('hash')
         ->setValidators(
             array($this->_getEditValidator('db_hash_col',
                                            $this->getElement('id')->getValue());
}

Also, in your question you mentioned using $this->getValue('id'); I believe this should actually be $this->getElement('id')->getValue().

Hope that helps.

找回味觉 2024-12-30 10:23:10

您必须将表单中的验证器添加到该字段,例如......

$name->addValidator(
    'Db_NoRecordExists', 
    true, 
    array(
        'table' => 'currencies',
        'field' => $field,
        'messages' => array( "recordFound" => "This Client Id already exists in our DB") ,
        'exclude' => array(
             'field' => 'id',
             'value' => $this->_attribs['id']
         )
    )
);

You have to abb a validator in your form to that field like..

$name->addValidator(
    'Db_NoRecordExists', 
    true, 
    array(
        'table' => 'currencies',
        'field' => $field,
        'messages' => array( "recordFound" => "This Client Id already exists in our DB") ,
        'exclude' => array(
             'field' => 'id',
             'value' => $this->_attribs['id']
         )
    )
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文