ZF2 form element description

こ雲淡風輕ζ 提交于 2019-12-12 03:47:10

问题


in ZF1 form and form elements had a setDescription method that was outputted as <p>description here</p> in view .... ZF2 seems that doesn't have this method so my question is how can i add description to form elements ?

this is my view :

<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>

回答1:


Using ZF 2.1.5, one solution might be setOptions().

In the form definiton:

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

When rendering the form element:

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

Will give you access to:

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



回答2:


If you mean a label for an Element you can use the setLabel method when you create the form (in Controller).

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

Or if you use an array to create your form elements use this:

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

Here is a link:

enter link description here



来源:https://stackoverflow.com/questions/15378957/zf2-form-element-description

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!