2009-04-22 2 views
6

私はそうのように作成したボタンの要素を持っている:Zend Framework Zend_Formデコレータ:<span>ボタン要素の内側?

$submit = new Zend_Form_Element_Button('submit'); 
$submit->setLabel('My Button'); 
$submit->setDecorators(array(
    'ViewHelper', 
    array('HtmlTag', array('tag' => 'li')) 
)); 
$submit->setAttrib('type', 'submit'); 

これは、次のHTMLを生成:

<li> 
    <label for="submit" class="optional">My Button</label> 
    <button name="submit" id="submit" type="submit">My Button</button> 
</li> 

私は<スパンで、ボタンの内部をラップしたいと思いますが>このように:

<button...><span>My Button</span></button> 

Zend_Formを使用してこれを行うには、どのような方法が最適ですか?

答えて

7

私は同じアプローチを使用してこれを達成しようとしましたが、最終的にはこれを達成できませんでした。

... 
$submit->setLabel('<span>My Button</span>'); 
... 

ただし、スパンはエスケープされますが、これを行うには最も簡単な方法があります。それは、lableデコレータのエスケープをオフにすることは完全に可能ですが、ラベルデコレータを追加すると、誤って出力をレンダリングし、例えば:

$decorator = array(
    array('ViewHelper'), 
    array('HtmlTag', array('tag' => 'li')), 
    array('Label', array('escape' => false)) 
); 

$submit = new Zend_Form_Element_Button('submit'); 
$submit->setLabel('<span>My Button</span>'); 
$submit->setDecorators($decorator); 
$submit->setAttrib('type', 'submit'); 

...レンダリング:

<label for="submit" class="optional"><span>My Button</span></label> 
<li> 
    <button name="submit" id="submit" type="submit">&lt;span&gt;My Button&lt;/span&gt</button> 
</li> 

...どの意味的に間違っている(簡単に修正可能である)ことを除けば、要素内のspanタグをエスケープしています。

あなたはどうしますか?

私は最良のアプローチ(これは私のメタアドバイスで、Zend_Formのレンダリングを厳密に制御することです)は、ViewScriptデコレータを使用することです。その後、_submitButton.phtmlで次のように定義し

$submit = new Zend_Form_Element_Button('submit'); 
$submit->setLabel('My Button'); 
$submit->setDecorators(array(array('ViewScript', array('viewScript' => '_submitButton.phtml')))); 
$submit->setAttrib('type', 'submit'); 

...:

<li> 
    <?= $this->formLabel($this->element->getName(), $this->element->getLabel()); ?> 
    <button 
    <?php 

    $attribs = $this->element->getAttribs(); 

    echo 
    ' name="' . $this->escape($this->element->getName()) . '"' . 
    ' id="' . $this->escape($this->element->getId()) . '"' . 
    ' type="' . $this->escape($attribs['type']) . '"'; 
    ?> 
    <?php 

    $value = $this->element->getValue(); 

    if(!empty($value)) 
    { 
     echo ' value="' . $this->escape($this->element->getValue()) . '"'; 
    } 
    ?> 
    > 
    <span> 
    <?= $this->escape($this->element->getLabel()); ?> 
    </span> 
    </button> 
</li> 

_submitButton.phtmlファイルは、ビュースクリプトのディレクトリにする必要があります(あなたが最良追加される可能性がありますあなたのフォームデコレータの具体的なものは$view->addScriptPath('/path/to/my/form/decorators')です)。

これは、あなたが探しているものを表示するはずです。私は仕事中に経験している柔軟性の問題のため、ViewScriptデコレータを見始めたばかりです。あなたは、私のスクリプトはそれほど柔軟ではなく、BNFではなく、要素オブジェクトに配置できるすべてのメンバーがあることに気づくでしょう。つまり、それは始まりであり、あなたの問題を解決します。

+0

非常に詳しい返信をありがとうございます。私はViewScriptデコレータを認識していますが、個々の要素に適用できるかどうかはわかりませんでした。 – leek

+0

これは非常に役に立ちました! – markus

0

ボタンにクラスを追加し、そのクラスをCSSで定義してスパンのように動作させます。私はあなたがスパンでそれをくっつける以上のことをしたいと確信しています。あなたのCSSに続いて

$submit->setAttrib('class', 'submitButton'); 

もう一つだけsetAttrib()の呼び出しを追加

.submitButton { 
    display:inline; 
    font-weight:bold; /* Or whatever you're wanting to do */ 
} 

を私はディスプレイを追加しました:このようインラインスパンのようなボタンの行為を行います。これはあなたが少なくとも始めるはずです。もちろん、CSSのidをボタンの基にすることもできます。display:inlineを使用してスパン動作を取得してください。

+0

私の目標は、実際に「ボタンスパン{表示:なし;}」を行うことですとしてみたボタンのテキストを非表示にする(しかし、私はそれはまだCSSない人々のためにそこになりたいです/ JSサポート)。 – leek

+0

Aaah。私はそれに何か他のものがあることを知っていた! – Kekoa

5

あなたはこれを行うことができます:

$this->addElement(new Zend_Form_Element_Button(
      'send', 
      array(
       'label' => '<span>registrieren</span>', 
       'class' => 'button-red', 
       'type' => 'submit', 
       'escape' => false, 
       'required' => false, 
       'ignore' => false, 
      ) 
)); 
+0

感謝します!!!!!! – wiLLiamcastrO

1

だけでラベルをエスケープすることは正常に動作します。

Zend_Form関数setElementDecorators()を使用し、スタイリングからいくつかの要素を除外することができます。 例3いくつかの要素のデコレータの設定Zend_Form Manual(注!を含む)に設定してください。ここでは、まだ混乱している人のために

は、サンプルコードは、テーブルとしてフォームのスタイルを設定し、二重のスパンに包まれているボタンを持つことです。

$フォームがZend_Formのある
//(...)putting some other elements in the form first 

//now add submit button 
$form  ->addElement('button', 'submit', array(
         'label'  => '<span><span>SUBMIT</span></span>', 
         'value'  => 'submit' 
      )); 

$form 
      //set decorators for non-button elements 
      ->setElementDecorators(array(
        'ViewHelper', 
        'Errors', 
        array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'element')), 
        array('Label', array('tag' => 'td')), 
        array(array('row' => 'HtmlTag'), array('tag' => 'tr'))), array('submit'), false) 
      //and then for button elements only 
      ->setElementDecorators(array(
        'ViewHelper', 
        'Errors', 
        array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'element')), 
        array(array('row' => 'HtmlTag'), array('tag' => 'tr'))), array('submit'), true) 
      //and finally for the form 
      ->setDecorators(array(
        'FormElements', 
        array('HtmlTag', array('tag' => 'table')), 
        'Form' 
      )); 

素子。お役に立てれば!

0

$element->addDecorator('Label', array('tag' => 'span', 'class' => 'aClass')) 
関連する問題