如果用户是AUTHENTICATED,则为表单输入提供值

时间:2015-01-02 14:33:28

标签: php symfony doctrine-orm

已更新

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

我在这里找到了一个解决方案symfony.com/doc/2.5/cookbook/form/dynamic_form_modification.html它非常适用当用户是AUTHENTICATED但是当他没有AUTHENTICATED时它会给我错误。

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

FormType:

<?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);

        }
    );
}

1 个答案:

答案 0 :(得分:0)

我找到了解决方案:

我认为不是要使用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);

    //......

}