ZF-4030: Warning on form validation

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:


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

Textfield code:


$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

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)

Both of these code snippets reproduce the bug:


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());

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:



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

Warning: htmlspecialchars() expects parameter 1 to be string, array given in /Library/WebServer/Library/Zend/View/Abstract.php on line 804
  FOOBAR'baz' was not found in the haystack   Value is empty, but a non-empty value is required

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.

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

Changing issues in preparation for the 1.7.0 release.

Use this for remove haystack error

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