Zend_Validate:Db_NoRecordExists with Doctrine [英] Zend_Validate: Db_NoRecordExists with Doctrine
问题描述
我的元素:
b
我正在尝试使用Zend_Validate和Zend_Form验证一个表单。 pre> $ this-> addElement('text','username',array(
'validators'=> array(
array(
'validator'=>'Db_NoRecordExists',
'options'=> array('user','username')
)
)
));
对于我使用Doctrine来处理我的数据库,Zend_Validate错过了一个DbAdapter。我可以在选项中传递一个适配器,但是如何组合Zend_Db_Adapter_Abstract和Doctrine?
有没有办法完成这项工作?
谢谢!
使用自己的验证器解决:
<?php
class Validator_NoRecordExists extends Zend_Validate_Abstract
{
private $ _table;
private $ _field;
const OK ='';
protected $ _messageTemplates = array(
self :: OK =>'%value%'ist bereits in der Datenbank
);
public function __construct($ table,$ field){
if(is_null(Doctrine :: getTable($ table)))
return null;
if(!Doctrine :: getTable($ table) - > hasColumn($ field))
return null;
$ this-> _table = Doctrine :: getTable($ table);
$ this-> _field = $ field;
}
public function isValid($ value)
{
$ this-> _setValue($ value);
$ funcName ='findBy'。 $这 - > _field;
if(count($ this-> _table-> $ funcName($ value))> 0){
$ this-> _error();
返回false;
}
返回true;
}
}
这样使用:
$ this-> addElement('text','username',array(
'validators'=> array(
array(
'validator'=> new Validator_NoRecordExists('User','username')
)
)
));
Hey there, I'm trying to validate a form with Zend_Validate and Zend_Form.
My element:
$this->addElement('text', 'username', array(
'validators' => array(
array(
'validator' => 'Db_NoRecordExists',
'options' => array('user','username')
)
)
));
For I use Doctrine to handle my database, Zend_Validate misses a DbAdapter. I could pass an adapter in the options, but how do I combine Zend_Db_Adapter_Abstract and Doctrine?
Is there maybe an easyer way to get this done?
Thanks!
Solved it with an own Validator:
<?php
class Validator_NoRecordExists extends Zend_Validate_Abstract
{
private $_table;
private $_field;
const OK = '';
protected $_messageTemplates = array(
self::OK => "'%value%' ist bereits in der Datenbank"
);
public function __construct($table, $field) {
if(is_null(Doctrine::getTable($table)))
return null;
if(!Doctrine::getTable($table)->hasColumn($field))
return null;
$this->_table = Doctrine::getTable($table);
$this->_field = $field;
}
public function isValid($value)
{
$this->_setValue($value);
$funcName = 'findBy' . $this->_field;
if(count($this->_table->$funcName($value))>0) {
$this->_error();
return false;
}
return true;
}
}
Used like that:
$this->addElement('text', 'username', array(
'validators' => array(
array(
'validator' => new Validator_NoRecordExists('User','username')
)
)
));
这篇关于Zend_Validate:Db_NoRecordExists with Doctrine的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!