Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get user data in form in Symfony 1.2?

Tags:

php

symfony1

I'm using Symfony 1.2 in a standard Propel form class.

public function configure()
{
    $this->setWidgets(array( 
'graduate_job_title' => new sfWidgetFormInput( array(), array( 'maxlength' => 80, 'size' => 30, 'value' => '' ) )
    ));
    //etc
}

However, I want the value of this field to come from the user information, which I'd normally access using $this->getUser()->getAttribute( '...' ). However, this doesn't seem to work in the form.

What should I be using?

like image 312
James Inman Avatar asked Mar 09 '09 19:03

James Inman


3 Answers

It's a very bad idea to rely on the sfContext instance. It's better to pass what you need during sfForm initialization in the options array parameter.

http://www.symfony-project.org/api/1_4/sfForm

__contruct method

for example in your action:

$form = new myForm(null, 
                   array('attributeFoo' => 
                         $this->getUser()->getAttribute('attributeFoo'));

and then retrieve the value inside the form class:

$this->getOption('attributeFoo');

cirpo

like image 70
cirpo Avatar answered Sep 20 '22 01:09

cirpo


Does that work?

sfContext::getInstance()->getUser()->getAttribute('...');

// Edit : See cirpo's recommandation on the use of sfContext instead.

like image 32
lpfavreau Avatar answered Sep 21 '22 01:09

lpfavreau


If someone need the same in admin (backend) here is a solution: http://blog.nevalon.de/en/wie-nutze-ich-die-rechteverwaltung-in-symfony-admin-generator-formularen-20100729

like image 30
Jérôme Avatar answered Sep 21 '22 01:09

Jérôme