Skip to content

Custom error messages added to elements don't trigger error class for parent control-group #23

Closed
k-risc opened this Issue Apr 24, 2012 · 5 comments

3 participants

@k-risc
k-risc commented Apr 24, 2012

Hello,

in a controller script I use the follwoing code to add an error message to a form's element:

$formName->getElement('elementName')->addError('Error message'); 

The error message is shown next to the element, but the whole div.control-group does not have class "error" added to it.

Es far as I can see this is for the following reasons:

  1. Performing addError on an element marks the element as invalid, but you would have to call the parent's form isValid() again in order to set the whole form invalid.
  2. In the class EasyBib_Form in function "buildBootstrapErrorDecorators" only the forms getErrors() are handled. But a custom error message added with element->addError() will not be returned by this.

Here is my current workaround:

Step 1

After I call $formName->getElement('elementName')->addError('Error message'), I call $form->buildBootstrapErrorDecorators();

This funtion is otherwise only called in EasyBib_Form's function "isValid", which calls "isValid()" of the parent class. But that seems to reset all error messages (as far as I can see), and only validates against the validators. The form would be valid again and "buildBootstrapErrorDecorators()" would not be performed.

Step2

In "buildBootstrapErrorDecorators" I add:

foreach ($this->getElements() AS $element) 
    if (count($element->getErrorMessages()) > 0) {
        $htmlTagDecorator = $element->getDecorator('HtmlTag');
        if (empty($htmlTagDecorator)) {
            continue;
        }
        $class = $htmlTagDecorator->getOption('class');
        $htmlTagDecorator->setOption('class', $class . ' error');
    }

I hope it is quite understandable what my problem is, and hopefully my (or a better) solution for this will be implemented. If I just missed something and am totally wrong, and if what I want could be achieved otherwise, I apologize and kinldy ask for a hint into the right direction.

Best regards

Christian

@Mischosch

hey k-risc,

thank you very much for the great problem analysis :)
Liked it very much to read and follow it!

Yeah, Zend_Form and Error handling is at some points really strange and behaves not like expected.
Don't know, why adding an error doesn't result in a form error.
I remember checking the Zend_Form code and some blog posts about it - didnt find any clear solution how to add an error manually.

So, no, I have no better solution than you have right now.
We could integrate your solution by pull-request!

Cheers

Mischosch

Michael

@till
EasyBib member
till commented Sep 12, 2012

I ran into this issue as well and had to manually handle this.

@Mischosch

we should fix it like this:

public function buildBootstrapErrorDecorators() {
    $errors = array_merge($this->getErrors(), $this->getMessages());
    foreach ($errors AS $key=>$errors) {
        ...
    }
}
@till
EasyBib member
till commented Sep 12, 2012

Do those methods always return arrays? =) The more I look into Zend_Form, the more scared I am. But otherwise, this seems like it does the trick?

@Mischosch

yes, both return arrays (as long as I can trust the doc comments there)

@Mischosch Mischosch closed this in 9920bf3 Sep 12, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.