我有一个客户端ID,和一个唯一的客户端哈希。当我注册这些数据时,它工作正常。为了清楚起见,我没有生成哈希值。
我用来验证该唯一哈希是否已经存在的代码:
protected function _getValidator($field)
{
return array(
'Db_NoRecordExists',
true,
array(
'table' => 'anunciantes',
'field' => $field,
'messages' => array(
'recordFound' => ucfirst($field) . ' "%value%" is registered'
)
)
);
}
但是当我必须编辑客户端时,我想要验证该哈希是否已经存在,以及该哈希是否属于该客户端。
我怎么做?我已经尝试通过使用'exclude' db验证器选项并传递$this->getValue('id')
来获取id的值,但该调用返回null
。
要验证哈希是否已经存在并且属于特定用户,您可以使用以下验证器和自定义排除条件。我假设哈希值是一个隐藏的文本字段,其中包含来自数据库的哈希值。
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
的数据库中的哈希值匹配。我们重写了exclude选项,使其哈希值必须与给定用户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()
。
希望对你有帮助。
您必须在表单中为该字段添加验证器,如…
$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']
)
)
);