Zend Framework

Warning on form validation

Details

  • Type: New Feature New Feature
  • Status: Resolved Resolved
  • Priority: Minor Minor
  • Resolution: Fixed
  • Affects Version/s: 1.6.0RC1, 1.6.0RC2
  • Fix Version/s: 1.7.0
  • Component/s: Zend_Form
  • Labels:
    None

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

Issue Links

Activity

Hide
Matthew Weier O'Phinney added a comment -

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)
Show
Matthew Weier O'Phinney added a comment - 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)
Hide
Jurrien Stutterheim added a comment -

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:

<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 />
<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 />
<form enctype="application/x-www-form-urlencoded" action="" method="post"><dl class="zend_form">
<dt>&nbsp;</dt>
<dd>
<select name="field1" id="field1">
    <option value="foo" label="FOO">FOO</option>
    <option value="bar" label="BAR">BAR</option>
</select>
<ul class="errors"><li>'baz' was not found in the haystack</li></ul></dd>
<dt>&nbsp;</dt>
<dd>
<input type="text" name="query1" id="query1" value="">
<ul class="errors"><li>Value is empty, but a non-empty value is required</li></ul></dd></dl></form>
<ul class="errors"><li></li><li></li></ul>
Show
Jurrien Stutterheim added a comment - 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:
<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 />
<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 />
<form enctype="application/x-www-form-urlencoded" action="" method="post"><dl class="zend_form">
<dt>&nbsp;</dt>
<dd>
<select name="field1" id="field1">
    <option value="foo" label="FOO">FOO</option>
    <option value="bar" label="BAR">BAR</option>
</select>
<ul class="errors"><li>'baz' was not found in the haystack</li></ul></dd>
<dt>&nbsp;</dt>
<dd>
<input type="text" name="query1" id="query1" value="">
<ul class="errors"><li>Value is empty, but a non-empty value is required</li></ul></dd></dl></form>
<ul class="errors"><li></li><li></li></ul>
Hide
Matthew Weier O'Phinney added a comment -

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.

Show
Matthew Weier O'Phinney added a comment - 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.
Hide
Matthew Weier O'Phinney added a comment -

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

Show
Matthew Weier O'Phinney added a comment - FormErrors decorator added in r12369 to trunk; will release with 1.7.0
Hide
Wil Sinclair added a comment -

Changing issues in preparation for the 1.7.0 release.

Show
Wil Sinclair added a comment - Changing issues in preparation for the 1.7.0 release.
Hide
ved prakash bishnoi added a comment -

Use this for remove haystack error

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

Show
ved prakash bishnoi added a comment - Use this for remove haystack error <code> $fieldElement1 = new Zend_Form_Element_Select('field1'); $fieldElement1->setRequired(true) ->setRegisterInArrayValidator(false); </code

People

Vote (0)
Watch (3)

Dates

  • Created:
    Updated:
    Resolved: