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

こ雲淡風輕ζ 提交于 2019-12-07 22:01:56

问题


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?


回答1:


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();


来源:https://stackoverflow.com/questions/3627531/where-to-place-zend-forms-controller-model-somewhere-else

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