2014-10-06 24 views
0

我有表單與集合。我附加驗證整個collecion - 我只是想檢查集合中的元素之間的某些關係的存在。 Zend Form Element Collection驗證作爲一個整體

它很好用。在數據錯誤的情況下 - 表單不會通過「isValid()」測試。

但有一個問題。 formElementErrors/getMessages沒有返回任何東西。 我做錯了什麼?

我的形式:

class Form implements InputFilterProviderInterface { 

    /** 
    * @return array 
    */ 
    public function getInputFilterSpecification() 
    { 

     return [ 
      [ 
       'name'  => 'legend', 
       'required' => true, 
       'allowEmpty' => false, 
       'validators' => [ 
        ['name' => 'Callback', 'options' => [ 
         'messages' => [ 
          \Zend\Validator\Callback::INVALID_VALUE => 'Wrong', 
         ], 
         'callback' => function ($values, $context=[]) { 
          return false; 
         }, 
        ]], 
       ] 
      ], 


     ]; 
    } 

    public function init() 
    { 
     $this->add(
      [ 
       'name' => 'legend', 
       'type' => 'Zend\Form\Element\Collection', 
       'options' => [ 
        'label'     => 'Legenda', 
        'count'     => 2, 
        'should_create_template' => true, 
        'allow_add'    => true, 
        'template_placeholder' => '__placeholder__', 
        'target_element'   => [ 
         'type' => 'Narzedzie\Form\Legenda\LegendyOpcjeFieldset', 
        ], 
       ], 
      ] 
     ); 
    } 
} 

,並查看:

$element = $NarzedzieForm->get('legend'); 

    var_dump($element->getMessages()); // in case of error - empty array! 
    echo $this->formElementErrors($element); // in case of error - empty string 
    echo $this->formColleciton($element); 

回答

0

也許你需要添加兩個消息?

'messages' => [ 
    \Zend\Validator\Callback::INVALID_VALUE => 'Wrong VALUE', 
    \Zend\Validator\Callback::INVALID_CALLBACK => 'Wrong CALLBACK', 
], 

因爲也許無效的回調消息被壓制,因爲你只提供一個?我希望它會回落到默認值。但是,所有這些驗證器消息對我來說似乎有點愚蠢。

看起來你在你的回調中有一個錯誤,這可能是拋出一個異常,並被try catch語句中的驗證器捕獲?

應該是?

function ($values, $context=[]) { 
    foreach ($values as $value) { 
     if ($value['el'] == '1') return false; 
    } 
    return true; 
}, 

在$ values中不是$ foreach中數組的值嗎?可能想要檢查該密鑰是否也設置了isset($ value ['el'])?

+0

我敢肯定,這不是在回調錯誤 - 相同的情況下,如果我只回調「返回假/真」;我要去檢查INVALID_CALLBACK。那個foreach部分只是例如。 – 2014-10-07 12:50:41