本文介紹了Zend_Validate: Db_NoRecordExists with Doctrine的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
您好,我正在嘗試使用 Zend_Validate 和 Zend_Form 驗(yàn)證表單.
Hey there, I'm trying to validate a form with Zend_Validate and Zend_Form.
我的元素:
$this->addElement('text', 'username', array(
'validators' => array(
array(
'validator' => 'Db_NoRecordExists',
'options' => array('user','username')
)
)
));
因?yàn)槲沂褂?Doctrine 來處理我的數(shù)據(jù)庫,Zend_Validate 錯(cuò)過了一個(gè) DbAdapter.我可以在選項(xiàng)中傳遞一個(gè)適配器,但是我如何結(jié)合 Zend_Db_Adapter_Abstract 和 Doctrine?
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?
是否有更簡單的方法來完成這項(xiàng)工作?
Is there maybe an easyer way to get this done?
謝謝!
推薦答案
用自己的驗(yàn)證器解決了:
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;
}
}
這樣使用:
$this->addElement('text', 'username', array(
'validators' => array(
array(
'validator' => new Validator_NoRecordExists('User','username')
)
)
));
這篇關(guān)于Zend_Validate: Db_NoRecordExists with Doctrine的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!