How to Use the Zend Form ViewScript Decorator In All Modules
If you’ve been using Zend Forms for any length of time, you’ll know just how flexible and configurable they are. Whether you’re initialising them in code or via Zend_Config with one of the adapters Zend_Config_Xml, Zend_Config_Ini, Zend_Config_Json, Zend_Config_Yaml. There’s not much that you can’t do with them, besides link them to a database model for validation - but that’s another story.
What’s more, we can, as I’m quite fond of, use the ViewScript decorator. This allows us to have nearly 100% control of the configuration of the look and feel of our rendered forms. Take the following script template, of a user profile form, as an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
You can see that we can layout forms exactly where and when we want them. There is a bit of a catch that we have to remember all the options that we want to display, but that’s not really an overarching concern.
But buyer beware, there is a catch. Depending on how long you’ve been using them you may or may not have come across the situation where involving the viewScript decorator. That is, it’s alright when the form’s being rendered in the module that it’s created in, but in any other module, it fails to find the view template.
What’s The Solution
If you’ve come across this situation, there is a solution. When you’re using the ViewScript decorator, pass the viewModule parameter as an argument when initialising it.
Similar to rendering partials within other view template scripts, the viewModule parameter provides a module-specific path for finding view script templates. Have a look at the following example to see just how simple it is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
We have now specified that the viewScript template will be under application/module/default/forms/. So irrespective of where in the application this form is initialised, if the template is in that location, the form will render with it correctly.
Having troubles with the viewScript decorator? Put your thoughts in the comments.
Image courtesy: kiler129
You might also be interested in...
- Why Zend Framework Plugins Save You Time
- Zend Form Mastery with Zend Config – Part 4 Configuring Zend Validators
- Zend Form Mastery with Zend Config - Part 1 Custom Filter Paths
- Zend Framework 2 Modules - The Application's Heart
- How to Build Extendable and Extensible Zend Framework Applications with Custom Resource Plugins
comments powered by Disqus