我結束了使用自定義的觀點,我泛化,以任意形式工作。
使用這種方法,我能夠做以下事情:在一個DIV
- 所需的表單元素的標籤後添加一個星號
- 組輸入和錯誤在一起,這樣,當我漂浮標籤的左側東西還是排隊
- 添加一個特殊的類來示數輸入,這樣我就可以凸顯他們
- 更改某些錯誤信息,包括該元素的名稱,而不是「價值」
- 傳遞文字註釋與表單元素沿下輸入要顯示
- 在特殊元素
有些事情是不可能沒有viewscript,有的只是一個痛苦的不換標籤和輸入實行。我認爲這個解決方案對我而言將會更加靈活。
在我的輔助類render()
功能:
$view = new Zend_View();
$view->setBasePath(SRC_ROOT . "/templates/forms");
$this->setDecorators(array(array('ViewScript', array('viewScript' => 'viewscript.php'))));
這是我的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>
是否有我應該怎麼寫那個人,通用表單元素工作viewscripts任何文件?我所看到的全部是如何爲整個表單編寫一個viewscript,這不是我所追求的。我喜歡我可以將元素對象傳遞給Zend_Form,並且它可以呈現沒有我爲每個我需要的表單編寫HTML的東西,我只是希望它呈現不同的東西*。 – spiffytech 2013-04-24 13:29:24
搜索setElementDecorators() – konradwww 2013-04-24 14:00:29