2013-09-25 22 views
2

心中已經creatated一個自定義的驗證:的Zend 表格表格不是從自定義驗證displayin錯誤信息

class MyValidator extends AbstractValidator 
{ 
    const ERROR_CONST = 'error'; 

    protected $dbAdapter; 
    protected $messageTemplates = array(
     self::ERROR_CONST => "Error msg for '%value%'." 
    ); 

    public function __construct($dbAdapter) 
    { 
     $this->dbAdapter = $dbAdapter; 
    } 

    public function isValid($value, $context = null) 
    { 
     $this->setValue($value); 
     /** 
     * Do validation against db 
     */ 

     if(/* Not valid */){ 
      $this->error(self::ERROR_CONST); 
      return false; 
     } 
     return true; 
    } 
} 

驗證工作,我已經能夠對其進行測試。什麼不工作是使用

echo $this->formElementErrors($form->get('action')); 

所有這一切都輸出是一個空UL錯誤信息的輸出。這是一個翻譯問題嗎?當我在驗證器中對$ this-> getTranslator()執行get_class時,我得到驗證器類的名稱。當我var_dump $ this-> getTranslator()它輸出null。我是否需要設置一個翻譯人員才能使其工作,並且在哪裏設置翻譯人員的最佳位置,以便爲我自己的驗證人員提供系統範圍的信息?

+0

你可以把一些代碼,你如何使用它? –

回答

2

因爲您爲您的驗證類__construct方法,父__construct不隱式調用: http://php.net/manual/en/language.oop5.decon.php(見注)

你應該修改__construct方法:

public function __construct($dbAdapter) 
{ 
    $this->dbAdapter = $dbAdapter; 
    //parent::__construct($options); 
    parent::__construct(null); // or (void) 
} 

如您所見,$messageTemplates$messageVariablesAbstractValidator::__construct「加載」,用於某些方法(error包括):

https://github.com/zendframework/zf2/blob/master/library/Zend/Validator/AbstractValidator.php#L73-L79

+0

就這麼簡單。有時你只需要有人指出明顯。謝謝! – Borje

0

也許你忘了添加messageVariables

/** 
* Message variables 
* @var array 
*/ 
protected $messageVariables = array(
    'value' => 'value', 
); 
+0

我不使用任何變量,除了值參數。根據文檔,不需要指定messageVariables變量。但只是嘗試一下,我想要提前添加它,但仍然沒有運氣。 – Borje