Symfony, forms and widgets tips

Howto get the user object in a form

Solution to get to user object in an sfForm object: use the 2nd $option parameter while creating the form object and retrieve it.

// In your actions.class.php
$this->form = new myForm(array(), array("currentUser" => $this->getUser()));

// In your form class

// ...
if (!is_object($this->getOption("currentUser")) || !($this->getOption("currentUser") instanceof sfUser))
{
throw new InvalidArgumentException("You must pass a user object as an option to this form!);
}

Source: http://eatmymonkeydust.com/2009/08/symfony-forms-flexible-widgets-based-on-user-credentials/

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *