2013-03-13 194 views
0

在ZF1表单的元素有一个setDescription方法是输出作为鉴于<p>description here</p> .... ZF2似乎没有这个方法 所以我的问题是,我怎样才能添加描述成元素?ZF2表单元素描述

这是我的看法:

<div> 
    <? 
    $form = $this->form; 
    $form->prepare(); 
    echo $this->form()->openTag($form); 

    foreach ($form->getElements() as $el) { 
     ?> 
     <div class="form_element"> 
      <?=$this->formRow($el);?> 
     </div> 
     <? 
    } 
    echo $this->partial('system/form/buttons_form_part.phtml', array('form' => $form)); 
    echo $this->form()->closeTag(); 
    ?> 
</div> 

回答

5

使用ZF 2.1.5,一个解决方案可能是setOptions()

在形式definiton:

$file = new Element\File('file'); 
$file->setLabel('Photo (.jpg, .gif, .png)'); 
$file->setOptions(array('description' => 'test description')); 
… 

当渲染表单元素:

$element = $form->get(…);  
var_dump($element->getOptions()); 

会给你访问:

array(1) { ["description"]=> string(16) "test description" } 
+0

太好了!有用... – 2013-06-11 05:34:01

0

如果你的意思是一个元素在创建表(控制器)可以使用setLabel方法的标签。

$name = new Element('name'); 
$name->setLabel('Your name'); 

或者,如果你使用一个数组来创建表单元素使用:

array(
     'spec' => array(
      'name' => 'name', 
      'options' => array(
       'label' => 'Your name', 
      ), 
      'attributes' => array(
       'type' => 'text' 
      ), 
     ) 
    ), 

这里是一个链接:

enter link description here