views:

40

answers:

1

I'm trying to display all form errors before the form using a ViewScript. Here is the code that I'm currently trying to use within my ViewScript:

<div class="errors">
<?php echo $this->formErrors($this->element->getMessages()); ?>
</div>

This call gives me an error message:

Warning: htmlspecialchars() expects parameter 1 to be string, array given

I've seen this same code suggested other places but its not working for me. If I print out $this->element->getMessages() I do see the error messages as the following:

Array ( [myField] => Array ( [isEmpty] => Value is required and can't be empty ) )

Any ideas?

+1  A: 

The getMessages() returns an array of form element names as keys which each contain an array of errors for that element. So basically instead of handing the formErrors view helper:

Array ( [isEmpty] => Value is required and can't be empty )

You are handing it:

Array ( [myField] => Array ( [isEmpty] => Value is required and can't be empty ) )

You would want to do something like this instead:

$arrMessages = $this->myForm->getMessages();
foreach($arrMessages as $field => $arrErrors) {
    echo sprintf(
        '<ul><li>%s</li>%s</ul>',
        $this->myForm->getElement($field)->getLabel(),
        $this->formErrors($arrErrors)

    );
}
Mark
I ended up doing something similar but I like your solution better. Odd that there is no function available to handle all form errors together like this, only to handle them for one field at a time.
Jeremy Hicks