Zend framework Zend_表单-同一页面上有多个表单
在一个页面中有多个表单,当我提交其中一个表单时,我如何知道是哪个表单提交的 我考虑过为每个from生成uniqe ID,并将它们保存为隐藏字段并保存到用户会话中——虽然这是一个解决方案,但问题是没有从会话中删除旧ID的好地方 有没有更好的办法来解决这个问题Zend framework Zend_表单-同一页面上有多个表单,zend-framework,zend-form,Zend Framework,Zend Form,在一个页面中有多个表单,当我提交其中一个表单时,我如何知道是哪个表单提交的 我考虑过为每个from生成uniqe ID,并将它们保存为隐藏字段并保存到用户会话中——虽然这是一个解决方案,但问题是没有从会话中删除旧ID的好地方 有没有更好的办法来解决这个问题 提前谢谢 首先:您是否考虑过将这两个表单发送到两个不同的操作?这样,您可以在每个操作中分别处理每个表单。如果使用Zend MVC组件,这应该是“最佳实践” 另一个选项是检查提交按钮的值,该按钮将包含在请求中,例如 <input type
提前谢谢 首先:您是否考虑过将这两个表单发送到两个不同的操作?这样,您可以在每个操作中分别处理每个表单。如果使用Zend MVC组件,这应该是“最佳实践” 另一个选项是检查提交按钮的值,该按钮将包含在请求中,例如
<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"
编辑:
在OP和我之间的评论对话中,以下似乎是一个可能的解决方案:
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
}
}
代码中的一些错误应该是这样的:
class Application_Form_Idee_Base extends Zend_Form
{
private static $_instanceCounter = 0;
public function __construct($options = null)
{
parent::__construct($options);
self::$_instanceCounter++;
$this->addElement('hidden', 'form-id', array(
'value' => sprintf('form-%s-instance-%s', $this->_getFormType(), self::$_instanceCounter))
);
}
protected function _getFormType()
{
return get_class($this);
}
}
两个操作:无法真正做到这一点,因为如果表单+错误未验证,我希望在同一页面中显示它。提交按钮:一个页面中可以有多个相同表单的Instance。此外,我不想在创建表单时将参数传递给表单。这太容易了;)我认为你应该更详细地描述你想要实现的目标。你想生成一个包含多个表单的页面(其中一些表单是同一表单的实例),你不想为每个表单添加任何特殊内容,但你仍然希望能够在提交时区分表单?我不明白为什么发布到不同的操作不是一个解决方案。。。你可以从每一个动作中呈现你想要的任何东西。格林:第一条评论:这正是我想要的。第二条评论:想象一下:该页面中的表单数量没有定义,可以是一个,也可以是50个,所以我不可能给每个表单一个单独的操作。我要求的是这个问题的通用解决方案,而不是目前有效的解决方案。
class Application_Form_Idee_Base extends Zend_Form
{
private static $_instanceCounter = 0;
public function __construct($options = null)
{
parent::__construct($options);
self::$_instanceCounter++;
$this->addElement('hidden', 'form-id', array(
'value' => sprintf('form-%s-instance-%s', $this->_getFormType(), self::$_instanceCounter))
);
}
protected function _getFormType()
{
return get_class($this);
}
}