Zend_Forms,Controller的位置?模型?别的地方? [英] Where to place Zend_Forms, Controller? Model? Somewhere else?

查看:183
本文介绍了Zend_Forms,Controller的位置?模型?别的地方?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最好把代码放在我的Zend_Forms中?



我曾经把这个逻辑放在Controllers里面,但是在我需要在不同的地方使用相同的表单。这意味着我必须在不同的控制器中复制表单的创建。



所以我将表单创建代码移动到我的模型中。这看起来是否正确,它适用于我。或者是有什么我失踪,他们应该实际上去其他地方?

解决方案

我通常把我的表单构建代码单独的文件,每个表单一个文件。

此外,我配置资源自动加载器,以便我可以在我的控制器中加载我的表单。



application / forms / Login.php

 <?php 
类Form_Login扩展了Zend_Form
{
public function init()
{
$ this-> addElement('text','username',array(
'filters'=> array('StringTrim','StringToLower'),
'required'=> ; true,
'label'=>'用户名:',
));

$ this-> addElement('password','password',array(
'filters'=> array('StringTrim'),
'required'= > true,
'label'=>'Password:',​​
));
$ b $ this-> addElement('submit','login',array(
'ignore'=> true,
'label'=>'Submit' ,
));




$ b $ p $在我的控制器中:

  $ loginForm = new Form_Login(); 


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?

解决方案

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

这篇关于Zend_Forms,Controller的位置?模型?别的地方?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆