1

我使用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),
        ]
    );
}

服务.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 ] ]

预订类型.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',
        ]
    );
}

预订流程:

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 会生成带有“表单有效”值的 JSON,但我得到的只是一遍又一遍的最后一步。当我调试该部分时,单击完成会给 $flow->isValid($form) 错误,尽管前面的每个步骤都是正确的,我无法在这里返回 JSON 响应。

我还在 twig 中转储 form 和 form->getErrors 值,但没有什么类似于我遇到了一些错误。如果我尝试持久化该数据,它将成功持久化。

我在 bundle 的 git 页面上没有找到解决方案。你们在这里也许知道我应该在哪里寻找解决方案?

4

1 回答 1

1

Lines for the solution was this:

app/config/config.yml

csrf_protection:
    enabled: true

Now when I figured that, it makes sense why it needs csrf_token to work with current workflow given on CraueFormFlowBundle example

于 2015-02-19T21:39:51.030 回答