2012-08-28 142 views
1

我寫了這段代碼,它完美地工作。我唯一不能改變的是默認的回調錯誤消息「const INVALID_VALUE ='callbackValue';」在Zend_Validate_Callback中。Zend_Validate_Callback覆蓋self :: INVALID_VALUE消息

$validators['ip'][] = array('Ip'); 
$validators['ip'][] = array('Callback', array('callback' => array($this->_ip, 'ipUnique'))); 
$filters = array('*' => 'StringTrim'); 
$input = new Zend_Filter_Input($filters, $validators, $_POST); 
$input->setOptions(array('presence' => 'required')); 

if($input->isValid()){ 
    // all okay 
} 
else{ 
    return $input->getMessages(); 
} 

任何想法?

回答

0

該類看起來像這樣,你只能從源代碼中改變const值。如果你想改變這個,然後找到Zend_Validate_Callback並在代碼中進行更改。

class Zend_Validate_Callback extends Zend_Validate_Abstract implements Zend_Validate_Interface{ 
/* Constants */ 

const INVALID_CALLBACK="callbackInvalid"; <-- here you change the value 
const INVALID_VALUE="callbackValue"; <-- here you change the value 
/* Public methods defined in class */ 

public function __construct (string|array $callback=null) 
public function getCallback () 
public function setCallback (string|array $callback) 
public function getOptions () 
public function setOptions (mixed $options) 
public function isValid (mixed $value) 
/* Public methods inherited from class Zend_Validate_Abstract */ 

public function getMessages () 
public function getMessageVariables () 
public function getMessageTemplates () 
public function setMessage (string $messageString, string $messageKey=null) 
public function setMessages (Array $messages) 
public function __get (string $property) 
public function getErrors () 
public function setObscureValue (bool $flag) 
public function getObscureValue () 
public function setTranslator (Zend_Translate|Zend_Translate_Adapter|null $translator=null) 
public function getTranslator () 
public function hasTranslator () 
public static function setDefaultTranslator (Zend_Translate|Zend_Translate_Adapter|null $translator=null) 
public static function getDefaultTranslator () 
public static function hasDefaultTranslator () 
public function setDisableTranslator (bool $flag) 
public function translatorIsDisabled () 
public static function getMessageLength () 
public static function setMessageLength (integer $length=-1) 
} 

在這裏閱讀更多http://zfdoc.hafees.com/class/Zend_Validate_Callback.html#class,Zend_Validate_Callback

+0

不要誤會我的意思,我不想改變消息的鑰匙,我想改變默認值: 保護$ _messageTemplates =陣列( self :: INVALID_VALUE =>「'%value%'無效」, self :: INVALID_CALLBACK =>「回調中發生異常」, ); – ibram

1

找到了答案,我的問題尋找到了ZF的源代碼後,您不能在基於陣列結構覆蓋其消息,「消息」,在迷路的選項Zend_Validate_Callback的構造函數。 Zend_Validate_Abstract有一個公共方法「setMessages」,它很好地覆蓋了回調錯誤消息。

最終代碼:

$callbackValidator = new Zend_Validate_Callback(array($this->_ip, 'ipUnique')); 
$callbackValidator->setMessages(array(Zend_Validate_Callback::INVALID_VALUE => "'%value%' is not a unique IP address")); 

$validators['ip'][] = array('Ip'); 
$validators['ip'][] = $callbackValidator; 
$filters = array('*' => 'StringTrim'); 
$input = new Zend_Filter_Input($filters, $validators, $_POST); 
$input->setOptions(array('presence' => 'required')); 

if($input->isValid()){ 
    // all okay 
} 
else{ 
    return $input->getMessages(); 
}