在 Zend 框架中使用 Zend_Form 的表单上可变数量的字段

发布于 2024-11-09 13:42:41 字数 474 浏览 0 评论 0原文

我有一个数据库表,并且在第一个表和第二个表中定义了一个链接表“站点”,可以添加从一个到多个不等的多个值,这些值指向“站点”。用户可以打开表单中的“站点”字段,其中链接的数据然后显示在字段中,以便用户可以编辑值。

例如“站点一”有一个相关字段。

“站点二”有三个相关字段。

“站点三”有两个相关字段。 等等。

如果我手动启动它,我只会读取数据并创建额外的字段,但我正在使用 Zend_Form 构建它,它似乎想提前知道字段的数量。我正在使用 models 目录中的模型以及脚本目录中的 _form_xxx.phtml (部分?)。

我对 Zend 很陌生,感觉有点像 Zend_Form 中的方法,否则我似乎必须重写 php 和 phtml ?

子表单(Zend_Form_SubForm)是一种可行的方法吗?

由于 Zend 很灵活,我认为在最坏的情况下我可以用普通的方式编写组件,但如果可能的话我想使用框架中的工具。

I have a db table and a linked table "site" is defined in the first and in the second a number of values varying from one to many can be added that point back to the "site". The user can open the "site" field in a form with the linked data then showing in fields so the user can edit the values.

e.g. "Site One" has one related field.

"Site Two" has three related fields.

"Site Three" has two related fields.
Etc.

If i was hand cranking it i would just read the data in and create the extra fields but i am building it with Zend_Form which appears to want to know the number of fields in advance. I am using a model in the models directory with an _form_xxx.phtml in the scripts directory (a partial?).

I am newish to Zend and feeling my way a bit so is there a way in Zend_Form of doing this as i would seem to have to rewrite php and phtml otherwise?

Are sub forms, Zend_Form_SubForm's, a way to go?

As Zend is flexible i assume at the worst i can write the component in the plain vanilla way but i would like to use the tools in the framework if possible.

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

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

发布评论

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

评论(1

草莓味的萝莉 2024-11-16 13:42:41

是什么让您认为 Zend_Form 应该是静态的?您可以根据需要在运行时动态创建表单内的字段。只需将添加动态表单元素的逻辑放入表单类(扩展 Zend_Form)的构造函数中即可。

IE。

class YourSplendidForm extends Zend_Form
{
    public function __construct($options = null, $yourParams)
    {
        parent::__construct($options);

        $this->setName('your_form_name');
        $this->setAction('/your/form/action/');

        // Add your fields here using addElement - or create a subform and add fields to that one and use addSubForm to add the subform


    }
}

然后只需将表单实例传递到您的视图(从控制器)并根据需要对其进行处理(通过简单的 form; ?> 或自定义渲染为你觉得合适。

What makes you think that the Zend_Form should be static? You can dynamically create the fields inside your form at runtime as you see fit. Just put the logic that adds your dynamic form elements in the constructor of your form class (which extends Zend_Form).

ie.

class YourSplendidForm extends Zend_Form
{
    public function __construct($options = null, $yourParams)
    {
        parent::__construct($options);

        $this->setName('your_form_name');
        $this->setAction('/your/form/action/');

        // Add your fields here using addElement - or create a subform and add fields to that one and use addSubForm to add the subform


    }
}

Then simply pass the form instance to your view (from the controller) and process it as needed (either through a simple <?php echo $this->form; ?> or custom rendering as you see fit.

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