원하는 샘플 출력을 바탕으로 내가 불분명 해요 당신이 어떤 라벨을 적용하지 않을 특히 때문에, 불필요한 마크 업되고 결국.
class TestForm extends Zend_Form
{
protected $_hiddenElementDecorator = array(
'ViewHelper',
array('HtmlTag', array('tag' => 'dd', 'class' => 'hidden')),
);
public function init()
{
$this->addElement('hidden', 'hiddenElement0');
$element = new Zend_Form_Element_Hidden('hiddenElement1');
$this->addElement($element);
}
public function loadDefaultDecorators()
{
foreach ($this->getElements() as $element) {
if ($element->getType() === "Zend_Form_Element_Hidden") {
$element->setDecorators($this->_hiddenElementDecorator);
}
}
parent::loadDefaultDecorators();
}
}
위의 요소를 모두 각각의 id와 같은 결과가 발생합니다 : 라벨 이 필요 인 가정에 따라, 다음에 원하는 효과를 얻을 수 있습니다. 예 : 형태가 내장되는 경우가
<dd class="hidden">
<input type="hidden" name="hiddenElement0" value="" id="hiddenElement0" />
</dd>
loadDefaultDecorators()
방법에 foreach
루프가 반복마다 숨겨진 형태 $this->_hiddenElementDecorator
소자에인가한다. 숨겨진 요소 장식자를 여러 양식에 적용하려면 $_hiddenElementDecorator
변수와 loadDefaultDecorators()
메소드가 포함 된 상위 클래스를 만듭니다. 그러나
, 당신의 마음이 당신의 샘플 출력에 설명 된대로 label 요소 (<dt>
)를 포함하고 끝낼 수 있도록 '숨겨진'클래스를 적용하는 설정 한 경우 :
<dt class="hidden"> </dt>
<dd class="hidden">
<input type="hidden" name="hiddenElement0" value="" id="hiddenElement0" />
</dd>
. .. Zend_Form_Decorator_Label
클래스를 확장하고 render() 메서드를 재정의해야합니다.당신이 당신의 장식과 재에 요소 접두사 경로 점을 추가해야합니다 숨겨진 양식의 모든 요소에 새로운 장식을 적용, 마지막으로
class My_Form_Decorator_Label extends Zend_Form_Decorator_Label
{
public function render($content)
{
$element = $this->getElement();
$view = $element->getView();
if (null === $view) {
return $content;
}
$label = $this->getLabel();
$separator = $this->getSeparator();
$placement = $this->getPlacement();
$tag = $this->getTag();
$id = $this->getId();
$class = $this->getClass();
$options = $this->getOptions();
if (empty($label) && empty($tag)) {
return $content;
}
if (!empty($label)) {
$options['class'] = $class;
$label = $view->formLabel($element->getFullyQualifiedName(), trim($label), $options);
} else {
$label = ' ';
}
if (null !== $tag) {
require_once 'Zend/Form/Decorator/HtmlTag.php';
$decorator = new Zend_Form_Decorator_HtmlTag();
// Add 'class' => 'hidden' to the <dt> tag decorator options.
$decorator->setOptions(array('tag' => $tag, 'class' => 'hidden'));
$label = $decorator->render($label);
}
switch ($placement) {
case self::APPEND:
return $content . $separator . $label;
case self::PREPEND:
return $label . $separator . $content;
}
}
}
다음 if (null !== $tag)
블록에서 주석을 살펴보십시오 추가 라벨 장식을 $_hiddenElementDecorator
배열에 TestForm
클래스 :
class TestForm extends Zend_Form
{
protected $_hiddenElementDecorator = array(
'ViewHelper',
array('HtmlTag', array('tag' => 'dd', 'class' => 'hidden')),
// Add back the label element.
array('Label', array('tag' => 'dt', 'class' => 'hidden')),
);
public function init()
{
$this->addElement('hidden', 'hiddenElement0');
$element = new Zend_Form_Element_Hidden('hiddenElement1');
$this->addElement($element);
}
public function loadDefaultDecorators()
{
foreach ($this->getElements() as $element) {
if ($element->getType() === "Zend_Form_Element_Hidden") {
$element->setDecorators($this->_hiddenElementDecorator);
}
}
// Add a decorator prefix path pointing to our new nifty decorator.
$this->addElementPrefixPath('My_Form_Decorator', '/path/to/Decorator', Zend_Form_Element::DECORATOR);
parent::loadDefaultDecorators();
}
}
쉬운 파이로, 아니?
당신은 신사입니다! 경의를 표하며 축하드립니다. –
D ' 오. 그것은 나를 위해 작동하지 않는다 : ($ this-> getElements() 빈 배열을 반환하는 : - | 폼에 확실히 요소가 있습니다! –