Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Forms 如何在Symfony 2表单字段中将FOSUserBundle中经过身份验证的用户名设置为默认值_Forms_Symfony_Fosuserbundle - Fatal编程技术网

Forms 如何在Symfony 2表单字段中将FOSUserBundle中经过身份验证的用户名设置为默认值

Forms 如何在Symfony 2表单字段中将FOSUserBundle中经过身份验证的用户名设置为默认值,forms,symfony,fosuserbundle,Forms,Symfony,Fosuserbundle,我发现在创建表单时,在表单中放入一个默认值确实很有用 $builder ->add('myfield', 'text', array( 'label' => 'Field', 'data' => 'Default value')) ; 如果我想用FOSUser捆绑包中经过身份验证的人员替换“默认值”,该怎么办?(已授予“真实”返回(“已认证”返回) 我可以用 {{ app.user.username }} 我也在控制器方法中使用 $us

我发现在创建表单时,在表单中放入一个默认值确实很有用

 $builder
 ->add('myfield', 'text', array(
     'label' => 'Field',
     'data' => 'Default value'))
 ;
如果我想用FOSUser捆绑包中经过身份验证的人员替换“默认值”,该怎么办?(已授予“真实”返回(“已认证”返回)

我可以用

 {{ app.user.username }} 
我也在控制器方法中使用

    $username=$this->container->get('security.context')->getToken()->getUser()->getUsername()   
但我无法使它在我的状态下工作

我不确定我是否理解容器的事情…也不知道如何在类和控制器之间传递变量。。。 也许是关于这个的

      ->add('myfield', 'text', array(
     'label' => 'Field',
     'data' => FOS\UserBundle\Model::$this->getUsername()))

您可以将变量从控制器传递到窗体:

protected $username;

public function __construct (array $username = null)
{
        $this->username = $username ;
}

public function buildForm(FormBuilder $builder, array $options)
{
   $builder  
     ->add('myfield', 'text', array(
      'label' => 'Field',
      'data' => $this->username))
}
在控制器中:

$username=$this->container->get('security.context')->getToken()->getUser()->getUsername()
$form = $this->createForm(new MyFormType($username), $entity);
以您的形式:

protected $username;

public function __construct (array $username = null)
{
        $this->username = $username ;
}

public function buildForm(FormBuilder $builder, array $options)
{
   $builder  
     ->add('myfield', 'text', array(
      'label' => 'Field',
      'data' => $this->username))
}

您可以将变量从控制器传递到窗体:

protected $username;

public function __construct (array $username = null)
{
        $this->username = $username ;
}

public function buildForm(FormBuilder $builder, array $options)
{
   $builder  
     ->add('myfield', 'text', array(
      'label' => 'Field',
      'data' => $this->username))
}
在控制器中:

$username=$this->container->get('security.context')->getToken()->getUser()->getUsername()
$form = $this->createForm(new MyFormType($username), $entity);
以您的形式:

protected $username;

public function __construct (array $username = null)
{
        $this->username = $username ;
}

public function buildForm(FormBuilder $builder, array $options)
{
   $builder  
     ->add('myfield', 'text', array(
      'label' => 'Field',
      'data' => $this->username))
}

在表单中设置默认值的另一种方法是在表单的基础数据对象上设置默认值,如Symfony文档中关于构建表单的示例所示:

public function newAction()
{
    // create a task and give it some dummy data for this example
    $task = new Task();
    $task->setTask('Write a blog post');
    $task->setDueDate(new \DateTime('tomorrow'));

    $form = $this->createFormBuilder($task)
        ->add('task', 'text')
        ->add('dueDate', 'date')
        ->getForm();

    return $this->render('AcmeTaskBundle:Default:new.html.twig', array(
        'form' => $form->createView(),
    ));
}
在本例中,表单的基础数据对象是任务,任务上设置的值是要在表单中显示的默认值。任务对象不是持久的。这种方法同样适用于表单类,并且假设表单的底层对象是用户,则如下所示:


这种方法的缺点是,如果表单在许多需要相同默认值的地方使用,代码就会重复。到目前为止,我还没有遇到过这种情况-我的用户表单被重新用于创建/编辑,但编辑不需要默认值。

将默认值设置到表单中的另一种方法是在表单的基础数据对象上设置默认值,如Symfony文档中关于构建表单的示例所示:

public function newAction()
{
    // create a task and give it some dummy data for this example
    $task = new Task();
    $task->setTask('Write a blog post');
    $task->setDueDate(new \DateTime('tomorrow'));

    $form = $this->createFormBuilder($task)
        ->add('task', 'text')
        ->add('dueDate', 'date')
        ->getForm();

    return $this->render('AcmeTaskBundle:Default:new.html.twig', array(
        'form' => $form->createView(),
    ));
}
在本例中,表单的基础数据对象是任务,任务上设置的值是要在表单中显示的默认值。任务对象不是持久的。这种方法同样适用于表单类,并且假设表单的底层对象是用户,则如下所示:

这种方法的缺点是,如果表单在许多需要相同默认值的地方使用,代码就会重复。到目前为止,我还没有遇到过这种情况——我的用户表单被重新用于创建/编辑,但编辑不需要默认值