使用子表单类型时,Form-> handleRequest将数据设置为null

时间:2016-09-16 11:28:24

标签: php forms symfony

Symfony版本:2.8.6

我尝试根据父字段类型中的选择更新表单字段。但是当我$form->handleRequest($request)时,所有数据都是空的。

我主要尝试遵循本指南:Dynamic Generation for Submitted Forms

ArticleType.php

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $updateRoute = $this->router->generate('appbundle_article_update_form');

    $builder
        ->add('name', 'text', [
            'required' => false,
        ])
        ->add('description', 'textarea', [
            'required' => false,
        ])
        ->add('ajax_update_action', 'hidden', [
            'data' => $updateRoute,
            'mapped' => false,
        ])
        ->add('type', ChoiceType::class, [
            'choices' => [
                'text' => 'Text',
                'textarea' => 'Multiline',
                'choice' => 'Choice',
            ],
        ])
        ->add('parameters', OptionsType::class)
    ;
}

OptionsType.php

如果ArticleType中的type设置为' choice',则应添加3个额外字段(他们会这样做)。

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('label', 'text', [
            'required' => false,
        ])
        ->add('required', 'checkbox', [
            'required' => false,
        ]);
    ;

    $builder->addEventListener(
        FormEvents::POST_SET_DATA,
        function (FormEvent $event) {

            $form = $event->getForm();
            $parentData = $event->getForm()->getParent()->getData();

            if ($parentData->getType() == "choice") {

                if(!$form->has('multiple')) {
                    $form->add('multiple', 'checkbox', [
                        'required' => false,
                    ]);
                }
                if(!$form->has('extended')) {
                    $form->add('extended', 'checkbox', [
                        'required' => false,
                    ]);
                }
                if(!$form->has('choices')) {
                    $form->add('choices', 'text', [
                        'required' => false,
                        'attr' => [
                            'help_block' => 'Provide a comma separerated list',
                        ],
                    ]);
                }

            }else {
                foreach(['multiple', 'extended', 'choices'] as $field_name) {
                    if($form->has($field_name)) {
                        $form->remove($field_name);
                    }
                }
            }
        }
    );
}

文章/ edit.html.twig: 非常简单,我们有一个自定义表单来更新表单(没有坚持!)

{{ form(form) }}

<script>

$( document ).ready(function () {
    $('select#article_type').on(
        'change',
        this,
        function() {
            var $form = $(document).find('form[name=article]'),
                $url = $form.find('#article_refresh_action').val(),
                $data = $form.serialize();

            $.ajax({
                url: $url,
                type: 'POST',
                data: $data,
                success: function (html) {
                $form.find('#article_parameters').replaceWith($(html).find('#article_parameters'));
            }
            });
        }
    );
});

</script>

ArticleController.php: 请注意,我不想将数据持久保存到数据库,只需更新表单,然后将其返回。

/**
 * @param Request $request
 *
 * @Route("/article/updateForm", name="appbundle_article_update_form")
 * @Method({"PUT", "POST"})
 *
 * @return Response
 */
public function updateFormAction(Request $request)
{
    $form = $this->createForm(
        $this->get('appbundle.form.type.article'),
        new Article()
    );

    // When i do a `dump($request->request->all())` here, I'm given all the correct data in an array

    $form->handleRequest($request);

    // When i do `dump($form->getData());` here, all the fields are null, which makes the form return blank.

    return $this->render(
        'AppBundle:Article:edit.html.twig', [
            'form' => $form->createView(),
        ]
    );
}

Article.php 实体:

/**
 * Storing it as json.
 *
 * @var string
 *
 * @ORM\Column(type="text", nullable=true)
 */
protected $parameters;

/**
 * Set parameters.
 *
 * @param array $parameters
 *
 * @return Article
 */
public function setParameters($parameters)
{
    $this->parameters = json_encode($parameters);

    return $this;
}

/**
 * Get parameters.
 *
 * @return array
 */
public function getParameters()
{
    return json_decode($this->parameters, true);
}

因此在handleRequest之后,我的所有字段都被清空,日志中没有任何异常或通知。这是一个错误,还是我只是以错误的方式做这个?

0 个答案:

没有答案