Kevin McArthur-2 wrote:
> 
> Is there a way to validate array inputs with Zend_Filter_Input?
> [...]
> This info might be in the manual, but I can't seem to locate it.
> 

Hej Kevin,

if it's not in the Manual, read the code :). I also wanted to validate and
filter arrays and I found that the way Zend_Filter_Input handles arrays is
determined in the methods _validateRule and_filterRule. I have to mention
that these investigations are based on SVN Revision 6309, 2007-09-11
20:38:27Z.

In _validateRule you can find is_array($field) and in _filterRule you can
find is_array($this->_data[$field]). This is the point where
Zend_Filter_Input distinguishes normal values and arrays.

The way Zend_Filter_Input actually handles arrays is that it applies the
validation or filter to every element of the array. However it only handles
1-dimensional arrays. In case of multidimensional arrays it passes the
arrays of the second dimension to the validators and filters. This can lead
to some nasty bugs if you are not perfectly aware of this. For example
Zend_Validate_Alpha casts the value it is supposed to validate to a string.
If the value is an array it is cast to the string 'Array' which of course
passes the alpha validation eventhough an array is not clearly alpha,
especially if it contains numbers like array(5).

I plan to extend Zend_Filter_Input in order to enable array validation and
filtering. My idea is to overwrite the methods _validateRule and _filterRule
in a subclass. The simplest way to allow array validation and filtering
would be if the methods just wrap the value into an additional array like
array( $value ) and pass it to the parent method. This way Zend_Filter_Input
would always pass the real value to validators and filter, since it only
recurses the first dimension.

However this leaves the bug described above which makes some validators
falsely accept arrays as valid. This could be countered by making the new
methods _validateRule and _filterRule distinguish filters and validators,
which can handle arrays from those which cannot and then react in some way
in case of a problem. The distinction could for example be done by making
array-aware validators and filters implement a certain interface. I haven't
yet thought this through completely.

I hope this helps :).

Best regards

Christopher

-- 
View this message in context: 
http://www.nabble.com/Zend_Filter_Input-and-Arrays-tp17131495p18992326.html
Sent from the Zend Framework mailing list archive at Nabble.com.

Reply via email to