Craue表单流在最后一步中在$ flow-> isValid()上返回false

时间:2015-02-19 10:08:08

标签: php forms symfony craueformflow

我使用CraueFormFlowBundle创建了表单向导,直到最后一步,$ flow-> isValid()失败并且它不断渲染最后一步而不是转到所需的表单向导操作结束。

以下是了解我犯错误所需的代码:

控制器:

public function indexAction(Activity $activity)
{
    $currency = $this->getDoctrine()->getRepository('MycompanyUtilBundle:UtilCurrency')->find(1);

    $booking = new Booking();
    $booking->setActivity($activity)
        ->setCurrency($currency)
        ->setReferenceCode(uniqid())
        ->setUserAccount($this->getDoctrine()->getRepository('MycompanySettingsBundle:UserAccount')->find(1));

    $flow = $this->get('mycompany.form.flow.Booking');
    $flow->bind($booking);

    $form = $flow->createForm();
    if ($flow->isValid($form)) {
        $flow->saveCurrentStepData($form);

        if ($flow->nextStep()) {
            $form = $flow->createForm();
        } else {
            return new JsonResponse(['Status' => 'Form is valid']);
        }
    }

    return $this->render(
        '@MycompanyDemoBundle/Default/booking.flow.html.twig',
        [
            'form' => $form->createView(),
            'flow' => $flow,
            'activity' => $activity->getTitle(),
            'formData' => $booking,
            'error' => $form->getErrors(true, true),
        ]
    );
}

services.yml:

mycompany.form.Booking:
    class: mycompany\BookingBundle\Form\BookingType
    tags:
        - { name: form.type, alias: mycompany_bookingbundle_booking_form }
mycompany.form.flow.Booking:
    class: mycompany\BookingBundle\Form\BookingFlow
    parent: craue.form.flow
    scope: request
    calls:
        - [ setFormType, [ @mycompany.form.Booking ] ]

BookingType.php:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    switch ($options['flow_step']) {
        case 1:
            $builder
                ->add(
                    'activity',
                    'entity',
                    [
                        'class' => 'Mycompany\ActivityBundle\Entity\Activity',
                        'property' => 'title',
                    ]
                )
                ->add(
                    'scheduledDeparture',
                    'entity',
                    [
                        'class' => 'Mycompany\ActivityBundle\Entity\ActivityScheduledDeparture',
                        'property' => 'departureDateTimeString',
                        'empty_value' => 'Select departure time',
                    ]
                )
                ->add(
                    'payer',
                    new CrmContactType()
                );
            break;
        case 2:
            $builder
                ->add(
                    'numberOfAdults',
                    'choice',
                    [
                        'choices' => range(1, 5),
                        'empty_value' => 'Select number of adult travellers'
                    ]
                )
                ->add(
                    'numberOfChildren',
                    'choice',
                    [
                        'choices' => range(1, 5),
                        'empty_value' => 'Select number of child travellers'
                    ]
                );
    }
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(
        [
            'data_class' => 'Mycompany\BookingBundle\Entity\Booking',
        ]
    );
}

BookingFlow:

protected function loadStepsConfig()
{
    return
        [
            [
                'label' => 'Select tour',
                'type' => $this->formType,
            ],
            [
                'label' => 'Select travellers',
                'type' => $this->formType,
            ],
            [
                'label' => 'Confirmation',
            ],
        ];
}

并在最后的枝条文件:

{% stylesheets '@CraueFormFlowBundle/Resources/assets/css/buttons.css' %}
    <link type="text/css" rel="stylesheet" href="{{ asset_url }}"/>
{% endstylesheets %}
<div>
    Steps:
    {% include '@CraueFormFlow/FormFlow/stepList.html.twig' %}
</div>
{{ form_start(form) }}
    {{ form_errors(form) }}
    {{ activity }}
    {{ form_rest(form) }}
    {% include '@CraueFormFlow/FormFlow/buttons.html.twig' %}
{{ form_end(form) }}

<div>
    Errors:
    {{ dump(error) }}
</div>
<div>
    Form:
    {{ dump(form) }}
</div>

根本没有额外的验证,只有该字段不能为空。我进入最后一步,当我点击Finish按钮时,我认为Symfony将生成带有“Form is valid”值的JSON,但我得到的只是一遍又一遍的最后一步。 当我调试那个部分时,单击finish会给$ flow-&gt; isValid($ form)赋予false,尽管前面的每一步都是真的,我不能在这里返回JSON响应。

我也在twig中转储表单和form-&gt; getErrors值,但没有类似于我有一些错误。如果我尝试保留该数据,则会成功保留该数据。

我没有在bundle的git页面找到解决方案。 你们这些人可能知道我应该在哪里寻找解决方案吗?

1 个答案:

答案 0 :(得分:1)

解决方案的行是:

应用程序/配置/ config.yml

csrf_protection:
    enabled: true

现在,当我想到这一点时,为什么需要csrf_token来处理CraueFormFlowBundle示例中给出的当前工作流程是有道理的