2.2.4

2013-10-04 8 views
7

Zend Frameworkの中の要素を形成するために、私は必要なHTMLクラスとidを追加する方法:2.2.4

<label for="text_field_username">User Name</lable> 
<input type="text" id="text_field_username" name="text_field_username" class="form-control" /> 

を私は入力のIDにリンクするラベルのためにしたいです。この方法で、ユーザーはラベルをクリックして入力を強調表示できます。チェックボックスでもっと便利です。 また、あまり重要ではない、私は入力フィールドにクラスを持っていたい。

echo $this->formRow($form->get('usr_name')); 

私はまた、部分的なレイアウトを使用しようとしました:私は試してみましたし、私の作品はありません何

echo $this->formElement($element); 

は、この質問を投稿する前に私はそれがない作品を行い

framework.zend.com/manual/2.2/en/modules/zend.form.view.helpers.html#formlabelこのドキュメントに出くわしました。それはforを追加しますが、何も指しません。 !?

答えて

9

フォームのレンダリングに役立つ部分を見ると、フォーム要素自体のプロパティは処理されません。これは、フォームクラスによって対処し、これは動作するはずですあなたは、フォームのinit()メソッド内で

だから、任意のフォーム要素にsetAttribute('class', 'class name')を使用することができます

フォーム要素(IEのTextElement)の集まりですされています

$element = $this->getElement('text_field_username'); 
$element->setAttribute('class', 'class name'); 
ただ

namespace Application\Form; 
use Zend\Form\Form; 
class NexForm extends Form 
{ 
public function __construct($name = null) 
{ 
    parent::__construct('Nex'); 
    $this->setAttribute('method', 'post'); 
    $this->setAttribute(
     'enctype', 
     'multipart/form- data' 
    ); 
    $this->add(array(
     'name' => 'default_widget', 

     'attributes' => array(
      'type' => 'text', 
      'id' => 'default_widget', 
      'class' => 'mtz-monthpicker-widgetcontainer', 
      'required' => 'required', 
     ), 
     'options' => array(
      'label' => 'Please choose the month of records you want to display:', 
     ), 
    )); 
} 
} 

とあなたのビューで:あなたはまた、このようなあなたの継承されたフォームヘルパークラスでこれを設定することができます

+0

私はそれが実際に 'setAttrib()' ZF2で –

+1

@MatthewRapati名前は 'のsetAttribute()' –

+0

@GuilhemSoulasあるの井戸それはより良い名前だかなり確信して! –

8

呼び出し:

 $nex=$this->app; //app is the form object we passed from controller to view 
    echo $this->formElement($nex->get('default_widget')); 
関連する問題