Search code examples
symfony1symfony-1.4symfony-forms

saving embeded form values based on user session data


I have a form that has many embedded forms, which are select lists.

What I'm ideally looking to do, is to save the select list values.

I can get the form values by using:

$this->form->getValues()

var_dumping the above gives me something like:

    array
      'fields' => 
        array
          'Field' => 
            array
              'field' => string 'field' (length=7)
              'column' => int 0
              'id' => null
          'Field 1' => 
            array
              'field' => string 'field_1' (length=7)
              'column' => int 1
              'id' => null
          'Field 2' => 
            array
              'field' => string 'field_2' (length=19)
              'column' => int 10
              'id' => null
      'id' => string '51' (length=2)

I've then set the form values in the user session:

$this->getUser()->getAttribute('something', $this->form->getValues());

I'm jsut wondering how I'd then set the select lists based on these saved values?

Thanks

EDIT:

In my actions.class.php i added the following after the $this->form-save()

$this->getUser()->setAttribute('user_fields', $this->form->getValues());

Then within my configure() method in my form class, imply used:

    if ($savedValues = sfContext::getInstance()->getUser()->getAttribute('user_fields'))
    {
        $this->setDefaults($savedValues);
    }

And it worked perfectly!!

Thanks


Solution

  • In the action after the form is created (and before bind() is called) you can do this:

    $this->form = new YourForm();
    
    if ($savedValues = $this->getUser()->getAttribute('something'))
    {
      $this->form->setDefaults($savedValues);
    }
    
    // ...