【问题标题】:Zend_Validate: Db_NoRecordExists with DoctrineZend_Validate: Db_NoRecordExists with Doctrine
【发布时间】:2010-11-26 15:02:18
【问题描述】:

您好,我正在尝试使用 Zend_Validate 和 Zend_Form 验证表单。

我的元素:

$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 zend-framework doctrine


    【解决方案1】:

    用自己的验证器解决了这个问题:

    <?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;
        }
    }
    

    这样使用:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-22
      • 2019-09-04
      • 1970-01-01
      • 2016-09-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多