如果用户经过身份验证,则为表单输入赋值

Give a value to form input if user is AUTHENTICATED

已更新

我有一个联系表单,如果用户是 AUTHENTICATED,我希望为表单中的某些输入赋值。例如电子邮件、电话、姓名...如果他是不是那么给空值。

我在这里找到了一个解决方案 symfony.com/doc/2.5/cookbook/form/dynamic_form_modification.html当用户被授权时它工作得很好 但当他没有被授权时它会给我错误。

Error: Call to a member function getName() on a non-object

表格类型:

<?php
namespace Project\AgenceBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Security\Core\SecurityContext;
use Doctrine\ORM\EntityRepository;

class AgenceContactType extends AbstractType
{

private $securityContext;

public function __construct(SecurityContext $securityContext)
{
    $this->securityContext = $securityContext;
}

public function buildForm(FormBuilderInterface $builder, array $options)
{

    $builder
        ->add('telephone', 'text', array(
            'required'  => false,
            'attr' => array('class' =>'input-text full-width'),
        ))
        ->add('subject', 'text', array(
            'required' => true,
            'attr' => array('class' =>'input-text full-width'),
        ))
        ->add('message', 'textarea', array(
            'required'  => true,
            'attr' => array('class' =>'input-text full-width', 'rows' =>'6'),
        ))
    ;

    $user = $this->securityContext->getToken()->getUser();

    $builder->addEventListener(
        FormEvents::PRE_SET_DATA,
        function(FormEvent $event) use ($user) {
            $form = $event->getForm();
            if ($user) {
            $name = $user->getName(); $surname = $user->getSurname(); $email = $user->getEmail();
            }
            else {
            $name = ""; $surname = ""; $email = "";
            }

            $nameOptions = array(
                'required' => true,
                'attr' => array('class' =>'input-text full-width','value' =>$name),
            );
            $form->add('name', 'text', $nameOptions);

            $surnameOptions = array(
                'required' => true,
                'attr' => array('class' =>'input-text full-width','value' =>$surname),
            );
            $form->add('surname', 'text', $surnameOptions);

            $emailOptions = array(
                'required' => true,
                'attr' => array('class' =>'input-text full-width','value' =>$email),
            );
            $form->add('email', 'text', $emailOptions);

        }
    );
}

我找到了解决办法:

我觉得不是用Events来做这样的事情,所以我把代码彻底改了。 我已经测试过了,效果很好。 这是新代码,你能给我意见吗?

<?php
//....
use Symfony\Component\Security\Core\SecurityContext; //SecurityContext

class AgenceContactType extends AbstractType
{

// Inject a service inside of the form
private $securityContext;

public function __construct(SecurityContext $securityContext)
{
    $this->securityContext = $securityContext;
}


public function buildForm(FormBuilderInterface $builder, array $options)
{

    // Testing if user is AUTHENTICATED
    if ($this->securityContext->isGranted('IS_AUTHENTICATED_REMEMBERED')){
        $user = $this->securityContext->getToken()->getUser();
        $name = $user->getName(); $surname = $user->getSurname(); $email = $user->getEmail();
    }
    else {
        $name = ""; $surname = ""; $email = "";
    }

    $builder
        ->add('name', 'text', array(
            'required' => true,
            'attr' => array('class' =>'input-text full-width','value' =>$name),
        ))
        ->add('surname', 'text', array(
            'required' => true,
            'attr' => array('class' =>'input-text full-width','value' =>$surname),
        ))
        ->add('email', 'email', array(
            'required'  => true,
            'attr' => array('class' =>'input-text full-width','value' =>$email),
        ))
        ->add('telephone', 'text', array(
            'required'  => false,
            'attr' => array('class' =>'input-text full-width'),
        ))
        ->add('subject', 'text', array(
            'required' => true,
            'attr' => array('class' =>'input-text full-width'),
        ))
        ->add('message', 'textarea', array(
            'required'  => true,
            'attr' => array('class' =>'input-text full-width', 'rows' =>'6'),
        ))
    ;

}

//..........
}

控制器:

public function viewAction(Agence $agence, $slug, Request $request)
{
    // pass $securityContext in the form
    $securityContext = $this->container->get('security.context');
    $form_contact = $this->createForm(new AgenceContactType($securityContext), $agenceContact);

    //......

}