+ Reply to Thread
Results 1 to 7 of 7

Thread: How to pass a variable to form?

  1. #1
    risoknop is offline 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?

  2. #2
    Tekerson is offline 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]
    Last edited by Tekerson; 01-31-2009 at 11:27 PM. Reason: Wasn't finished
    Brenton Alker
    PHP Developer - Brisbane, Australia

    blog.tekerson.com | twitter.com/tekerson | brenton.mp

  3. #3
    risoknop is offline Junior Member
    Join Date
    Jan 2009
    Posts
    26

    Default

    Thanks for help

  4. #4
    risoknop is offline 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...).

  5. #5
    Tekerson is offline 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

  6. #6
    Nitecon is offline Junior Member
    Join Date
    Nov 2008
    Posts
    19

    Default

    what I generally have in my controllers is something like this...
    '
    Code:
    $form = new Default_Form_ChartAdd();
    $formData = $this->_request->getPost();
    			if ($form->isValid($formData)){
    ...
    ...
    }else{
    // The form is not displayed yet so I can add values to the form from the db here.
    
    $db = new Default_Model_SomeModel;
    $test = $db->fetchRow("id=1");
    // Where name is <input type... name="name" />
    // And $this->view->form is echod in the view as $this->form;
    $this->view->form->name->setValue( $test->name );
    }
    Last edited by Nitecon; 04-14-2010 at 12:43 AM.

  7. #7
    Nitecon is offline Junior Member
    Join Date
    Nov 2008
    Posts
    19

    Default

    Err yeah disregard my previous post it's not really valid to this question... will teach me for posting on my phone and not reading the question completely!
    Last edited by Nitecon; 04-14-2010 at 12:44 AM.

+ Reply to Thread

Similar Threads

  1. How can i pass a variable while using zend_paginator
    By anees_muhd in forum Core Infrastructure
    Replies: 9
    Last Post: 06-23-2010, 06:36 PM
  2. pass data to form's init()?
    By sims in forum Model-View-Controller (MVC)
    Replies: 7
    Last Post: 03-03-2010, 12:11 AM
  3. pass a variable into a partialloop
    By AceWebDesign in forum Model-View-Controller (MVC)
    Replies: 0
    Last Post: 01-05-2009, 10:16 PM
  4. How to pass a variable to a form
    By AceWebDesign in forum Model-View-Controller (MVC)
    Replies: 6
    Last Post: 12-16-2008, 06:43 PM
  5. pass variable from db to form
    By AceWebDesign in forum General Q&A on Zend Framework
    Replies: 7
    Last Post: 10-24-2008, 10:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts