Symfony2 DataTransformer中的访问请求参数

时间:2015-01-13 10:45:27

标签: php forms symfony

是否可以从DataTransformer中的另一个表单字段获取字段的值?

我可以创建一个链接到电子邮件的邀请,然后当用户注册他必须输入邀请代码时,即使该代码没有链接到他输入的电子邮件,它也会起作用,因为{{1} } field是Invitation,用于检查DB中的值。如果电子邮件存在,我想在查询中查看。

的MainForm

DataTransformer

public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('invitation', 'invitation_type', array( 'required' => true, 'label' => false, 'attr' => array( 'placeholder' => 'form.invitation_code', 'class' => 'form-control', ) )) ; } 是一个服务,我将entityManager注入一个呈现dataTransformer的字段类型:

InvitationType

invitation_type

然后是变压器

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $transformer = new InvitationTransformer($this->entityManager);
    $builder->addModelTransformer($transformer);
}

这是查询,正如我所说的那样,在没有查看电子邮件价值的情况下工作,它会像public function reverseTransform($value) { $invitation = $this->entityManager->getRepository('Invitation') ->findOneBy(array( 'code' => $value )); return $invitation; } ,但我不知道如何访问'email' => $emailValue

2 个答案:

答案 0 :(得分:0)

您可以将值注入变压器

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $entity = $builder->getData();
    $email = $entity->getEmail();

    $transformer = new MyTransformer($email);

    $builder->add(
        $builder->create('sample', 'choice', array(
            'attr'     => array('class' => 'test')
        ))->addModelTransformer($transformer)
    )

}

class MyTransformer implements DataTransformerInterface {

    private $emailValue;

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

    public function reverseTransform($value)
    {

        // Do something with $this->emailValue;

        $invitation = $this->entityManager->getRepository('Invitation')
            ->findOneBy(array('code' => $value));

        return $invitation;
    }

}

答案 1 :(得分:0)

所以如果有人遇到同样的问题,我找到了解决方案。

首先为自定义字段类型创建服务,注入@request_stack

foo.form.type.invitation:
    class: Foo\BarBundle\Form\Type\InvitationType
    arguments: [ "@doctrine.orm.entity_manager" ]
    tags:
        - { name: form.type, alias: invitation_type}
    calls:
        - [setRequest, [@request_stack]]

然后创建自定义字段类型类,它会将Request注入DataTransformer

<?php

namespace Foo\BarBundle\Form\Type;
use Doctrine\ORM\EntityManager;
use Foo\BarBundle\Form\DataTransformer\InvitationTransformer;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\HttpFoundation\RequestStack;

class InvitationType extends AbstractType
{
    private $entityManager;

    protected $request;

    public function __construct(EntityManager $em)
    {
        $this->entityManager = $em;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $transformer = new InvitationTransformer($this->entityManager, $this->request);
        $builder->addModelTransformer($transformer);
    }

    public function getParent()
    {
        return 'text';
    }

    public function getName()
    {
        return 'invitation_type';
    }

    public function setRequest(RequestStack $request_stack)
    {
        $this->request = $request_stack->getCurrentRequest();
    }
}

然后我们DataTransformer需要获取数据,更改值以匹配您的请求,当然

<?php
namespace Foo\BarBundle\Form\DataTransformer;

use Doctrine\ORM\EntityManager;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\HttpFoundation\Request;

class InvitationTransformer implements DataTransformerInterface
{
    protected $entityManager;
    protected $request;

    public function __construct(EntityManager $entityManager, Request $request)
    {
        $this->entityManager    = $entityManager;
        $this->request          = $request;
    }

    public function transform($value)
    {
        if (null === $value) {
            return null;
        }

        if (!$value instanceof Invitation) {
            throw new UnexpectedTypeException($value, 'Foo\BarBundle\Entity\Invitation');
        }

        return $value->getCode();
    }

    public function reverseTransform($value)
    {
        if (null === $value || '' === $value) {
            return null;
        }

        if (!is_string($value)) {
            throw new UnexpectedTypeException($value, 'string');
        }
        $formData = $this->request->get('registration_form'); // Your MAIN form goes here
        $email = $formData['email']; // The value you need
        $invitation = $this->entityManager->getRepository('FooBarBundle:Invitation')
            ->findOneBy(array(
                'code'  => $value,
                'email' => $email
            ));

        if($this->entityManager->getRepository('FooBarBundle:User')->findOneBy(array("invitation" => $invitation))){
            return null;
        }

        return $invitation;
    }
}

现在,您可以访问DataTransformer内的参数包,这是一块蛋糕。