2009-10-05 45 views
1

如何添加表单到我的layout.phtml?网站范围Zend_Form

我希望能够通过我的网站上的每种表单持续使用搜索表单和登录表单。

+0

我的答案有帮助吗? – Andrew 2009-10-06 14:21:30

回答

7

我有一个博客文章,解释这一点:http://blog.zero7ict.com/2009/11/how-to-create-reusable-form-zend-framework-zend_form-validation-filters/

在您的应用程序文件夹中创建一个表单文件夹

这是一个例子形式:

<?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; 

并显示它来回m你查看使用

echo $this->form; 

希望这会有所帮助。

编辑:如果你想要这个被列入everypage你可以创建一个新的辅助文件在您的意见

文件夹中创建文件夹的助手,并创建一个文件loginHelper.php

class Zend_View_Helper_LoginHelper 
{ 
    function loginHelper() 
    { 

$form = new Form_CreateEmail(); 
     $form->submit->setLabel('Add'); 
     return = $form; 

    } 
} 

这可能从您的布局使用输出:

<?php echo $this->LoginHelper(); ?>  
+1

请坚持编码标准!你的课应该是Form_CreateEmail! (如在Zend_View_Helper_LoginHelper中) – 2009-10-05 19:17:28

+0

错过了那个,谢谢指出:) – Andrew 2009-10-06 07:40:27

+0

init()存在,所以你不必重载'__construct($ options)'。 – chelmertz 2009-11-03 16:45:35

2

在布局只是做:

$form = new Loginform(); 
echo $form->render(); 

你只需要确保您指定张贴到形式的控制器/动作,所以这是不会发布到任何控制器,你现在是在,这是默认行为。