2011-11-29 58 views
3

我如何驗證Zend框架(Zend_Filter_Input)中的多維數組?Zend Validators和多維數組

實施例:

  • 輸入必須是陣列
  • 輸入必須具有「角色」和「名稱」
  • 「角色」必須是陣列
  • 所有元件中的角色'必須是陣列
  • 在「角色」的所有元素都必須有「名」和「身份證」,「訪問」是可選
  • 「ID」必須是int
  • 「訪問」必須是數組

$input = array(
    'roles' => array(
     array('name' => 'Test', 'id' => 1), 
     array('name' => 'Test2', 'id' => 2, 'access' => array('read', 'write')) 
    ), 
    'name' => 'blabla' 
); 

回答

1

也有類似問題,一些天前:Passing an array as a value to Zend_Filter

總之,如果你使用Zend_Filter_Input,它將分別傳遞數組值的相關的驗證器。所以,不可能將數組作爲一個整體來使用,而是使用單個組件。

編輯:一個可能的解決方案是創建自己的特定Zend_Validate類,幷包括在isValid方法所有的檢查,類似如下:

class MyValidator extends Zend_Validate_Abstract 
{ 
    const MESSAGE = 'message'; 

    protected $_messageTemplates = array(
     self::MESSAGE => "Invalid format for the array" 
    ); 

    public function isValid($value) 
    { 
     if (!is_array($value)) { 
      $this->_error(); 
      return false; 
     } 

     // ... 

     return true; 
    } 
} 

希望幫助,

+0

不這意味着Morfi將不得不從他的客戶端代碼手動應用他的「邏輯驗證」列表中的所有項目?如果是這樣,那麼將在哪裏做適當的地方呢?在表單模型中? – cantera

+0

@ cantera25:我已經用我將採取的方法更新了我的答案。 – dinopmi