目的のサンプル出力に基づいて、特にラベルを適用していないため、 on hidden 要素を含めたい理由が<dt>
わかりません。それらは不要なマークアップになります。ラベルが不要であるという仮定に従って、次の方法で目的の効果を得ることができます。
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>
メソッドのforeach
ループは、フォームが作成されるときに、非表示の各フォーム要素にloadDefaultDecorators()
繰り返し適用されます。$this->_hiddenElementDecorator
非表示の要素デコレータを複数のフォームに適用する場合は、$_hiddenElementDecorator
変数とloadDefaultDecorators()
メソッドを含む親クラスを作成するだけです。
ただし、サンプル出力で説明されているようにラベル要素 ( <dt>
) を含め、「hidden」クラスを適用することに心当たりがある場合は、次のようになります。
<dt class="hidden"> </dt>
<dd class="hidden">
<input type="hidden" name="hiddenElement0" value="" id="hiddenElement0" />
</dd>
Zend_Form_Decorator_Label
...クラスを拡張して render() メソッドをオーバーライドする必要があります。if (null !== $tag)
ブロック内のコメントを見てください。
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;
}
}
}
最後に、新しいデコレータを非表示のフォーム要素すべてに適用するには、要素プレフィックス パス ポイントをデコレータに追加し、ラベル デコレータをクラスの配列に再度追加する必要があります。$_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();
}
}
パイのように簡単ですよね?