Tofuwarrior
Tofuwarrior

Reputation: 669

How to access the user object in the form configure method

I'm currently doing this kind fo thing with symfony forms

$this->myForm = new MyForm();
$this->myForm->customConfigureMethod($this->getUser()->getGuardUser());

because I need to configure a DoctriineChoice widget on the basis of the user.

I would rather do this kind of thing

$this->myForm =new myCustomConfiguredForm($this->getUser()->getGuardUser());

With the customisation being part of the form instantiation.

Anyone know how I could achieve this? I think I might be a bit unclear about the difference between the configure() and setup() functions for the forms so can't think clearly about it.

Upvotes: 0

Views: 50

Answers (1)

1ed
1ed

Reputation: 3668

You shpuld pass the user object as an option. Here is an exapmle:

class ProductForm extends BaseProductForm
{
  public function configure()
  { 
    // or use an instance variable if you need the user in an another method too
    $user = $this->getOption('user');

    if (!$user instanceof sfBasicSecurityUser)
    {
      throw new InvalidArgumentException('A user object is required as "user" option in ' . __METHOD__);
    }

    // do something with the user...
  }

}

$form = new ProductForm(array(), array('user' => $this->getUser()));

Upvotes: 1

Related Questions