handleRequest之后的Symfony2 DataTransformer

时间:2015-05-24 11:38:40

标签: php symfony

我有一个API而且我发送了一个实体的引用,我使用DataTransformer来获取我的实体,但是在$form->handleRequest($request)之前始终调用DataTransformer,该值始终为null并且它可以& #39; t工作

我的控制器

public function newAction(Request $request)
    {
        $orderNewFormType = $this->get('competitive_bo.api_bundle.form.type.order_new');
        $card = new Card();

        try {
            $form = $this->createForm($orderNewFormType, $card);
            $form->handleRequest($request);
        } catch (TransformationFailedException $e) {
            return $this->notFoundErrorResponse(
                'Business not found'
            );
        }

        if ($form->isValid()) {
            return $this->okResponse(array());
        }

        $validatorErrorFormatter = $this->get('competitive_bo.api_bundle.formatter.validator_error');
        $errors = $validatorErrorFormatter->formatFromFormError($form->getErrors(true));

        return $this->badRequestErrorResponse(
            'Invalid data',
            $errors
        );
    }

表单类型

class OrderNewFormType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name')
            ->add('customer', 'entity', array(
                'class' => 'CompetitiveBOBusinessBundle:Customer',
                'property' => 'id'
            ))
            ->add('business', 'business', array(
                'mapped' => false,
            ))
        ;
    }

    /**
     * {@inheritdoc}
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'csrf_protection'   => false,
            'data_class'        => Card::class
        ));
    }

    public function getName()
    {
        return null;
    }
}

业务表单类型

class BusinessReferenceFormType extends AbstractType
{
    /**
     * @var ReferenceToBusinessTransformer
     */
    private $referenceToBusinessTransformer;

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

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->addViewTransformer($this->referenceToBusinessTransformer);
    }

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

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

和DataTransformer

/**
 * Class ReferenceToBusinessTransformer
 */
class ReferenceToBusinessTransformer implements DataTransformerInterface
{
    /**
     * @var BusinessRepository
     */
    private $businessRepository;

    public function __construct(BusinessRepository $businessRepository)
    {

        $this->businessRepository = $businessRepository;
    }

    /**
     * {@inheritdoc}
     */
    public function transform($reference)
    {
        var_dump($reference);
        $business = $this->businessRepository->findOneBy(array(
            'reference' => $reference
        ));

        if (null === $business) {
            throw new TransformationFailedException;
        }

        return $business;
    }

    /**
     * {@inheritdoc}
     */
    public function reverseTransform($value)
    {
        if (!($value instanceof Business)) {
            throw new TransformationFailedException;
        }

        return $value->getReference();
    }
}

var_dump($reference)始终为空

我有我的考试

public function testNewAction($getParams, $postParam, $responseCode)
    {
        $client = static::createClient();

        $router = $client->getContainer()->get('router');
        $route = $router->generate('competitivebo_api_order_new',$getParams);

        $client->request('POST', $route, $postParam);
        $response = $client->getResponse();

        $this->assertJsonResponse($response, $responseCode);
    }

使用post params

'customer' => 1,
'business' => LoadBusinessData::REFERENCE_1,
'name' => 'Test',

TransformationFailedException期间始终抛出异常$this->createForm(...),因此请求未得到处理

1 个答案:

答案 0 :(得分:2)

根据documentation

  

当null传递给transform()方法时,你的变换器应该返回它正在转换的类型的等价值(例如一个空字符串,0表示整数或0.0表示浮点数)。