如何创建 zend 控制器实例
我有一个名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在哪里实例化表单 - 是在控制器中吗?您应该考虑在实例化表单后在表单上设置值,而不是让表单调用控制器上的操作来动态获取值。
一种快速而肮脏的方法是获取控制器中的值并通过以下方式将其分配给元素:
当然,对控制器中的表进行数据库查询并不是最佳实践......根据 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:
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.
可以尝试以下几件事(不建议将控制器传递到表单中或从表单内部实例化):
A couple of things to try (passing a controller into a form or instantiating from within one is not recommended):