【问题标题】:Override default Zend_Form element renderer with custom HTML template?用自定义 HTML 模板覆盖默认 Zend_Form 元素渲染器?
【发布时间】:2013-04-23 21:15:15
【问题描述】:

我对 Zend_Form 默认呈现表单元素的方式不满意,并希望为通过实例化从 Zend_Form 继承的帮助器类生成的所有表单覆盖它,以处理我对所有我所做的一些事情形式。

我想要进行的更改看起来比使用装饰器的合理/可能更复杂,因此我想使用自定义 HTML 模板来完成此操作,在该模板中我可以将表单值插入自定义 HTML sn-p。

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

【问题讨论】:

    标签: php zend-framework zend-form


    【解决方案1】:

    您可以使用自己的 Custom_Form 类扩展默认的 Zend_Form 类。在init() 方法中覆盖默认的元素装饰器。这是我的代码sn-p:

    //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')
            );
    }
    

    【讨论】:

    • 是否有任何文档说明我应该如何编写适用于单个通用表单元素的视图脚本?我所看到的只是如何为整个表单编写视图脚本,这不是我所追求的。我喜欢我可以将元素对象传递给 Zend_Form,它无需我为我需要的每个表单编写 HTML 即可呈现内容,我只希望它以 不同方式 呈现内容。
    • 搜索 setElementDecorators()
    【解决方案2】:

    我已经完成了我使用 ZF1 编码的分享,我发现呈现漂亮表单的最佳方法是使用 Twitter Bootstrap。

    查看以下链接,看看这是否也是您满意的解决方案:

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

    【讨论】:

      【解决方案3】:

      我最终使用了一个自定义视图脚本,我将其泛化为使用任意表单。

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

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

      如果没有 viewscript,其中一些事情是不可能的,而有些事情实施起来很痛苦。我认为这个解决方案对我来说会更加灵活。

      在我的助手类'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>
      

      【讨论】:

        猜你喜欢
        • 2011-01-15
        • 2019-08-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-27
        • 1970-01-01
        • 2023-03-25
        相关资源
        最近更新 更多