如何创建 zend 控制器实例

发布于 2024-10-20 23:42:18 字数 165 浏览 7 评论 0原文

我有一个名为 TestController 类的控制器,它扩展了一些 Zend_Controller_Action。现在我想在 TestForms(Zend_Form)中创建 TestController 的实例。我想动态填充 Zend_Form_Element_Select 。 请建议我如何做到这一点。提前致谢。

I have a controller named class TestController which extends some Zend_Controller_Action. Now I would like to use create an instance of TestController in TestForms (a Zend_Form). I want to populate a Zend_Form_Element_Select dynamically.
Please suggest how I can do this. Thanx in advance.

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

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

发布评论

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

评论(2

つ可否回来 2024-10-27 23:42:18

您在哪里实例化表单 - 是在控制器中吗?您应该考虑在实例化表单后在表单上设置值,而不是让表单调用控制器上的操作来动态获取值。

一种快速而肮脏的方法是获取控制器中的值并通过以下方式将其分配给元素:

$values = $db->query('query');
$element = $form->getElement('dynamicSelect');
$element->setValue($values);

当然,对控制器中的表进行数据库查询并不是最佳实践......根据 philistyne 的建议,我使用表单构建器类从我的模型动态构建表单。我为每个模型都有映射器,并将映射器传递给表单构建器类,以便它可以动态填充我的选择元素。

Where are you instantiating the form - is it in the controller? Instead of having the form call an action on the controller to dynamically get the values, you should look at setting the values on the form after it has been instantiated.

A quick and dirty way of doing that would be to grab the values in the controller and assign it to the element via:

$values = $db->query('query');
$element = $form->getElement('dynamicSelect');
$element->setValue($values);

Of course having DB queries to a table in your controller isn't exactly best practice... Per philistyne's suggestion, I use a a form builder class to build forms dynamically from my models. I have mappers for each model, and I pass in the mapper to the form builder class so it can dynamically populate my select elements.

删除→记忆 2024-10-27 23:42:18

可以尝试以下几件事(不建议将控制器传递到表单中或从表单内部实例化):

  1. 使用模型来访问要放入 Zend_Form_Element_Select 中的动态值。
  2. 如果表单很复杂,请创建一个表单构建器类来处理并分离出表单构建的繁重工作。
  3. 如果您觉得需要对表单元素的构造/行为/外观进行非常精细的控制,但希望能够在其他地方重用该元素,则可以通过从 Zend_Form_Element_(Radio、Select 等)扩展来创建自定义表单元素。

A couple of things to try (passing a controller into a form or instantiating from within one is not recommended):

  1. Use a model to access the dynamic values you want to put into your Zend_Form_Element_Select.
  2. If the form is complex, create a form builder class to take care of, and separate out, the heavy lifting of the form construction.
  3. Create customised form elements by extending from Zend_Form_Element_(Radio, Select, etc etc) if you feel you need very fine control over the form element's construction/behaviour/appearance, but wish to be able to reuse that element elsewhere.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文