使用 Zend_Form_Decorator 生成定制的表单标识(Markup)
解析一个表单对象完全是可选的 -- 你根本不需要使用 每个条目(元素、显示组、子表单或表单对象自己)可能附有任意数量的装饰器,然而,只有一个给定类型的装饰器可能附加给每个条目。装饰器按照注册的顺序被调用。依靠装饰器,它替换传递给它的内容,或追加或预先准备内容。
对象状态通过传递给构造器或构造器的
在每个装饰器的 操作
要配置装饰器,传递选项数组或 标准选项包括:
装饰器接口指定和选项交互使用的方法,包括:
装饰器和各种
每个装饰器的 标准装饰器
定制装饰器如果你觉得你的解析很复杂或需要大量定制,可以考虑构造一个定制的装饰器。
装饰器只需要实现 <?php
interface Zend_Form_Decorator_Interface
{
public function __construct($options = null);
public function setElement($element);
public function getElement();
public function setOptions(array $options);
public function setConfig(Zend_Config $config);
public function setOption($key, $value);
public function getOption($key);
public function getOptions();
public function removeOption($key);
public function clearOptions();
public function render($content);
}
?>
要简化这个,可以继承
例如,你想减少装饰器的数量,构造一个在 HTML <?php
class My_Decorator_Composite extends Zend_Form_Decorator_Abstract
{
public function buildLabel()
{
$element = $this->getElement();
$label = $element->getLabel();
if ($translator = $element->getTranslator()) {
$label = $translator->translate($label);
}
if ($element->isRequired()) {
$label .= '*';
}
$label .= ':';
return $element->getView()->formLabel($element->getName(), $label);
}
public function buildInput()
{
$element = $this->getElement();
$helper = $element->helper;
return $element->getView()->$helper(
$element->getName(),
$element->getValue(),
$element->getAttribs(),
$element->options
);
}
public function buildErrors()
{
$element = $this->getElement();
$messages = $element->getMessages();
if (empty($messages)) {
return '';
}
return '<div class="errors">' . $element->getView()->formErrors($messages) . '</div>';
}
public function buildDescription()
{
$element = $this->getElement();
$desc = $element->getDescription();
if (empty($desc)) {
return '';
}
return '<div class="description">' . $desc . '</div>';
}
public function render($content)
{
$element = $this->getElement();
if (!$element instanceof Zend_Form_Element) {
return $content;
}
if (null === $element->getView()) {
return $content;
}
$separator = $this->getSeparator();
$placement = $this->getPlacement();
$label = $this->buildLabel();
$input = $this->buildInput();
$errors = $this->buildErrors();
$desc = $this->buildDescription();
$output = '<div class="form element">'
. $label
. $input
. $errors
. $desc
. '</div>'
switch ($placement) {
case (self::PREPEND):
return $output . $separator . $content;
case (self::APPEND):
default:
return $content . $separator . $output;
}
}
}
?>接着把它放到装饰器路径里: <?php
// for an element:
$element->addPrefixPath('My_Decorator', 'My/Decorator/', 'decorator');
// for all elements:
$form->addElementPrefixPath('My_Decorator', 'My/Decorator/', 'decorator');
?>然后指定这个装饰器为 'Composite' 并附加到一个元素: <?php
// Overwrite existing decorators with this single one:
$element->setDecorators(array('Composite'));
?>虽然这个例子示范了如何生成从若干元素的属性解析复杂输出装饰器,你也可以生成元素的一个单方面的装饰器,'Decorator' 和 'Label' 装饰器是这个练习的优秀示范。这样做让你混合和匹配装饰器来完成复杂输出 -- 并且也覆盖(override)装饰器的单个方面来符合你的需求。 例如,如果你想在校验元素时显示发生的错误,但不想显示每个独立的校验错误消息,可以生成 'Errors' 装饰器: <?php
class My_Decorator_Errors
{
public function render($content = '')
{
$output = '<div class="errors">The value you provided was invalid;
please try again</div>';
$placement = $this->getPlacement();
$separator = $this->getSeparator();
switch ($placement) {
case 'PREPEND':
return $output . $separator . $content;
case 'APPEND':
default:
return $content . $separator . $output;
}
}
}
?>
在这个特定的例子中,因为装饰器的最后一节,'Errors',匹配
|