使用 jQuery 和 Zend_Form 动态添加表单元素

发布于 2024-12-25 09:57:36 字数 1207 浏览 2 评论 0原文

我有一个表单,人们可以在其中使用加号按钮添加元素的相同部分,从而生成类似的内容:

<div id="person-1" class="person">
    <input type="text" name="name-1" id="name-1" />
    <input type="text" name="age-1" id="age-1" />
</div>
<!-- as of here, it's JS created -->
<div id="person-2" class="person">
    <input type="text" name="name-2" id="name-2" />
    <input type="text" name="age-2" id="age-2" />
</div>
<div id="person-3" class="person">
    <input type="text" name="name-3" id="name-3" />
    <input type="text" name="age-3" id="age-3" />
</div>

我已经设法编写了 jquery 代码,该代码允许我再次添加相同的元素一个新的 ID(姓名-1、年龄-1、姓名-2、年龄-2、姓名-3、年龄-3,...)。

当然,Zend_Form 不知道 name-2name-3,因此当表单包含错误并再次显示时,它只是删除它们。我也无法使用 $form->getValue('name-2') 访问 name-2 的值。我必须检查原始 $this->getRequest()->getPost()

有没有更好的方法可以用来组合 Zend_Form 和基于 javascript 的添加表单元素(与硬编码元素的类型相同)。

警告:在真正的问题中,是选择而不是输入。发现这可能会产生影响(使用 ->setIsArray(true)),但使用 select 会破坏示例代码。

I have a form in which people shall be able to add the same portion of elements with a plus-button, so that something like this is produced:

<div id="person-1" class="person">
    <input type="text" name="name-1" id="name-1" />
    <input type="text" name="age-1" id="age-1" />
</div>
<!-- as of here, it's JS created -->
<div id="person-2" class="person">
    <input type="text" name="name-2" id="name-2" />
    <input type="text" name="age-2" id="age-2" />
</div>
<div id="person-3" class="person">
    <input type="text" name="name-3" id="name-3" />
    <input type="text" name="age-3" id="age-3" />
</div>

I already managed to write jquery-code that allows me to add the same elements once again with a new id (name-1, age-1, name-2, age-2, name-3, age-3, …).

Of course, Zend_Form does not know about name-2 and name-3, so it just drops them when the form contains an error and is displayed again. Neither can I access the value of name-2 with $form->getValue('name-2'). I have to go over raw $this->getRequest()->getPost().

Is there a better method I can use to combine Zend_Form and javascript-based added form elements (of same type like an hardcoded element).

Caveat: In the real problem, it’s select and not input. Found out this could make a difference (with ->setIsArray(true)), but using select would blow up the example code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥抱影子 2025-01-01 09:57:36

您可以做的是在主窗体内创建一个子窗体容器,并向该容器添加 X 数量的子窗体。
例如:

class My_Form extends Zend_Form
{
    private $_numPersons = 1;

    public function setNumPersons($numPersons)
    {
        $this->_numPersons = (int) $numPersons;
    }


    public function init()
    {
        $container = new Zend_Form_SubForm();
        $this->addSubForm($container, 'persons');

        for($index = 0; $index < $this->_numPersons; $index++) {
            $personForm = new My_PersonForm();
            $container->addSubForm($personForm, $index+1);
        }
    }
}

渲染时,输入字段的名称将类似于 persons[1][name]。注意$index+1,Zend_Form 不允许表单被命名为“0”。

当然,只有在人员子表单的数量有限时才应该使用此方法。

另一种策略是重写 isValid 方法并使用单个 My_PersonForm 表单来验证所有人员数据。

旁注;仅当您在创建表单实例时将 numPersons 定义为选项集的一部分时,上述代码才有效。例如;

$form = new My_Form(array('numPersons' => 10));

What you could do is create a subform container inside your main form and add an X amount of subforms to that container.
For example:

class My_Form extends Zend_Form
{
    private $_numPersons = 1;

    public function setNumPersons($numPersons)
    {
        $this->_numPersons = (int) $numPersons;
    }


    public function init()
    {
        $container = new Zend_Form_SubForm();
        $this->addSubForm($container, 'persons');

        for($index = 0; $index < $this->_numPersons; $index++) {
            $personForm = new My_PersonForm();
            $container->addSubForm($personForm, $index+1);
        }
    }
}

When rendered, the input fields will have names like persons[1][name]. Note the $index+1, Zend_Form does not allow a form to be named '0'.

Ofcourse, you should only use this method if the amount of person subforms is limited.

Another strategy would be to override the isValid method and use a single My_PersonForm form to validate all the person data.

Sidenote; the above code will only work when you define the numPersons as part of the options set, when creating the form instance. E.g.;

$form = new My_Form(array('numPersons' => 10));

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文