|
View:
New views
4 Messages
—
Rating Filter:
Alert me
|
|
|
Zend_Filter_Input and ArraysIs there a way to validate array inputs with Zend_Filter_Input? Both
single dimension name='array[0]' and multi-dimensional where one wants to validate multiple levels of keys like (name="array['input1']['input2']" value="input3") This info might be in the manual, but I can't seem to locate it. Kevin -- Kevin McArthur StormTide Digital Studios Inc. Author of the recently published book, "Pro PHP" http://www.stormtide.ca |
|
|
Re: Zend_Filter_Input and ArraysHej 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 |
|
|
Re: Zend_Filter_Input and ArraysI was a little to quick with my idea of wrapping $value into an array. Apparently it is not that easy :). But still overwriting _validateRule and _filterRule is the right way to solve it in a custom solution.
The permanently best thing however would be to write a patch that factors out array handling into a Strategy and propose it to the Zend Framework developers. Let's see if I find the time to do this. Christopher |
|
|
Re: Zend_Filter_Input and ArraysAnother, perhaps simpler, approach would be to create an array-aware
decorator that decorates a validator object with the ability to validate arrays. That might be the easiest way to let a scalar-only validator automatically validate each element of a given array. Regards, Bryce Lohr cvogt wrote: > I was a little to quick with my idea of wrapping $value into an array. > Apparently it is not that easy :). But still overwriting _validateRule and > _filterRule is the right way to solve it in a custom solution. > > The permanently best thing however would be to write a patch that factors > out array handling into a Strategy and propose it to the Zend Framework > developers. Let's see if I find the time to do this. > > Christopher > |
| Free Forum Powered by Nabble | Forum Help |