7

我在标准的 Propel 表单类中使用 Symfony 1.2。

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

但是,我希望此字段的值来自用户信息,我通常使用$this->getUser()->getAttribute( '...' ). 但是,这似乎不适用于表格。

我应该使用什么?

4

4 回答 4

14

依赖 sfContext 实例是一个非常糟糕的主意。最好在 options 数组参数中传递 sfForm 初始化期间所需的内容。

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

__construct 方法

例如在你的行动中:

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

然后检索表单类中的值:

$this->getOption('attributeFoo');

环波

于 2010-05-27T11:49:41.507 回答
6

那样有用吗?

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

// 编辑:参见 cirpo 关于使用 sfContext 的建议。

于 2009-03-09T19:25:53.513 回答
1

如果有人在管理员(后端)中需要相同的解决方案:http: //blog.nevalon.de/en/wie-nutze-ich-die-rechteverwaltung-in-symfony-admin-generator-formularen-20100729

于 2011-03-18T08:42:16.423 回答
-1

在 Symfony 1.4 中,对象$sf_user

于 2016-02-08T12:32:19.560 回答