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/

03. février 2010 par Saad Tazi
Catégories: programmation, Symfony | Tags: , | Laisser un commentaire

Laisser un commentaire

Les champs obligatoires sont indiqués avec *

*