问题
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