Search code examples
formssymfony-1.4propel

Pass a variable to a form using Symfony 1.4 and Propel


I need to pass a variable from an action class to a form using Symfony 1.4 and Propel.

I have tried the method listed here: Pass a variable to a Symfony Form which I believe is a method that works with Doctrine, but not Propel.

This is what I've tried:

action class:

$this->saved_cart_form = new ItemSavedCartForm(array(), array('user_id' => $user_id));

form (line to grab variable):

$this->getOption('user_id');

This hasn't worked, and there isn't much documentaion, but I believe you need to pass options through the forms constructor function, which I don't know how to do.


Solution

  • I solved this problem by adding a constructor to the form and passing the variable to it through the action.

    Action:

    $this->saved_cart_form = new ItemSavedCartForm($this->getUser()->getUserId());
    

    Form:

    class ItemSavedCartForm extends myFormHorizontal
    {
    
        protected $user_id = null;
    
    
        public function __construct($user_id = null)
        {
        $this->user_id = $user_id;
    
        parent::__construct();
    
        //rest of class...
    }
    

    Afterwards, $this->user_id is available throughout the form class. If anyone can find documentation or articles about other ways to do this through Symfony 1.4 and Propel, please share - I'd be very interested to see other solutions.