![]() |
|
|||
|
Hello,
What I need to do: pass a variable to form (so I can use it inside form class). This is how most of my form cl***** look like: [php] class FormName extends Zend_Form { public function init() { // something here } } [/php] And when I do: [php] include APPLICATION_PATH . '/forms/FormName.php'; $form = new FormName($var); [/php] I need the $var to be accessible inside the form class. I tried adding the var inside init() method as well as creating a constructor and putting it there but to no success. Any suggestions? |
|
|||
|
The first (only) parameter to the contructor of a form is an options array, which map to set* functions, so you should be able to do this (untested)
[php] $form = new FormName(array('myOption' => 'Some Value')); class FormName extends Zend_Form { protected $_myOption; public function init() { $myVar = $this->_myOption; } public function setMyOption($value) { $this->_myOption = $value; } } [/php]
__________________
Brenton Alker PHP Developer - Brisbane, Australia blog.tekerson.com | twitter.com/tekerson | brenton.mp Last edited by Tekerson; 02-01-2009 at 12:27 AM. Reason: Wasn't finished |
|
|||
|
Actually, upon further investigation I found out it doesn't work. The variable $myVar is null even though I have passed a value (I checked it with var_dump()).
To clarify, I need the variable inside the form to build a multicheckbox (basically I want to pass an array of categories to form and build multicheckbox for each category inside the form class...). |
|
|||
|
I have to play the "it works for me" card. I just put the code I posted above into an app and it worked exactly as expected (I var_dump($myVar) in the init, and got "Some Value").
One thing I can think it might be is, if you changed the key in the options array to one of the "forbidden" values (I'm not sure if they are documented anywhere, I read the code). From Zend/Form.php: [php] $forbidden = array( 'Options', 'Config', 'PluginLoader', 'SubForms', 'View', 'Translator', 'Attrib', 'Default', ); [/php] So if you used array('options' => ...) or any of the others in the list, it wouldn't work.
__________________
Brenton Alker PHP Developer - Brisbane, Australia blog.tekerson.com | twitter.com/tekerson | brenton.mp |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
| Designed by: Miner Skinz |
Powered by vBulletin® Version 3.8.4 Copyright ©2000 - 2010, Jelsoft Enterprises Ltd. Search Engine Friendly URLs by vBSEO 3.1.0 |
![]() |