【问题标题】:Add some html to Zend Forms向 Zend Forms 添加一些 html
【发布时间】:2011-02-03 17:13:31
【问题描述】:

我正在寻找一段简单的代码,可以让我将以下 html 添加到我的 zend 表单中:

<div id="wmd-button-bar" class="wmd-panel"></div>

就是这样,它需要在表单中我的“方法”元素之上,仅此而已。对于这样一个简单的动作,我找不到任何不涉及我学习火箭科学的方法(即 Zend 装饰器)。

【问题讨论】:

  • 不幸的是,我是 JavaScript 新手。我现在只是做一个即插即用的拖放。如果我要理解它,我需要 PHP 中的答案。

标签: php zend-framework zend-form


【解决方案1】:

目前我能想到的唯一方法是在表单中添加一个虚拟元素并删除所有装饰器,除了具有您在问题中指定的属性的“HtmlTag”。移除装饰器意味着不会呈现实际元素 - 只会呈现 HtmlTag 装饰器。

所以假设你的表单是 $form:

$form->addElement(
    'hidden',
    'dummy',
    array(
        'required' => false,
        'ignore' => true,
        'autoInsertNotEmptyValidator' => false,
        'decorators' => array(
            array(
                'HtmlTag', array(
                    'tag'  => 'div',
                    'id'   => 'wmd-button-bar',
                    'class' => 'wmd-panel'
                )
            )
        )
    )
);
$form->dummy->clearValidators();

请注意,您要阻止对元素进行任何验证。这只是一种方式 - 可能还有其他方式。

输出:

<div id="wmd-button-bar" class="wmd-panel"></div>

有一个很好的article describing decorators

【讨论】:

  • +1 例如帮助 OP 使用装饰器,而不仅仅是建议使用它们(正如问题中提到的那样)
  • 是的,我多次看到那篇文章,但我只需要这一特定操作。我计划在此项目签署后立即转移到另一种语言。感谢您的帮助
【解决方案2】:

您可以创建自己的视图助手 libraray--App>View>Helper>PlainTextElemet.php

在您的库文件夹中创建一个名为 App 的文件夹 所以一个名为 View 的文件夹,所以在 View 中创建 Helper 文件夹,所以在 Helper 文件夹中创建 一个具有 PlainTextElement 名称的类,如下

 class App_View_Helper_PlainTextElement extends Zend_View_Helper_FormElement {

        public function PlainTextElement($name, $value = null, $attribs = null) {
            $info = $this->_getInfo($name, $value, $attribs);
            extract($info); // name, value, attribs, options, listsep, disable
            if (null === $value) {$value = $name;}

            return $value;
          }

    }

然后在上面相同的库中创建一个类 App>Form>Element>PlainText.php

并将以下代码放入该类中

class App_Form_Element_PlainText extends Zend_Form_Element_Xhtml {

    public $helper='PlainTextElement';

    public function isValid($value){

        return true;
    }
}

现在您可以在表单中创建您喜欢的每个 html 代码:

$someValue = '<div id="wmd-button-bar" class="wmd-panel"></div>';

        $this->addElement(new App_Form_Element_PlainText('pliantext1', array(
                            'value'=>$someValue,
        )));

不要忘记在您的 application.ini 中也添加以下行:

 autoloaderNamespaces.app = "App_"
 resources.view.helperPath.App_View_Helper="App/View/Helper"

【讨论】:

  • 这对我不起作用,我收到此错误:表单捕获异常:在注册表中找不到名为“PlainTextElement”的插件
  • 在我的视图助手中进行了一些更改,它现在可以正常工作了,谢谢。
【解决方案3】:

你可以试试这种方式,无需配置,只需一个扩展类 参考:http://www.zfsnippets.com/snippets/view/id/50

<?php

/**
 * Form note element
 *
 * @author Ruslan Zavackiy <ruslan.zavackiy@gmail.com>
 * @package elements
 */

/**
 * Loads helper Zend_View_Helper_FormNote
 */

class Custom_Form_Element_Note extends Zend_Form_Element_Xhtml
{
    public $helper = 'formNote';
}
?>

然后

$companies->addElement('note', 'companyNote', array(
            'value' => '<a href="javascript:;" id="addCompany">Add Company</a>'
        ));

【讨论】:

    【解决方案4】:

    使用一些 JQuery 怎么样:

    类似:

    <script language="javascript">
        $(document).ready(function() {
            $('#submit-element').append('<div id="wmd-button-bar" class="wmd-panel"></div>');
        });
    </script>
    

    【讨论】:

      【解决方案5】:
      • 创建一个返回标签(或其他任何东西)的自定义装饰器:

        类 My_Decorator_CustomHtml 扩展 Zend_Form_Decorator_Abstract {
                    公共函数渲染($内容)
                {
                    $element = $this->getElement();
                    if (!$element instanceof Zend_Form_Element) {
                        返回$内容;
                    }
                    if (null === $element->getView()) {
                        返回$内容;
                    }
                    $html = $element->getLabel();
                    返回 $html;
               }
        
        
        }
        
      • 把它放在装饰器路径中

        <pre>$form->addElementPrefixPath('My_Decorator', 'My/Decorator/', 'decorator');</pre>

      • 创建元素并将自定义html放入标签中

        $html = '<div id="wmd-button-bar" class="wmd-panel">some text....</div>'; $element = new Zend_Form_Element_Hidden('hidden-input', array( 'label'=>$html, ));
        $element->setDecorators(array('CustomHtml')); //add it to the form $form->addElement($element);

      就是这样

      【讨论】:

        【解决方案6】:

        此功能通过 Zend_Form_Element_Note 内置到 Zend 中。

        new Zend_Form_Element_Note('forgot_password', array(
            'value' => '<a href="' . $this->getView()->serverUrl($this->getView()->url(array('action' => 'forgot-password'))) . '">Forgot Password?</a>',
        ))
        

        【讨论】:

        • 这个答案收到了一些反对票。 Zend_Form_Element_Note 是 Zend 中的标准类(至少 Zend 1.x)
        【解决方案7】:

        我提供了一个 Html 元素,您可以将其包含在您自己的库中

        class Application_Form_Element_Html extends Zend_Form_Element_Xhtml
        {
            /**
             * Build the element and set the decorator callback to generate the html.
             */
            public function __construct($name, $options)
            {
                // Get the HTML to generate.
                $html = $options['html'];
        
                // Set the decorators for the generation.
                $this->setDecorators(array
                (
                    array('Callback', array
                    (
                        'callback' => function($content) use ($html)
                        {
                            return $html;
                        }
                    ))
                ));
            }
        }
        

        要包含它,别忘了做

        $form->addPrefixPath('Application_Form_Element', APPLICATION_PATH . '/forms/Element', 'element');
        

        然后在你的表单初始化中简单地调用:

        $form->addElement($this->createElement('html', 'info', array
        (
            'html' => '<div>My awesome HTML</div>';
        )));
        

        【讨论】:

          【解决方案8】:

          解决方案代码将此类添加到您的 /application/form 并从此类扩展您的所有表单

            class Application_Form_SpecialSubform extends Zend_Form_SubForm
          {
              protected $_openTag = '<form>';
              protected $_closeTag = '</form>';
              protected $_htmlIniCloseTagChars = '</';
          
              public function render(\Zend_View_Interface $view = null) {
                  if (!$this->isPartOfAForm())
                     $this->addDecorator('Form'); 
                  return parent::render($view);
              }
          
              protected function isPartOfAForm(){
                  return (!is_null($this->getElementsBelongTo()));
              }
          
              public function initForm()
              {       
                  $defaultZendCloseTag = $this->getDefaultFormViewCloseTag();      
                  $completeTag='';
                  $this->addDecorator('Form'); 
                  $this->getDecorator('Form')->setElement($this);
                  $completeTag=$this->getDecorator('Form')->render('');
                  $this->set_openTag(str_replace($defaultZendCloseTag, '', $completeTag));
                  return $this->get_openTag();
             }
          
              public function endForm()
              {
                  return $this->get_closeTag();
              }
          
              protected function getDefaultFormViewCloseTag()
              {
                  $defaultFormTag = $this->get_closeTag();
                  $view = $this->getView();
                  $defaultTag = $view->form('',null,true);
                  $pos = strrpos ($this->get_htmlIniCloseTagChars(),$defaultFormTag);
                  if ($pos !== false) {
                      $defaultFormTag =  substr($defaultTag, $pos); 
                  }
                  $this->set_closeTag($defaultFormTag);
                  return $defaultFormTag;
              }
              protected function get_openTag() {
                  return $this->_openTag;
              }
          
              protected function get_closeTag() {
                  return $this->_closeTag;
              }
          
              protected  function get_htmlIniCloseTagChars() {
                  return $this->_htmlIniCloseTagChars;
              }
          
              protected function set_openTag($_openTag) {
                  $this->_openTag = $_openTag;
              }
          
              protected  function set_closeTag($_closeTag) {
                  $this->_closeTag = $_closeTag;
              }
          
              protected function set_htmlIniCloseTagChars($_htmlIniCloseTagChars) {
                  $this->_htmlIniCloseTagChars = $_htmlIniCloseTagChars;
              }  
              }
          

          在您看来,当您想要打开表单标签时必须调用 initForm() 并调用 endForm() 来关闭它,因为您可以看到所有 ZF 行为都没有受到影响,因此它完全兼容。

          更多技术说明:

          要在我们的 zend 表单之间添加或注入任何代码,这是它在所有表单中使用子表单的最佳和最干净的方式,子表单是表单,因此您可以获得验证、过滤器等所有功能......而且您可以轻松地重用它并在您的表单或任何其他子表单内堆叠任意数量。处理生成的帖子也很简单。
          所以让我们举个例子 假设你有一个用户信息的管理员,比如地址、电话号码等,比如说 userInfo yout 网站的另一部分处理更多私人信息,如银行账户和宗教。以及至少一名受保护的受限区域管理员,负责处理用户密码和角色。 你当然有你的 3 种形式,在你的代码的不同控制器和动作上。 现在您需要将所有内容放在一起,但您需要大量标记才能在标签中显示或解释任何区域。 对于子表单,它的微不足道只是在您的视图中回显 $this->form->subformName 。 此时您会注意到表单标签不会出现并且您无法发送帖子。这是该技术的唯一问题,它将通过一个简单且(让我告诉它)优雅的类扩展和渲染方法重载来解决。

          【讨论】:

            【解决方案9】:

            把它放在你的视图脚本中...

            <!-- /application/views/scripts/myController/myAction.phtml -->
            
            <div id="wmd-button-bar" class="wmd-panel"></div>
            <?php echo $this->form ;?>
            

            【讨论】:

            • @bluedaniel:HTML 中没有“方法”元素,所以我将您的问题解释为希望将 div 放在包含方法属性的表单元素之上。
            • 对不起,我的意思是我给了 Method id 的 textarea。要求是一堆输入和这个 html 进入这个元素上方的表单。
            【解决方案10】:

            您必须添加decorator

            Any markup decorator 可能会有所帮助。

            有关装饰器的更多信息,请参阅:http://www.slideshare.net/weierophinney/leveraging-zendform-decorators

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2016-09-08
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多