【问题标题】:Zend HTML5 Form element typesZend HTML5 表单元素类型
【发布时间】:2011-06-22 00:27:18
【问题描述】:

是否有任何简单的方法可以将 HTML5 表单元素转换为 Zend 表单? createElement('tel','phone');只是不起作用,因为 zend 不支持 html5 表单元素,但它似乎......你不能在创建后覆盖 type 属性。

例如我需要在我的 zend 表单上输入 type="tel/email/date/number" 等。

【问题讨论】:

    标签: html zend-form


    【解决方案1】:

    没关系,我找到了这篇文章:http://www.enrise.com/2010/12/html5-zend-framework-form-elements/。我只需要将他的代码复制到我的 MVC 架构中,它现在就可以工作了。除了我必须将 $this->setAttrib('type','number') 例如放在 Number.php 中,因为它不会添加选项,除非它在控制器中设置,这有点愚蠢,因为我正在创建一个新的 Glitch_Form_Element_Text_Number 或任何 o_O。

    【讨论】:

    • 您确定将您的文档类型设置为 HTML 5 吗?它将根据您设置的文档类型更改表单文本输入上的type$documentType = new Zend_View_Helper_Doctype(); $documentType->doctype('HTML5');
    • 是的,该网站有 HTML 5 文档类型。从阅读代码看起来它实际上甚至不会设置 type="number" 除非它被设置在对象中,即它有 if(!isset($elem->getAttrib('type')) 或类似的东西......
    【解决方案2】:

    本诺,

    这是我为使 Glitch 工作所做的工作:(Zend Framework 1.11)

    在我的控制器 init() 函数中

    //someController 扩展 Zend_Controller_Action

    public function init(){
        $this->view->addHelperPath('Glitch/View/Helper', 'Glitch_View_Helper_');
    }
    

    然后在我的元素创建中,我只是用 attrib('type','email') 覆盖类型

    public function getEmailField(){
         $element = new Zend_Form_Element_Text('email');
         $element->setLabel('Email');
         $element->setAttrib("type", "email");
         $element->setRequired();
    
         return $element;
    }
    

    不设置视图助手,输入变为文本。

    谢谢!

    【讨论】:

    • 您正在加载一个不存在的视图助手。如果是自定义视图助手,它的作用是什么?它与这个问题有什么关系?
    • Glitch_View_Helper 是人们用来让 HTML5 元素工作的 View Helper。请注意,Benno 在他的回复中使用了 Glitch_View_Helper。他甚至链接到这里的文章enrise.com/2010/12/html5-zend-framework-form-elements。没有必要重新解释自定义视图助手,因为它之前已经解释过了。我也使用了视图助手,它确实存在。
    • 那么您至少应该参考其他答案,但最好简短地解释一下故障。答案必须能够在 SE 网站上相互独立。
    • 我相信任何查看此页面的人都会确切地知道我所引用的内容,因为回复此主题的每个人都使用 Glitch_View_Helper 来解决问题。
    【解决方案3】:

    我创建了一个writeup,展示了如何最好地使用 Enrise HTML5 表单元素,重申您需要指定 HTML5 文档类型这一事实。

    【讨论】:

      【解决方案4】:

      这是我为解决 XML5 元素所做的。

      首先我在以下位置创建了一个自定义表单元素:library/Custom/Form/Element/Html5.php

          <?php
      
          /** Zend_Form_Element_Xhtml */
          require_once 'Zend/Form/Element/Xhtml.php';
      
      
          class Custom_Form_Element_Html5 extends Zend_Form_Element_Xhtml
          {
              public $helper = 'formHtml5';
          }
      

      然后我在以下位置创建了一个自定义视图助手:library/Custom/View/Helper/FormHtml5.php

          <?php
      
      
          /**
          * Abstract class for extension
          */
          require_once 'Zend/View/Helper/FormElement.php';
      
      
          /**
          * Helper to generate an "Html5" element
          *
          */
          class Custom_View_Helper_FormHtml5 extends Zend_View_Helper_FormElement
          {
      
              public function formHtml5($name, $value = null, $attribs = null)
              {
                  $info = $this->_getInfo($name, $value, $attribs);
                  extract($info); // name, value, attribs, options, listsep, disable
      
                  // build the element
                  $disabled = '';
                  if ($disable) {
                      // disabled
                      $disabled = ' disabled="disabled"';
                  }
      
                  // XHTML or HTML end tag?
                  $endTag = ' />';
                  if (($this->view instanceof Zend_View_Abstract) && !$this->view->doctype()->isXhtml()) {
                      $endTag= '>';
                  }
      
                  $xhtml = '<input'
                          . ' type="' . (($attribs['type'])?($this->view->escape($attribs['type'])):'text') . '"'
                          . ' name="' . $this->view->escape($name) . '"'
                          . ' id="' . $this->view->escape($id) . '"'
                          . ' value="' . $this->view->escape($value) . '"'
                          . $disabled
                          . $this->_htmlAttribs($attribs)
                          . $endTag;
      
                  return $xhtml;
              }
          }
      

      然后在表格中我添加了这个:

          class Application_Form_UserBasic extends Zend_Form
          {
      
              public function init()
              {
                   // this will tell zf to look for custom helpers on your custom library
                   $view = $this->getView();
                   $view->addHelperPath(APPLICATION_PATH.'/../library/Custom/View/Helper/', 'Custom_View_Helper');
      
                   /* Some other code */
      
                   $email      = new Custom_Form_Element_Html5('email');
                   $email->setAttribs(array( 'type' => 'email'));
      
                   /* Your other elements*/
      
                   $this->addElements(array(
                        $email, /* your other elements */
                   ));
              }
          }
      

      如果您还没有这样做,请不要忘记将此行添加到您的 application.ini 文件中:

          autoloaderNamespaces[] = "Custom_"
      

      我希望它对某人有所帮助。

      【讨论】:

        【解决方案5】:

        由于没有“开箱即用”的解决方案,我自己编写并制作了一个作曲家包以便于分发。 如果你使用composer,可以使用https://github.com/Alez/html5input

        1. 在控制台中输入 composer 需要 alez/html5input
        2. 在您的表单类中只需创建一个带有“type”属性的实例 Html5Input_Element_AnyType 或使用php $formElement-&gt;setAttrib('type', 'email') 将其“type”属性设置为您想要的任何内容。这就是它和 Zend_Form_Element_Text 的唯一区别,你可以把它当作简单的文本元素。

        示例代码:

        class My_Form extends Zend_Form
        {
            public function init()
            {
                $email = new Html5Input_Element_AnyType('email', array(
                    'type' => 'email',
                ));
        
                $this->addElement($email);
            }
        }
        

        【讨论】:

        • 请详细说明并提供一个示例,将其变成完整的 Stack Overflow 答案。谢谢!
        猜你喜欢
        • 2015-05-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多