【问题标题】:Zend_Form overriding element defaults for custom layoutZend_Form 覆盖自定义布局的元素默认值
【发布时间】:2011-01-15 01:00:48
【问题描述】:

我目前正在尝试构建一个简单的自定义层,我将扩展它而不是 Zend_Form。例如,My_Form。

我希望我的所有表单看起来都一样,所以我在 My_Form 中进行了设置。这是目前的情况。

class My_Form extends Zend_Form
{
    protected $_elementDecorators = array(
        'ViewHelper',
        'Errors',
        array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'value_cell')),
        array('Label', array('tag' => 'td')),
        array(array('row' => 'HtmlTag'), array('tag' => 'tr')),
    );
}

我所有的表单都会扩展它。现在这正在工作,问题来自 $_elementDecorators 数组。我将标签包装在“td”中,标签装饰器将默认“id”应用于该“td”,但我也想向该“td”添加一个类。

有没有办法用这个数组来完成这个?如果没有,有没有更好的方法来做到这一点?或者如果是这样,有人可以向我描述一下这个数组是如何工作的吗?

想要的结果:

<tr>
    <td class='label_cell'>
        <label />
    </td>
    <td class='value_cell'>
        <input />
    </td>
</tr>

谢谢。

【问题讨论】:

    标签: php zend-framework zend-form decorator


    【解决方案1】:

    我找到了一个解决方案,虽然不确定它是最好的。

    在这里,我决定只创建一个自定义装饰器并加载它。

    /**
     * Overide the default, empty, array of element decorators.  
     * This allows us to apply the same look globally
     * 
     * @var array
     */
    protected $_elementDecorators = array(
        'ViewHelper',
        'Errors',
        array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'value_cell')),
        array('CustomLabel', array('tag' => 'td')),
        array(array('row' => 'HtmlTag'), array('tag' => 'tr'))
    );
    
    /**
     * Prefix paths to use when creating elements
     * @var array
     */
    protected $_elementPrefixPaths = array(
        'decorator' => array('My_Form_Decorator' => 'My/Form/Decorator/')
    );
    

    装饰者:

    class My_Form_Decorator_CustomLabel extends Zend_Form_Decorator_Label
    {
        public function render($content)
        {
            //...
            /**
             * Line 48 was added for the cutom class on the <td> that surrounds the label
             */
            if (null !== $tag) {
                require_once 'Zend/Form/Decorator/HtmlTag.php';
                $decorator = new Zend_Form_Decorator_HtmlTag();
                $decorator->setOptions(array('tag'   => $tag,
                                             'id'    => $this->getElement()->getName() . '-label',
                                             'class' => 'label_cell'));
    
                $label = $decorator->render($label);
            }
            //...
        }
    }
    

    虽然这工作得很好,但我仍然很好奇是否有更简单的方法来做到这一点。

    有什么想法吗?

    【讨论】:

      【解决方案2】:

      我在处理相同问题时最终使用的快速技巧:

      class My_Form extends Zend_Form
      {
          protected $_elementDecorators = array(
              'ViewHelper',
              'Errors',
              array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'value_cell')),
              array('Label', array('tag' => 'th')),
              array(array('row' => 'HtmlTag'), array('tag' => 'tr')),
          );
      }
      

      区别在于:`array('Label', array('tag' => 'th')),

      所以你的“标签”列有 TH 元素,而你的元素列有 TD 元素。

      然后,您可以随意设置它们的样式。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多