I made form wizard with CraueFormFlowBundle and it works until last step where $flow->isValid() fails and it constantly renders last step instead of going to desired end of form wizard action.
Here is the code necessary to understand where I made mistake:
Controller:
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',
],
];
}
and at the end twig file:
{% 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>
There is no extra validation at all, only that field cannot be empty. I get to the last step, and when I click Finish
button I thought Symfony will generate JSON with "Form is valid" value, but all I got is last step over and over again.
When I debugged that section, clicking on finish gives false to $flow->isValid($form) although every previous step was true and I cannot get to returning JSON response here.
I'm also dumping form and form->getErrors values in twig but nothing resembles that I'm having some error. If I try to persist that data it is successfully persisted.
I didn't find solution for this on git page of bundle. Do you guys here maybe know where should I look for the solution?