Site wide Zend_Form

只谈情不闲聊 提交于 2020-01-24 19:44:10

问题


How can I add a form to my layout.phtml?

I would like to be able to have a search form and a login form that persists through every form on my site.


回答1:


I have a blog post explaining this: http://blog.zero7ict.com/2009/11/how-to-create-reusable-form-zend-framework-zend_form-validation-filters/

In your Application folder create a Forms folder

This is an example form:

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

}
?>

You can then call it from your controller like this:

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

And display it from you view using

echo $this->form;

Hope this helps.

Edit: if you want this to be included on everypage you could create a new helper file

in your views folder create a helpers folder and create a loginHelper.php file

class Zend_View_Helper_LoginHelper
{
    function loginHelper()
    {

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

    }
}

This could be output from your layout using:

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



回答2:


In your Layout just do:

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

You just have to make sure that you specify a Controller / Action for the form to POST to, so that is does not POST to whatever Controller you currently are on, wich is the default behaviour.



来源:https://stackoverflow.com/questions/1520864/site-wide-zend-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!