如何将表单添加到我的 layout.phtml?
我希望能够拥有一个搜索表单和一个登录表单,该表单在我网站上的每个表单中都存在。
如何将表单添加到我的 layout.phtml?
我希望能够拥有一个搜索表单和一个登录表单,该表单在我网站上的每个表单中都存在。
我有一篇博客文章对此进行了解释: http: //blog.zero7ict.com/2009/11/how-to-create-reusable-form-zend-framework-zend_form-validation-filters/
在您的 Application 文件夹中创建一个 Forms 文件夹
这是一个示例表单:
<?php
class Form_CreateEmail extends Zend_Form
{
public function __construct($options = null)
{
parent::__construct($options);
$this->setName('createemail');
$title = new Zend_Form_Element_Text('title');
$title->setLabel('Subject')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
$info = new Zend_Form_Element_Textarea('info');
$info->setLabel('Email Content')
->setAttribs(array('rows' => 12, 'cols' => 79));
$submit = new Zend_Form_Element_Submit('submit');
$submit->setAttrib('id', 'submitbutton');
$this->addElements(array($title, $info, $submit));
}
}
?>
然后你可以像这样从你的控制器调用它:
$form = new Form_CreateEmail();
$form->submit->setLabel('Add');
$this->view->form = $form;
并从您的视图中使用
echo $this->form;
希望这可以帮助。
编辑:如果您希望将其包含在每个页面中,您可以创建一个新的帮助文件
在您的意见文件夹中创建一个助手文件夹并创建一个 loginHelper.php 文件
class Zend_View_Helper_LoginHelper
{
function loginHelper()
{
$form = new Form_CreateEmail();
$form->submit->setLabel('Add');
return = $form;
}
}
这可以使用以下方式从您的布局中输出:
<?php echo $this->LoginHelper(); ?>
在您的布局中,只需执行以下操作:
$form = new Loginform();
echo $form->render();
您只需要确保为要发布到的表单指定一个控制器/操作,这样就不会发布到您当前所在的任何控制器,这是默认行为。