3

My goal is to validate parameters passed in the URL, so I created a validate method that has a list of validators to run, like so:

$validators = array(
        'number' => array(
            'digits',
            'presence' => 'required',
            'messages' => array(
                "%value%' is not a valid number.",
            ),
        ),
        'country' => array(
            'presence' => 'required',
            'InArray' => array('haystack' => array('USA', 'CAN', 'AUS', 'JPN')),
            'messages' => array(
                "'%value%' is not a valid country code.",
            ),
        ),
        // etc. 
);

$valid = new Zend_Filter_Input(array(), $validators, $data);
return $valid->isValid()

The issue is that the 'InArray' validator does nothing. It doesn't raise any errors, it just doesn't work. I assume that I'm getting the syntax wrong.

What is the correct syntax for the 'InArray' validator?

3
  • What are you passing those validators to? haystack should probably be its own index like messages is, not an array element on the InArray key. Commented Aug 14, 2012 at 19:23
  • I updated the question for clarity. Are you saying that 'InArray' would be declared like 'digits', with 'haystack' as a separate index? [Ok, that doesn't work. It results in a Missing argument for Zend_Validate_InArray] Commented Aug 14, 2012 at 20:21
  • See my answer, I thought you may have been trying to pass the array to Zend_Form which would accept a haystack parameter although not in the format provided above. Commented Aug 14, 2012 at 20:31

1 Answer 1

3

To pass additional rules and properties to validators to be used with Zend_Filter_Input, create a concrete instance of the object and set it as your validator like this:

    $validators = array(
            'number' => array(
                    'digits',
                    'presence' => 'required',
                    'messages' => array(
                            "%value%' is not a valid number.",
                    ),
            ),
            'country' => array(
                    new Zend_Validate_InArray(
                        array('haystack' => array('USA', 'CAN', 'AUS', 'JPN'))
                    ),
                    'presence' => 'required',
                    'messages' => array(
                            "'%value%' is not a valid country code.",
                    ),
            ),
            // etc.
    );

The reason you have to do it like this is because there are no filter metacommands for setting the haystack when using the InArray validator. There are some basic metacommands that apply to many validators, but haystack is not one of them.

To specify the haystack, create a new Zend_Validate_InArray object directly with the require options and pass that validator to the array of validators given to Zend_Filter_Input.

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

Comments

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.