我对Zend_Form默认呈现表单元素的方式不满意,并希望通过实例化我从Zend_Form继承的helper类来覆盖它,以处理我在所有表单上做的一些事情。
我想做的更改看起来比装饰器合理/可能的更复杂,所以我想使用一个自定义HTML模板来完成这一点,在那里我可以将表单值插入到自定义HTML片段中。
如何设置类呈现的所有HTML元素使用HTML模板?我应该从模板调用哪些属性/函数来获得默认情况下Zend_Form渲染的东西?最后,我更愿意这样做,而不必在代码中创建的每个输入元素上手动设置模板。
您可以用自己的Custom_Form
类扩展默认的Zend_Form
类。在init()
方法中覆盖默认的元素装饰器。下面是我的代码片段:
//class Custom_Form extends Zend_Form
public function init()
{
$this->setElementDecorators(
array(array('ViewScript', array('viewScript' => '/controller_name/forms/fields/input-text.phtml'))),
array('email', 'firstname', 'lastname')
);
}
我已经用ZF1完成了我的编码,我发现渲染漂亮表单的最好方法是使用Twitter Bootstrap。
查看以下链接,看看这是否也是您满意的解决方案:
- 如何在zend框架1应用程序中使用twitter引导框架?
- http://twitter.github.io/bootstrap/
我最终使用了一个自定义的viewscript,我将其一般化以处理任意表单。
使用这种方法,我能够做以下事情:
- 在所需表单元素的标签后添加星号
- 将输入和错误组合在一个div中,这样当我将标签浮动到左边时,东西仍然对齐
- 为错误输入添加一个特殊的类,这样我就可以突出显示它们
- 更改某些错误消息以包含元素的名称而不是"Value" 在输入下传递一个文本注释和要显示的表单元素
- 不换行标签和特殊元素
如果没有viewscript,其中一些事情是不可能的,有些实现起来很痛苦。我认为这个解决方案对我以后的工作来说会灵活得多。
在我的助手类的render()
函数:
$view = new Zend_View();
$view->setBasePath(SRC_ROOT . "/templates/forms");
$this->setDecorators(array(array('ViewScript', array('viewScript' => 'viewscript.php'))));
这是我的viewscript:
<link rel="stylesheet" type="text/css" href="/styles.css" />
<form id="<?php echo $this->element->html_id ?>" class="<?php echo $this->element->html_class ?>" enctype="application/x-www-form-urlencoded" action="" method="post">
<?php foreach($this->element as $element) { ?>
<?php
$decorators = $element->getDecorators();
if(isset($decorators["Zend_Form_Decorator_Label"])) {
$label = $element->getLabel();
} else {
$label = "";
}
if($element->isRequired() === true) {
$label .= " *";
}
?>
<label class="label" for="<?php echo $element->getName(); ?>"><?php echo $label; ?></label>
<div class="formInput">
<?php
// Add the error class to make the form inputs highlight in red
if($element->hasErrors()) {
$attribs = $element->getAttribs();
if(!isset($attribs["class"])) {
$attribs["class"] = "";
}
$attribs["class"] .= " inputError";
$element->setAttribs($attribs);
}
// Print the input using Zend_Form's own mechanisms
$element->setDecorators(array('ViewHelper')); // Removes all decorators (labels, etc.)
$v = new Zend_View();
$element->setView($v);
echo $element->render();
if(isset($element->note)) {
echo "<p>{$element->note}</p>";
}
// Print the error messages
if($element->hasErrors()) {
$errors = $element->getMessages();
?>
<ul class="errors <?php echo sizeof($errors) == 1 ? "noDecorations" : "" ?>">
<?php
foreach($errors as $error => $message) {
// Custom error messages
if($error === "isEmpty") {
$message = $element->getLabel() . " cannot be empty";
} ?>
<li><?php echo $message ?></li>
<?php } ?>
</ul>
<?php } ?>
</div>
<div style="float: clear;"></div>
<?php } ?>
</form>