7

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?

4 Answers 4

14

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

Sign up to request clarification or add additional context in comments.

2 Comments

What if you are using a plugin like sfDoctrineGuard which autogenerates the forms? I am not directly creating the forms in my own classes.
It was what I was looking for. Thanks! In my case it work with Symfony 1.4
6

Does that work?

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

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

Comments

1

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

Comments

-1

In Symfony 1.4, object $sf_user

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.