How can I check if Zend_Form_Elements has no decorators set
I find that even if I just declare an element like
开发者_如何学JAVA$this->addElement('textarea', 'txt1');
I find that it already has decorators set
Zend_Debug::dump($this->getElement('txt1')->getDecorators());
http://pastebin.com/7Y24g62w
I want to test that I didn't set decorators using setDecorators() or using something like
$this->addElement('textarea', 'txt1', array(
'decorators' => array(...)
));
If I didn't set any decorators then apply default decorators, how can I do that. I want to apply default decorators per element basis, not using Zend_Form#setDisableLoadDefaultDecoraotrs()
There are two options, depending on how exactly you want to be sure a decorator hasn't been changed.
Decorator without option
If you just want to know if all default decorators are set, without thinking about the options of each decorator, you can use this option. Of course you can alter options of a default decorator and this method won't recognize this (but it is faster than an extensive check). Unfortunately the default decorators are hard-coded in the Zend_Form_Element
at Zend_Form_Element::loadDefaultDecorators()
so you need to copy that list. When in future releases the chain changes, you need to alter your code.
<?php
$default = array(
'Zend_Form_Decorator_ViewHelper',
'Zend_Form_Decorator_Errors',
'Zend_Form_Decorator_Description',
'Zend_Form_Decorator_HtmlTag',
'Zend_Form_Decorator_Label',
);
$decorators = array_keys($element->getDecorators());
if ($decorators === $default) {
// They are the same
}
Check decorators with all options
Here you create a copy of your element and at this copy you reload all default decorators. They get instantiated again so it takes a bit more resources, but all options of the decorators are checked as well.
$clone = clone $element;
$clone->clearDecorators()
->setDisableLoadDefaultDecorators(false)
->loadDefaultDecorators();
if ($clone === $element) {
// They are the same
}
精彩评论