Skip to content
This repository has been archived by the owner on Nov 12, 2021. It is now read-only.

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 · Fixed by #30
Closed

Comments

@k-risc
Copy link

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
Copy link
Contributor

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
Copy link
Contributor

till commented Sep 12, 2012

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

@Mischosch
Copy link
Contributor

we should fix it like this:

public function buildBootstrapErrorDecorators() {
    $errors = array_merge($this->getErrors(), $this->getMessages());
    foreach ($errors AS $key=>$errors) {
        ...
    }
}

@till
Copy link
Contributor

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
Copy link
Contributor

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants