Zend_Form - 同一页面上的多个表单 [英] Zend_Form - multiple forms on same page

查看:24
本文介绍了Zend_Form - 同一页面上的多个表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一页中有多个表单,当我提交其中一个时,我怎么知道提交的是哪一个?

Having multiple forms in one page, when i submit one of them, how can i tell wich one was submitted?

我想为每个 from 生成 uniqe id,并将它们保存为隐藏字段和用户会话 - 虽然这是一个解决方案,但它的问题是没有好地方可以从会话中删除旧 id.

I thought about generating uniqe ids for each from, and saving them as hidden fields and to the user-session - while this is a solution, the problem with it is that there is no good place to remove old ids from the session.

有什么更好的想法来解决这个问题吗?

Any better ideas how to solve this problem?

提前致谢!

推荐答案

首先:您是否考虑过将两个表单发送到两个不同的操作?这样你就可以在一个动作中分别处理每个表单.如果您使用 Zend MVC 组件,这应该是最佳实践".

First of all: have you considered sending the two forms to two different actions? That way you can handle each form separately in an action each. This should be the "best-pratice" if you're using the Zend MVC component.

另一个选项是检查将包含在请求中的提交按钮的值,例如

The other option is to check for the value of the submit button which will be included in the request, e.g.

<input type="submit" name="save" value="form1" />
// in PHP:
// $_POST["save"] will contain "form1"

<input type="submit" name="save" value="form2" />
// in PHP:
// $_POST["save"] will contain "form2"

要小心,因为 value 属性将呈现为按钮的标签.

Be careful as the value-attribute will be rendered as the button's label.

所以也许您想通过不同的提交按钮名称来区分表单:

So perhaps you want to distingush the forms by different submit-button names:

<input type="submit" name="save-form1" value="Submit" />
// in PHP:
// $_POST["save-form1"] will contain "Submit"

<input type="submit" name="save-form2" value="Submit" />
// in PHP:
// $_POST["save-form2"] will contain "Submit"

在 OP 和我自己之间的评论对话中,以下似乎是一个可能的解决方案:

During the comment-dialog between the OP and myself the following seems to be a possible solution:

class My_Form_Base extends Zend_Form
{
    private static $_instanceCounter = 0;

    public function __construct($options = null)
    {
        parent:: __construct($options);

        self::$_instanceCounter++;
        $this->addElement('hidden', 'form-id', 
            sprintf('form-%s-instance-%d', $this->_getFormType(), self::$_instanceCounter);
    }

    protected _getFormType()
    {
        return get_class($this);
    }
}

class My_Form_Type1 extends My_Form_Base
{
    public function init()
    {
        // more form initialization
    }
}

class My_Form_Type2 extends My_Form_Base
{
    public function init()
    {
        // more form initialization
    }
}

这篇关于Zend_Form - 同一页面上的多个表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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