Zend_Validate: Db_NoRecordExists with Doctrine

Hi, I am trying to validate a form using Zend_Validate and Zend_Form.

My item:

$this->addElement('text', 'username', array( 'validators' => array( array( 'validator' => 'Db_NoRecordExists', 'options' => array('user','username') ) ) )); 

I use Doctrine to process my database, Zend_Validate skips the DbAdapter. I can pass the adapter into the parameters, but how can I combine Zend_Db_Adapter_Abstract and Doctrine?

Perhaps there is an easier way to do this?

Thanks!

+2
source share
1 answer

I decided it with my 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 as follows:

 $this->addElement('text', 'username', array( 'validators' => array( array( 'validator' => new Validator_NoRecordExists('User','username') ) ) )); 
+9
source

Source: https://habr.com/ru/post/888719/


All Articles