使用自定义HTML模板覆盖默认的Zend_Form元素渲染器?

时间:2013-04-23 21:15:15

标签: php zend-framework zend-form

我对Zend_Form默认呈现表单元素的方式不满意,并希望通过实例化我从Zend_Form继承的一个帮助程序类生成的所有表单来覆盖它,以便处理我在所有表面上执行的操作形式。

我想要做的更改看起来比使用装饰器合理/可能更复杂,所以我想使用自定义HTML模板来实现这一点,我可以将表单值插入自定义HTML代码段。

如何设置我的类呈现的所有HTML元素以使用HTML模板?我应该从模板中调用哪些属性/函数来获取Zend_Form默认渲染的东西?最后,我更愿意这样做而不必在我在代码中创建的每个输入元素上手动设置模板。

3 个答案:

答案 0 :(得分:1)

我已经完成了ZF1的编码工作,我发现渲染漂亮表单的最好方法就是使用Twitter Bootstrap。

检查以下链接,看看这对您来说是否也是一个令人满意的解决方案:

  1. how to use the twitter bootstrap framework in a zend framework 1 application?
  2. http://twitter.github.io/bootstrap/

答案 1 :(得分:1)

您可以使用自己的Zend_Form课程扩展默认的Custom_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')
        );
}

答案 2 :(得分:1)

我使用了一个自定义的视图,我将其归结为使用任意形式。

使用这种方法,我能够做到以下几点:

  • 在所需表单元素的标签后添加星号
  • 将输入和错误组合在一个div中,这样当我将标签浮动到左侧时,事物仍然排列
  • 为错误输入添加一个特殊类,以便我可以突出显示它们
  • 更改某些错误消息以包含元素的名称而不是“值”
  • 将文本注释与要在输入
  • 下显示的表单元素一起传递
  • 不用特殊元素包装标签和输入

如果没有意见稿,有些事情是不可能的,有些只是实施的痛苦。我认为这个解决方案对我来说会更加灵活。

在我的助手班'render()函数中:

$view = new Zend_View();
$view->setBasePath(SRC_ROOT . "/templates/forms");
$this->setDecorators(array(array('ViewScript', array('viewScript' => 'viewscript.php'))));

这是我的观点:

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