Re: [fw-general] Zend_Filter_Input and Arrays
Glad to help! Incidentally, I already have a few implementations of such validator decorators in the Laboratory here: http://framework.zend.com/svn/framework/laboratory/library/Zend/Validate/ See the file Array.php, which does exactly what I had mentioned. Regards, Bryce Lohr cvogt wrote: Hej Bryce, Another, 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. you are right, decorators seem to be the way to go. I implemented it this way and added it as a feature request including code to the issue tracker. http://framework.zend.com/issues/browse/ZF-4354 Best Regards Christopher
Re: [fw-general] Zend_Filter_Input and Arrays
Hej Bryce, > Another, 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. you are right, decorators seem to be the way to go. I implemented it this way and added it as a feature request including code to the issue tracker. http://framework.zend.com/issues/browse/ZF-4354 Best Regards Christopher -- View this message in context: http://www.nabble.com/Zend_Filter_Input-and-Arrays-tp17131495p19619388.html Sent from the Zend Framework mailing list archive at Nabble.com.
Re: [fw-general] Zend_Filter_Input and Arrays
Another, 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
Re: [fw-general] Zend_Filter_Input and Arrays
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 -- View this message in context: http://www.nabble.com/Zend_Filter_Input-and-Arrays-tp17131495p18992433.html Sent from the Zend Framework mailing list archive at Nabble.com.
Re: [fw-general] Zend_Filter_Input and Arrays
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.