为什么Zend_Form产生<dt id =“label-id”> </dt> <dd id =“elem-id”>?</dd>

时间:2010-07-14 13:48:33

标签: php zend-framework forms zend-form

为什么Zend_Form会产生<dl> <dt id="label-id"> <dd id="elem-id"> </dl>? (我明白它的产品是默认的装饰器,但为什么以及它们用于什么?)

由于

1 个答案:

答案 0 :(得分:4)

装饰器是一种包装表单元素的方法。

在Zend / Form.php中,您可以看到默认配置:

/**
 * Load the default decorators
 *
 * @return void
 */
public function loadDefaultDecorators()
{
    if ($this->loadDefaultDecoratorsIsDisabled()) {
        return $this;
    }

    $decorators = $this->getDecorators();
    if (empty($decorators)) {
        $this->addDecorator('FormElements')
             ->addDecorator('HtmlTag', array('tag' => 'dl', 'class' => 'zend_form'))
             ->addDecorator('Form');
    }
    return $this;
}

Plus Zend / FormElements.php使用自己的装饰器:

/**
 * Load default decorators
 *
 * @return Zend_Form_Element
 */
public function loadDefaultDecorators()
{
    if ($this->loadDefaultDecoratorsIsDisabled()) {
        return $this;
    }

    $decorators = $this->getDecorators();
    if (empty($decorators)) {
        $this->addDecorator('ViewHelper')
            ->addDecorator('Errors')
            ->addDecorator('Description', array('tag' => 'p', 'class' => 'description'))
            ->addDecorator('HtmlTag', array('tag' => 'dd',
                                            'id'  => $this->getName() . '-element'))
            ->addDecorator('Label', array('tag' => 'dt'));
    }
    return $this;
}

您可以覆盖每个元素或整个表单的默认装饰器:

$element->setDecorators(array(
    'ViewHelper',
    'Description',
    'Errors',
    array(array('elementDiv' => 'HtmlTag'), array('tag' => 'div')),
    array(array('td' => 'HtmlTag'), array('tag' => 'td')),
    array('Label', array('tag' => 'td')),
));

有一个非常简单的播客解释装饰器如何工作:

http://feeds.feedburner.com/ZendScreencastsVideoTutorialsAboutTheZendPhpFrameworkForiPhone

查看名为的视频: Zend_Form装饰器说明