Zend Framework Forum

Go Back   Zend Framework Forum > Zend Framework Forum > General Talks

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-31-2009, 10:53 PM
Junior Member
 
Join Date: Jan 2009
Posts: 26
Default How to pass a variable to form?

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?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-01-2009, 12:25 AM
Senior Member
 
Join Date: Jul 2008
Posts: 288
Default

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-01-2009, 12:40 PM
Junior Member
 
Join Date: Jan 2009
Posts: 26
Default

Thanks for help
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-01-2009, 02:03 PM
Junior Member
 
Join Date: Jan 2009
Posts: 26
Default

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...).
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-01-2009, 08:56 PM
Senior Member
 
Join Date: Jul 2008
Posts: 288
Default

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT. The time now is 06:09 PM.


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