php - Zend_Validate : Db_NoRecordExists with Doctrine

标签 php zend-framework doctrine

嘿,我正在尝试使用 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

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')
            )
    )
));

关于php - Zend_Validate : Db_NoRecordExists with Doctrine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1395947/

相关文章:

php - 为什么排序会失败?

php - 此结果是一个仅向前的结果集,不支持在向前移动后调用 rewind() - Zend

php - 使用数组的 Doctrine 查询生成器

php - 获取 Doctrine 1.2 - Symfony 1.4 中的表/模型列表并进行描述?

php - 使用 jQuery 和 SQL 从表中删除数据

php - Switch Case - 并非所有情况都有效

PHP/MYSQL 语句因此选择最大连接之和

php - 如何获取 OpenID 用户个人资料信息?

php - 如何计算两个 Zend_Date 对象之间的差异,以月为单位

symfony - 使用 jms 序列化程序和 ISO8601 获取日期时间格式不匹配