php 如何向 Zend 框架 2 中的 Zend/Form 生成的标签添加属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14746497/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:54:03  来源:igfitidea点击:

How to add attributes to a label generated with Zend/Form in Zend framework 2

phpformszend-framework2labelzend-form2

提问by El Dorado

I'm adding forms to my page using Zend/Form.

我正在使用 Zend/Form 向我的页面添加表单。

I'm adding elements by defining them as follows:

我通过如下定义来添加元素:

    $this->add(array(
            'name' => 'value',
            'attributes' => array(
                    'type'  => 'text',
                    'id' => 'value',
                    'autocomplete' => 'off',
                    'placeholder' => 'Cost',
            ),
            'options' => array(
                    'label' => 'Cost',
            ),
    ));

As you can see there is a 'label' => 'cost' node, this generated a label to go with the input element.

正如您所看到的,有一个 'label' => 'cost' 节点,这会生成一个标签以与输入元素一起使用。

How do I add classes, attributes to this label ?

如何向该标签添加类、属性?

回答by Sam

Please try this, i haven't tested or used this, but going by the source it should function properly:

请试试这个,我还没有测试或使用过这个,但从源头来看它应该可以正常运行:

$this->add(array(
    'name'       => 'value',
    'attributes' => array(),
    'options'    => array(
        'label_attributes' => array(
            'class'  => 'mycss classes'
        ),
        // more options
    ),        
));

If this does not function, please leave me a comment. If it won't function, it is not possible using this approach, since the FormLabelrestricts the validAttributesquite a bit:

如果这不起作用,请给我留言。如果它不起作用,则无法使用这种方法,因为它FormLabel限制了validAttributes很多:

protected $validTagAttributes = array(
    'for'  => true,
    'form' => true,
);

回答by speedy32

This works well in Zend Framework 2.3 :

这在 Zend Framework 2.3 中运行良好:

$this->add(array(
  'name' => 'userName',
  'attributes' => array(
      'type'  => 'text',
      'class' => 'form-control',
      'placeholder' =>'Username',
  ),
  'options' => array(
      'label' => 'Username',
      'label_attributes' => array('class' => 'control-label')
  ),

));

回答by Dennis

For programmatic approach on ZF2+ try this:

对于 ZF2+ 上的编程方法,试试这个:

$element->setOptions(array(
    'label_attributes' => array(
        'style' => 'color:gray;'
    )
));

Inspired by Damon's answer.

灵感来自达蒙的回答。

回答by Damon Hogan

$element->setOptions(array('label_class' => array('class' => 'control-label')));

Produces code like this:

产生这样的代码:

<label class="control-label">
  <input type="radio" name="option1" id="option1" value="1">
  Option 1
</label>
<label class="control-label">
  <input type="radio" name="option2" id="option2" value="2">
  Option 2
</label>

I have tried this. It works in Zend Framework One.

我试过这个。它适用于 Zend Framework One。

Note if you use

请注意,如果您使用

$element->setOptions(array('label_attributes' => array('class' => 'control-label')));

$element->setOptions(array('label_attributes' => array('class' => 'control-label')));

you get the undesirable effect for some reason of

由于某种原因,你得到了不良影响

<label attributes="control-label">
  <input type="radio" name="option1" id="option1" value="1">
  Option 1
</label>