2

Zend performs validation for Zend_Filter_Input recursively, so the code:

    $data = array(
        'update' => array(1, 2, 3)
    );

    $validators = array(
        'update' => array(
            new Zend_Validate_Callback('is_array'),
            'presence' => 'required'
        )
    );

    $filter = new Zend_Filter_Input(array(), $validators, $data);
    var_dump($filter->isValid());
    var_dump($filter->getMessages());

returns false and messages that 1, 2 and 3 aren't valid values.

Any ways to validate if a value is array, without recursive rule applying?

8
  • don't wanna use $data = array( 'update' => array(array(1, 2, 3)) ); ? Commented May 1, 2012 at 4:30
  • Did you find a solution ? I've looked in Zend_Filter_Input source, on :1011 it makes field value an array in case if it's not, and foreach over it on :1024. So I'm guessing the only solution will be to extend Zend_Filter_Input, or wrap value in array as suggested before ? Commented May 1, 2012 at 11:33
  • @b.b3rn4rd: not yet - haven't found enough time at work Commented May 1, 2012 at 11:48
  • Your question is another good reason why I created my own IsArray validator class that does a little bit more than just is_array(). Commented May 1, 2012 at 13:53
  • 1
    @Adrian World: how would it help in this case? Zend_Filter_Input performs recursive traversing, so a particular validator can nothing to do with it Commented May 1, 2012 at 20:30

1 Answer 1

1

As @zerkms said :

Zend_Filter_Input performs recursive traversing, so a particular validator can nothing to do with it.

To solve this problem, I'm using a "durty" way :

$input = new Zend_Filter_Input(
    array(
        'the_field_should_be_array'=> array(
             new MyPersonalValidator($this->_request->getParam('the_field_should_be_array')),
         )
    )
);


 class MyPersonalValidator extends Zend_Validate_Abstract{

    private $_paramIsValid;

public function __construct($param)
{
    $this->_paramIsValid = is_array($param);
}

public function isValid($not_usefull)
{
    if(!$this->_paramIsValid)
    {
        return false;
    }

    return true;
}}

Code explanation : I perform the validation of the field in the constructor of the validator. For this, It's needed to pass the field to the constructor and it not a good pratice but I didn't find another way.

Edit : The clean way is to extend Zend_Filter_Input to support the context. I never new why it wasn't implement before.

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

1 Comment

It's too dirty to be used :-) Checking since there will be no other answers

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.