What I've been doing is creating a subclass of Zend_Filter_Input that adds the ability to automatically add filters and inputs from class members and automatically sets up paths for filters/validators. So the following is possible:

class Input_Form extends Jb_Filter_Input
{
        $_filters = array ( ... );

        $_validators = array ( ... )';
}

This simplifies code a lot, and is especially useful if you have a lot of forms requiring validation.

If you're expecting one of your incoming variables to be an array (say for instance the 'phone[]' variable you described) then Zend_Filter_Input will automatically apply Filters and Validators to each element of that array separately (i.e. it wont actually pass an array to your filters and validators). So in your case you need to name each of the fields separately and use the fields Meta-command to specify which fields should be passed to your validator. That way, your validator will recieve an array containing the fields you specified. e.g.:

class Input_Form extends Jb_Filter_Input
{
        $_filters = array ( ... );

        $_validators = array (
                'phone' => array (
                        'fields' => array ('phone-1', 'phone-2', 'phone-3', 
'phone-4'),
                        'PhoneValidator'
                )
        );
}

Obviously you'd need to implement the phone validator first. SpotSec had a great example article regarding validating multiple fields with a single rule:

http://www.spotsec.com/blogs/archive/2007/8/18/zend_validate- stringequals-implementation?Itemid=125

Cheers,

Mathew Byrne

On 27/10/2007, at 12:09 PM, Ryan Lange wrote:

I'm curious how others are using the current (as of ZF 1.0.2) implementation of Zend_Filter_Input. I'm trying to use it for a contact form, but everything I've typed out seems clumsy and overly complicated. (Maybe that's just the cost of flexibility, though, because even the "less basic" examples in the documentation look extremely convoluted.)

For instance, I have one hidden field (using CSS) on the form that's used in spam detection. This field needs to remain completely untouched by the filters I set, but in order to do that, I have to explicitly assign the filters to /every/ other field (14 of them) rather than use the "*" wildcard.

Also, I have no clue how to handle multiple fields of the same name. This same contact form allows the user to provide their phone number if they wish. This consists of 4 fields that represent parts of a U.S. telephone number, all with the name "phone[]": ([___]) [___]-[____] ext. [_____]

If any one can provide any insight or examples of their own of their, that'd be great.


Thanks,
Ryan Lange

Reply via email to