Jake N
Jake N

Reputation: 10583

Where to place Zend_Forms, Controller? Model? Somewhere else?

Where is it best to put the code for building my Zend_Forms?

I used to put this logic inside my Controllers, but moved away from that after I needed to use the same form in different places. It meant I had to duplicate the creation of forms in different controllers.

So I moved the form creation code into my Models. Does this seem right, it works for me. Or is there something I am missing, and they should in fact go somewhere else?

Upvotes: 2

Views: 417

Answers (1)

Benjamin Cremer
Benjamin Cremer

Reputation: 4822

I usually put my form building code in separate files, one file per form.
Additionally I configure the Resource Autoloader so that I can load my forms in my controllers.

application/forms/Login.php

<?php
class Form_Login extends Zend_Form
{
    public function init()
    {
        $this->addElement('text', 'username', array(
            'filters'    => array('StringTrim', 'StringToLower'),
            'required'   => true,
            'label'      => 'Username:',
        ));

        $this->addElement('password', 'password', array(
            'filters'    => array('StringTrim'),
            'required'   => true,
            'label'      => 'Password:',
        ));

        $this->addElement('submit', 'login', array(
            'ignore'   => true,
            'label'    => 'Submit',
        ));
    }
}

In my controllers:

$loginForm = new Form_Login();

Upvotes: 5

Related Questions