Zend_Validate_Callback覆盖self :: INVALID_VALUE消息

时间:2012-08-28 12:34:09

标签: php validation zend-framework zend-validate

我写了这段代码,效果很好。我唯一无法改变的是默认的回调错误消息“const INVALID_VALUE ='callbackValue';”在Zend_Validate_Callback。

$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();
}

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

在查看ZF源代码后找到我的问题的答案,你不能在基于数组的结构中覆盖它的消息,选项“messages”在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();
}

答案 1 :(得分: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