3

How do i validate multidimensional array in Zend Framework (Zend_Filter_Input)?

Example:

  • Input must be array
  • Input must have 'roles' and 'name'
  • 'roles' must be array
  • All element in 'roles' must be array
  • All element in 'roles' must have 'name' and 'id', 'access' is optional
  • 'id' must be int
  • 'access' must be array

$input = array(
    'roles' => array(
        array('name' => 'Test', 'id' => 1),
        array('name' => 'Test2', 'id' => 2, 'access' => array('read', 'write'))
    ),
    'name' => 'blabla'
);
John Conde
  • 217,595
  • 99
  • 455
  • 496
Morfi
  • 101
  • 1
  • 3

1 Answers1

1

There was a similar question some days ago: Passing an array as a value to Zend_Filter

In short, if you use Zend_Filter_Input, it will pass the array values individually to the associated validators. So, it's not possible to use the array as a whole, but the individual components.

EDIT: A possible solution would be to create your own specific Zend_Validate class and include all the checks in the isValid method, something like the following:

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;
    }
}

Hope that helps,

Community
  • 1
  • 1
dinopmi
  • 2,683
  • 19
  • 24
  • Does this mean Morfi would have to apply all of the items in his 'logical validation' list manually from his client code? And if so, where would be the appropriate place to do this? In the form model? – cantera Nov 29 '11 at 15:24
  • @cantera25: I've updated my answer with the approach I would take. – dinopmi Nov 29 '11 at 16:01