Php 如何基于另一个元素验证元素';s值多少?
我有一个客户端ID和一个唯一的客户端哈希。当我注册这些数据时,它工作正常。 我只是想说清楚,我不生成散列 我用于验证唯一哈希是否已存在的代码:Php 如何基于另一个元素验证元素';s值多少?,php,oop,zend-framework,zend-form,zend-validate,Php,Oop,Zend Framework,Zend Form,Zend Validate,我有一个客户端ID和一个唯一的客户端哈希。当我注册这些数据时,它工作正常。 我只是想说清楚,我不生成散列 我用于验证唯一哈希是否已存在的代码: protected function _getValidator($field) { return array( 'Db_NoRecordExists', true, array( 'table' => 'anunciantes', 'field
protected function _getValidator($field)
{
return array(
'Db_NoRecordExists',
true,
array(
'table' => 'anunciantes',
'field' => $field,
'messages' => array(
'recordFound' => ucfirst($field) . ' "%value%" is registered'
)
)
);
}
但当我必须编辑该客户机时,我想验证该散列是否已经存在,以及该散列是否属于该客户机
我怎么做?我已经尝试通过使用db validator的“exclude”选项并传递
$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\u 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());
}
在编辑用户与创建用户时,需要使用一些逻辑来交换验证程序。您可以这样设置:
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()
希望对您有所帮助。要验证哈希是否已经存在并且属于特定用户,您可以使用以下带有自定义排除条件的验证器。我假设散列是一个隐藏的文本字段,包含来自数据库的散列
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\u 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());
}
在编辑用户与创建用户时,需要使用一些逻辑来交换验证程序。您可以这样设置:
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']
)
)
);
您必须在表单中为该字段指定一个验证器,如
$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']
)
)
);