Issues

ZF-11819: Zend_Filter_Input raises PHP Notice when validator metacommand value is empty array

Issue Type: Bug Created: 2011-10-13T14:47:06.000+0000 Last Updated: 2011-10-14T15:12:28.000+0000 Status: Resolved Fix version(s): - 1.11.12 (22/Jun/12)

Reporter: Arron Woods (idealarron) Assignee: Adam Lundrigan (adamlundrigan) Tags: - Zend_Filter_Input

  • zf-caretaker-adamlundrigan

Related issues: - ZF-9289

Attachments:

Description

Upgrading from 1.11.10 to 1.11.11 I experienced some problems with Zend_Filter_Input. Seems to be a backwards compatibility issue.

The following code:

<pre class="highlight">
$filters = array(
    'html' => 'StringTrim',
);

$validators = array(
    'html' => array('NotEmpty', 'presence' => 'required'),
    'perms' => array('default' => array()),
);

$validate = new Zend_Filter_Input($filters, $validators, array('html' => '<br></br>'));
$validate->isValid();

produces the following error in 1.11.11:

{quote} PHP Notice: Undefined index: Stack trace:

0 /usr/share/php/Zend/Filter/Input.php(854): My_Error_Catch->error(8, 'Undefined index...', '/usr/share/php/...', 854, Array)

1 /usr/share/php/Zend/Filter/Input.php(786): Zend_Filter_Input->_validate()

2 /usr/share/php/Zend/Filter/Input.php(442): Zend_Filter_Input->_process()

3 .......(48): Zend_Filter_Input->isValid()

{quote}

Seems like setting the default to "array()" is causing the notice, is there anything wrong with setting the default to an array?

If ZF doesn't have to be backwards compatible I guess this is a non-issue?

Comments

Posted by Adam Lundrigan (adamlundrigan) on 2011-10-13T17:28:03.000+0000

It would appear that the fix for ZF-9289 (this commit) is the source of this notice.

I've also noticed that the format you use to define $validators is not one of the approved formats from the manual (see here); in your example the 'perms' key is an array but the first element is not the name of a validator class.

As 'perms' in your example doesn't represent a valid validator entry, it should be silently ignored as was the case pre-ZF-9289. I suggest a fix like this, which short-circuits the code addition of ZF-9289 if $rule[$classkey] is undefined:

<pre class="highlight">
Index: library/Zend/Filter/Input.php
===================================================================
--- library/Zend/Filter/Input.php       (revision 24503)
+++ library/Zend/Filter/Input.php       (working copy)
@@ -851,11 +851,13 @@
                     if (is_array($rule)) {
                         $keys      = array_keys($rule);
                         $classKey  = array_shift($keys);
-                        $ruleClass = $rule[$classKey];
-                        if ($ruleClass === 'NotEmpty') {
-                            $foundNotEmptyValidator = true;
-                            // field may not be empty, we are ready
-                            break 1;
+                        if ( isset($rule[classKey]) ) {
+                            $ruleClass = $rule[$classKey];
+                            if ($ruleClass === 'NotEmpty') {
+                                $foundNotEmptyValidator = true;
+                                // field may not be empty, we are ready
+                                break 1;
+                            }
                         }
                     }

Posted by Arron Woods (idealarron) on 2011-10-13T17:49:29.000+0000

{quote} As 'perms' in your example doesn't represent a valid validator entry, it should be silently ignored as was the case pre-ZF-9289. I suggest a fix like this, which short-circuits the code addition of ZF-9289 if $rule[$classkey] is undefined: {quote}

Agreed, I checked the manual and it was definitely an incorrect definition for 'perms'. I've updated this in my app already.

The notice is thrown even if I define a validator for perms though, for example:

<pre class="highlight">
$validators = array(
    'html' => array('NotEmpty', 'presence' => 'required'),
    'perms' => array('Int', 'default' => array()),
);

Only if I set 'default' to a string/null/boolean does the notice disappear:

<pre class="highlight">
$validators = array(
    'html' => array('NotEmpty', 'presence' => 'required'),
    'perms' => array('Int', 'default' => 'foo'),
);

Posted by Adam Lundrigan (adamlundrigan) on 2011-10-14T14:29:45.000+0000

That is due to fact that the code added in ZF-9289 (spec. Line 851) only operates when $rule (the value part of any key-value pair) is an array, but fails to check whether that array contains the requested key before trying to use it, so when passed an empty array it emits a notice. The fix I suggested above should take care of that problem.

Posted by Adam Lundrigan (adamlundrigan) on 2011-10-14T14:57:08.000+0000

Fixed in trunk r24504 r24506. Merged to release-1.11 in r24505 r24507.

ZF2 pull request: https://github.com/zendframework/zf2/pull/500

Have you found an issue?

See the Overview section for more details.

Copyright

© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts