[英]Override default Zend_Form element renderer with custom HTML template?
I'm dissatisfied with the way Zend_Form renders form elements by default, and want to override it for all forms produced by instantiating a helper class I have that inherits from Zend_Form to take care of some things that I do on all of my forms. 我对Zend_Form默认呈现表单元素的方式不满意,并希望通过实例化我从Zend_Form继承的一个帮助程序类生成的所有表单来覆盖它,以处理我在所有表单上执行的操作。
The changes I want to make seem more complicated than is reasonable/possible with decorators, so I'd like to use a custom HTML template to accomplish this, where I get to plug form values into a custom HTML snippet. 我想要做的更改似乎比装饰器的合理/可能更复杂,所以我想使用自定义HTML模板来实现这一点,我将表单值插入到自定义HTML片段中。
How can I set all HTML elements rendered by my class to use an HTML template? 如何设置我的类呈现的所有HTML元素以使用HTML模板? And what properties/functions should I call from the template to get the stuff Zend_Form renders by default? 我应该从模板中调用哪些属性/函数来获取Zend_Form默认渲染的东西? Lastly, I would prefer to do this without having to manually set the template on each input element I create in my code. 最后,我更愿意这样做而不必在我在代码中创建的每个输入元素上手动设置模板。
I've done my share of coding with ZF1 and the best way I found to render nice forms was to use Twitter Bootstrap along with it. 我已经完成了ZF1的编码工作,我发现渲染漂亮表单的最好方法是使用Twitter Bootstrap。
Check the following links to see if that's also a satisfactory solution for you too: 检查以下链接,看看这对您来说是否也是一个令人满意的解决方案:
You can extend the defualt Zend_Form
class with your own Custom_Form
class. 您可以使用自己的Custom_Form
类扩展defualt Zend_Form
类。 In the init()
method overwrite the default element decorators. 在init()
方法中覆盖默认的元素装饰器。 Here my code snippet: 这是我的代码片段:
//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')
);
}
I wound up using a custom viewscript that I genericized to work with arbitrary forms. 我使用了一个自定义的视图,我将其归结为使用任意形式。
Using this approach, I was able to do the following things: 使用这种方法,我能够做到以下几点:
Some of these things are impossible without a viewscript, and some are just a pain to implement. 如果没有意见稿,有些事情是不可能的,有些只是实施的痛苦。 I think this solution will be much more flexible for me going forward. 我认为这个解决方案对我来说会更加灵活。
In my helper class' render()
function: 在我的助手类' render()
函数中:
$view = new Zend_View();
$view->setBasePath(SRC_ROOT . "/templates/forms");
$this->setDecorators(array(array('ViewScript', array('viewScript' => 'viewscript.php'))));
And here's my viewscript: 这是我的观点:
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.