【问题标题】:How to add attributes to a label generated with Zend/Form in Zend framework 2如何在 Zend 框架 2 中为 Zend/Form 生成的标签添加属性
【发布时间】:2013-01-22 16:19:03
【问题描述】:

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

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

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

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

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

【问题讨论】:

    标签: php forms zend-framework2 label zend-form2


    【解决方案1】:

    请试试这个,我没有测试或使用过这个,但是从源代码来看它应该可以正常工作:

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

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

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

    【讨论】:

    • 您好 Sam,我已将元素更改如下: $this->add(array( 'name' => 'value', 'attributes' => array( 'type' => ' text', 'id' => 'value', 'autocomplete' => 'off', 'placeholder' => 'Cost', ), 'options' => array( 'label' => 'Cost', ), 'label_attributes' => 数组('class' => 'css', ) ));不过还是没有运气。但是,它被呈现为: 关于“for”的任何想法?
    • 我将不得不稍后检查这个,尽管框架指出这将起作用,但我不能保证任何事情:$element->setLabelAttributes(array('class' => 'control-label')); 也许尝试添加label_attributes 作为options 的子 -查看编辑后的版本
    • 在选项下! =) 所以把编辑后的版本留给全世界看看吧! ;-)
    • 始终在视图中执行此操作。您的视图不得泄漏到您的课程中。
    • @mike 虽然我大体上同意,但 Forms - 一般而言 - 是 Web 开发的极端案例。表格很烂。你不能做正确的形式。表格总是一团糟。在应该自动呈现的 Form-Object 中按默认分配 CSS-Class 非常好。
    【解决方案2】:

    这在 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')
      ),
    
    ));
    

    【讨论】:

      【解决方案3】:
      $element->setOptions(array('label_class' => array('class' => 'control-label')));
      

      产生这样的代码:

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

      我已经试过了。它在 Zend Framework One 中工作。

      注意如果你使用

      $element->setOptions(array('label_attributes' => array('class' => '控制标签')));

      由于某种原因,您会得到不良影响

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

      【讨论】:

      • I have tried this. It works in Zend Framework One. 很好,但这是一个 ZF2 问题,它在那里不起作用。
      【解决方案4】:

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

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

      受到 Damon 回答的启发。

      【讨论】:

        猜你喜欢
        • 2014-05-02
        • 1970-01-01
        • 1970-01-01
        • 2013-09-25
        • 2011-05-26
        • 2013-11-15
        • 1970-01-01
        • 1970-01-01
        • 2014-03-29
        相关资源
        最近更新 更多