Issues

ZF-4030: Warning on form validation

Issue Type: New Feature Created: 2008-08-22T01:51:41.000+0000 Last Updated: 2010-01-28T15:34:32.000+0000 Status: Resolved Fix version(s): - 1.7.0 (17/Nov/08)

Reporter: Jurrien Stutterheim (norm2782) Assignee: Matthew Weier O'Phinney (matthew) Tags: - Zend_Form

Related issues: - ZF-9002

Attachments:

Description

For each invalid field Zend_Form generates a warning:

Warning: htmlspecialchars() expects parameter 1 to be string, array given in /Library/WebServer/Library/Zend/View/Abstract.php on line 804

I've encountered this with both Select fields and normal Text fields, but other elements might be affected as well. A valid field does not generate this warning. The error messages do appear next to the elements.

Select code:

<pre class="highlight">
$fieldElement1 = new Zend_Form_Element_Select('field1');
$fieldElement1->setRequired(true)
              ->setAllowEmpty(false)
              ->setMultiOptions(array('foo' => 'FOO', 'bar' => 'BAR'));

Textfield code:

<pre class="highlight">
$queryElement1 = new Zend_Form_Element_Text('query1');
$queryElement1->setRequired(true)
              ->setAllowEmpty(false);

Edit:

The call to Zend_View_Abstract's escape() method originates from Zend_View_Helper_FormErrors line 75. A var_dump() on line 74 gives these results:

array 'isEmpty' => string 'Value is empty, but a non-empty value is required' (length=49)

array 'isEmpty' => string 'Value is empty, but a non-empty value is required' (length=49)

array 'field1' => array 'isEmpty' => string 'Value is empty, but a non-empty value is required' (length=49) 'query1' => array 'isEmpty' => string 'Value is empty, but a non-empty value is required' (length=49)

Edit2: It appears it's caused by adding the Errors decorator manually with $this->addDecorator('Errors');

Comments

Posted by Matthew Weier O'Phinney (matthew) on 2008-08-23T20:57:03.000+0000

I'm actually unsure exactly what the issue is. I've tried creating the elements you specify, passing invalid values to isValid(), and rendering, but with no errors. I've even tried specifying adding the Errors decorator manually -- I simply cannot reproduce any sort of warning.

I'm going to mark the issue as "cannot reproduce". Feel free to re-open if you can provide the following, succinctly: * Reproduce case * Expected result (this can even be "no warnings") * Actual result (this could be "warnings generated" -- please make sure you paste in the warnings)

Posted by Jurrien Stutterheim (norm2782) on 2008-08-24T03:53:48.000+0000

Both of these code snippets reproduce the bug:

<pre class="highlight">
require_once 'Zend/Form.php';
require_once 'Zend/Form/Element/Select.php';
require_once 'Zend/Form/Element/Text.php';
require_once 'Zend/View.php';

class FooForm extends Zend_Form
{
    public function init()
    {
        $fieldElement1 = new Zend_Form_Element_Select('field1');
        $fieldElement1->setRequired(true)
                      ->setAllowEmpty(false)
                      ->setMultiOptions(array('foo' => 'FOO', 'bar' => 'BAR'));
        
        $queryElement1 = new Zend_Form_Element_Text('query1');
        $queryElement1->setRequired(true)
                      ->setAllowEmpty(false);
                      
        $this->addElements(array($fieldElement1, $queryElement1));
    }
    
    public function loadDefaultDecorators()
    {
        parent::loadDefaultDecorators();
        $this->addDecorator('Errors');
    }
}

$form = new FooForm();
$form->isValid(array('field1' => 'baz', 'query1' => ''));

echo $form->render(new Zend_View());



<pre class="highlight">
require_once 'Zend/Form.php';
require_once 'Zend/Form/Element/Select.php';
require_once 'Zend/Form/Element/Text.php';
require_once 'Zend/View.php';

class FooForm extends Zend_Form
{
    public function init()
    {
        $fieldElement1 = new Zend_Form_Element_Select('field1');
        $fieldElement1->setRequired(true)
                      ->setAllowEmpty(false)
                      ->setMultiOptions(array('foo' => 'FOO', 'bar' => 'BAR'));
        
        $queryElement1 = new Zend_Form_Element_Text('query1');
        $queryElement1->setRequired(true)
                      ->setAllowEmpty(false);
                      
        $this->addElements(array($fieldElement1, $queryElement1));
        
        $this->addDecorator('FormElements')
             ->addDecorator('HtmlTag', array('tag' => 'dl', 'class' => 'zend_form'))
             ->addDecorator('Form')
             ->addDecorator('Errors');
    }
}

$form = new FooForm();
$form->isValid(array('field1' => 'baz', 'query1' => ''));

echo $form->render(new Zend_View());

Output:

<pre class="highlight">
<br></br><b>Warning</b>:  htmlspecialchars() expects parameter 1 to be string, array given in <b>/Library/WebServer/Library/Zend/View/Abstract.php</b> on line <b>804</b><br></br><br></br><b>Warning</b>:  htmlspecialchars() expects parameter 1 to be string, array given in <b>/Library/WebServer/Library/Zend/View/Abstract.php</b> on line <b>804</b><br></br>

 

FOOBAR
'baz' was not found in the haystack
 


Value is empty, but a non-empty value is required

``

Posted by Matthew Weier O'Phinney (matthew) on 2008-08-24T14:31:46.000+0000

The 'Errors' decorator is currently not intended for use at the form level, only with individual elements. I'm going to change this to a feature request, as we probably need a 'FormErrors' decorator.

Posted by Matthew Weier O'Phinney (matthew) on 2008-11-07T08:14:08.000+0000

FormErrors decorator added in r12369 to trunk; will release with 1.7.0

Posted by Wil Sinclair (wil) on 2008-11-13T14:10:09.000+0000

Changing issues in preparation for the 1.7.0 release.

Posted by ved prakash bishnoi (vpgodara) on 2010-01-20T03:31:52.000+0000

Use this for remove haystack error

$fieldElement1 = new Zend_Form_Element_Select('field1');
$fieldElement1->setRequired(true)
->setRegisterInArrayValidator(false);
</code

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