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'
);

1 Answer 1

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,

Sign up to request clarification or add additional context in comments.

2 Comments

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?
@cantera25: I've updated my answer with the approach I would take.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.