views:

213

answers:

2

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.

+2  A: 

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.

smoove666
+7  A: 

I have a blog post explaining this: http://blog.zero7ict.com/2009/11/how-to-create-reusable-form-zend-framework-zend%5Fform-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(); ?>
Andi
Please stick to coding standarts ! your class should be Form_CreateEmail ! (as in Zend_View_Helper_LoginHelper)
Tomáš Fejfar
missed that one , thanks for pointing it out :)
Andi
The method `init()` exists so that you don't have to overload `__construct($options)`.
chelmertz