php 向 Zend Forms 添加一些 html
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2566432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add some html to Zend Forms
提问by bluedaniel
Im looking for a simple bit of code that will let me add the following html into my zend form:
我正在寻找一些简单的代码,可以让我将以下 html 添加到我的 zend 表单中:
<div id="wmd-button-bar" class="wmd-panel"></div>
<div id="wmd-button-bar" class="wmd-panel"></div>
Thats it, it needs to be above my 'method' element in the form but thats it. For such a simple action I cant find any methods that don't involve me learning rocket science (i.e Zend Decorators).
就是这样,它需要在表单中我的“方法”元素之上,仅此而已。对于这样一个简单的操作,我找不到任何不涉及我学习火箭科学的方法(即 Zend 装饰器)。
回答by jah
The only way I can think of at the moment is to add a dummy element to the form and remove all decorators except an 'HtmlTag' with the attributes you specified in your question. Removing the decorators means that the actual element will not be rendered - only the HtmlTag decorator will be rendered.
目前我能想到的唯一方法是在表单中添加一个虚拟元素并删除除“HtmlTag”之外的所有装饰器,其中包含您在问题中指定的属性。移除装饰器意味着不会渲染实际元素——只会渲染 HtmlTag 装饰器。
so assuming your form is $form:
所以假设你的表格是 $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();
Note that you want to prevent any validation of the element. This is only one way - there are likely others.
请注意,您希望防止对元素进行任何验证。这只是一种方式——可能还有其他方式。
Output:
输出:
<div id="wmd-button-bar" class="wmd-panel"></div>
There is a good article describing decorators.
有一篇很好的文章描述了装饰器。
回答by user1400
You can create your own view helper libraray--App>View>Helper>PlainTextElemet.php
您可以创建自己的视图助手库--App>View>Helper>PlainTextElemet.php
Create a folder in your library folder that name is App so a folder that name is View so in View create Helper Folder so in Helper folder create a class with PlainTextElement name same following
在您的库文件夹中创建一个名称为 App 的文件夹,因此名称为 View 的文件夹,因此在 View 创建 Helper 文件夹,因此在 Helper 文件夹中创建一个名称与以下相同的类
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;
}
}
Then in libray same above create a class App>Form>Element>PlainText.php
然后在上面相同的库中创建一个类 App>Form>Element>PlainText.php
And put folowing code in this class
并将以下代码放在这个类中
class App_Form_Element_PlainText extends Zend_Form_Element_Xhtml {
public $helper='PlainTextElement';
public function isValid($value){
return true;
}
}
Now in your form you can create each html code you like:
现在在您的表单中,您可以创建您喜欢的每个 html 代码:
$someValue = '<div id="wmd-button-bar" class="wmd-panel"></div>';
$this->addElement(new App_Form_Element_PlainText('pliantext1', array(
'value'=>$someValue,
)));
Don't forget in your application.ini add fllowing lines too:
不要忘记在您的 application.ini 中也添加流线:
autoloaderNamespaces.app = "App_"
resources.view.helperPath.App_View_Helper="App/View/Helper"
回答by Tri.Le
You can try this way, no config, just one extension class reference: http://www.zfsnippets.com/snippets/view/id/50
你可以试试这种方式,没有配置,只有一个扩展类参考:http: //www.zfsnippets.com/snippets/view/id/50
<?php
/**
* Form note element
*
* @author Ruslan Zavackiy <[email protected]>
* @package elements
*/
/**
* Loads helper Zend_View_Helper_FormNote
*/
class Custom_Form_Element_Note extends Zend_Form_Element_Xhtml
{
public $helper = 'formNote';
}
?>
then
然后
$companies->addElement('note', 'companyNote', array(
'value' => '<a href="javascript:;" id="addCompany">Add Company</a>'
));
回答by richie
How about using some JQuery:
使用一些 JQuery 怎么样:
Something like:
就像是:
<script language="javascript">
$(document).ready(function() {
$('#submit-element').append('<div id="wmd-button-bar" class="wmd-panel"></div>');
});
</script>
回答by mohamed elbou
Create a custom Decorator that return the label(or anything else):
class My_Decorator_CustomHtml extends Zend_Form_Decorator_Abstract { public function render($content) { $element = $this->getElement(); if (!$element instanceof Zend_Form_Element) { return $content; } if (null === $element->getView()) { return $content; } $html = $element->getLabel(); return $html; }}Place this in the decorator path
<pre>$form->addElementPrefixPath('My_Decorator', 'My/Decorator/', 'decorator');</pre>Create the element and put the custom html in the label
$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);
创建一个返回标签(或其他任何东西)的自定义装饰器:
class My_Decorator_CustomHtml extends Zend_Form_Decorator_Abstract { public function render($content) { $element = $this->getElement(); if (!$element instanceof Zend_Form_Element) { return $content; } if (null === $element->getView()) { return $content; } $html = $element->getLabel(); return $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);
and that's it
就是这样
回答by Baptiste Pernet
I come with a Html element that you can include in you own library
我附带了一个 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;
}
))
));
}
}
To include it, don't forget to do
要包括它,不要忘记做
$form->addPrefixPath('Application_Form_Element', APPLICATION_PATH . '/forms/Element', 'element');
Then in you form initialization simply call:
然后在你的表单初始化中简单地调用:
$form->addElement($this->createElement('html', 'info', array
(
'html' => '<div>My awesome HTML</div>';
)));
回答by Plinio
SOLUTION CODE add this class to your /application/form and extend all your forms from this class
解决方案代码将此类添加到您的 /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;
}
}
at your view, you must call initForm() when you want to open the form tag and endForm() to close it, as you can see ALL ZF behaviour is untouched so its totally compatible.
在您看来,当您想打开表单标签时必须调用 initForm() 并调用 endForm() 来关闭它,因为您可以看到所有 ZF 行为都未受影响,因此它完全兼容。
MORE TECH EXPLANATION:
更多技术说明:
To add or inject any code between our zend forms the best and cleanest way it use SubForms in all of your forms, Subforms are forms so you got all features like validation, filter .... and also you can reuse it easy and stack as many as you want inside your form or inside any other subform. Also handle the resulting post its trivial.
so lets do an example
supouse you got an admin of user information like address, telephone number etc, lets say userInfo
another part of yout site handles more private information like banck account and religion. and at least one more restricted area admin protected who handle user pasword and role.
you of course has your 3 forms, at different controllers and actions of your code.
And now you need to put it all together, but you need a lot of markup to show it in labels or to explain any area.
with subforms its trivial just echo $this->form->subformName at your view.
at this point you will notice that the form tag will not appear and you are unable to send the post. this is the only problem of this tecnique and it will solve with a simple and (let me tell it) elegant class extend and overload of render method.
要在我们的 zend 表单之间添加或注入任何代码,它以在所有表单中使用子表单的最佳和最干净的方式,子表单是表单,因此您可以获得所有功能,例如验证、过滤……而且您可以轻松地重用它并堆叠为您可以在表单中或任何其他子表单中随意使用。还要处理由此产生的帖子。
所以让我们举一个例子,假设你有一个用户信息的管理员,比如地址、电话号码等,假设你网站的另一部分 userInfo 处理更多的私人信息,比如银行帐户和宗教。以及至少一名受保护的受限制区域管理员处理用户密码和角色。你当然有你的 3 种形式,在你的代码的不同控制器和动作中。现在你需要把它们放在一起,但你需要大量的标记来在标签中显示它或解释任何区域。带有子表单它的琐碎只是在您的视图中 echo $this->form->subformName 。此时您会注意到表单标签不会出现并且您无法发送帖子。这是该技术的唯一问题,它将通过简单且(让我告诉它)优雅的类扩展和渲染方法重载来解决。
回答by axiom82
This functionality is built into Zend via Zend_Form_Element_Note.
此功能通过 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>',
))
回答by Thomas
Put it in your view script...
把它放在你的视图脚本中......
<!-- /application/views/scripts/myController/myAction.phtml -->
<div id="wmd-button-bar" class="wmd-panel"></div>
<?php echo $this->form ;?>
回答by takeshin
You have to add decorator.
你必须添加装饰器。
Any markup decoratormay be helpful.
任何标记装饰器都可能有所帮助。
For further information about decorators see: http://www.slideshare.net/weierophinney/leveraging-zendform-decorators
有关装饰器的更多信息,请参阅:http: //www.slideshare.net/weierophinney/leveraging-zendform-decorators

