PHP code example of megawilddaddy / formflow-bundle
1. Go to this page and download the library: Download megawilddaddy/formflow-bundle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
// src/MyCompany/MyBundle/Form/CreateVehicleForm.php
use MyCompany\MyBundle\Form\Type\VehicleEngineType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
class CreateVehicleForm extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
switch ($options['flow_step']) {
case 1:
$validValues = [2, 4];
$builder->add('numberOfWheels', ChoiceType::class, [
'choices' => array_combine($validValues, $validValues),
'placeholder' => '',
]);
break;
case 2:
// This form type is not defined in the example.
$builder->add('engine', VehicleEngineType::class, [
'placeholder' => '',
]);
break;
}
}
public function getBlockPrefix() {
return 'createVehicle';
}
}
// src/MyCompany/MyBundle/Form/CreateVehicleFlow.php
use Craue\FormFlowBundle\Form\FormFlow;
use Craue\FormFlowBundle\Form\FormFlowInterface;
use MyCompany\MyBundle\Form\CreateVehicleStep1Form;
use MyCompany\MyBundle\Form\CreateVehicleStep2Form;
class CreateVehicleFlow extends FormFlow {
protected function loadStepsConfig() {
return [
[
'label' => 'wheels',
'form_type' => CreateVehicleStep1Form::class,
],
[
'label' => 'engine',
'form_type' => CreateVehicleStep2Form::class,
'skip' => function($estimatedCurrentStepNumber, FormFlowInterface $flow) {
return $estimatedCurrentStepNumber > 1 && !$flow->getFormData()->canHaveEngine();
},
],
[
'label' => 'confirmation',
],
];
}
}
// src/MyCompany/MyBundle/Form/CreateVehicleStep1Form.php
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
class CreateVehicleStep1Form extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$validValues = [2, 4];
$builder->add('numberOfWheels', ChoiceType::class, [
'choices' => array_combine($validValues, $validValues),
'placeholder' => '',
]);
}
public function getBlockPrefix() {
return 'createVehicleStep1';
}
}
// src/MyCompany/MyBundle/Form/CreateVehicleStep2Form.php
use MyCompany\MyBundle\Form\Type\VehicleEngineType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class CreateVehicleStep2Form extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('engine', VehicleEngineType::class, [
'placeholder' => '',
]);
}
public function getBlockPrefix() {
return 'createVehicleStep2';
}
}
// in src/MyCompany/MyBundle/Controller/VehicleController.php
public function createVehicleAction() {
$formData = new Vehicle(); // Your form data class. Has to be an object, won't work properly with an array.
$flow = $this->get('myCompany.form.flow.createVehicle'); // must match the flow's service id
$flow->bind($formData);
// form of the current step
$form = $flow->createForm();
if ($flow->isValid($form)) {
$flow->saveCurrentStepData($form);
if ($flow->nextStep()) {
// form for the next step
$form = $flow->createForm();
} else {
// flow finished
$em = $this->getDoctrine()->getManager();
$em->persist($formData);
$em->flush();
$flow->reset(); // remove step data from the session
return $this->redirect($this->generateUrl('home')); // redirect when done
}
}
return $this->render('@MyCompanyMy/Vehicle/createVehicle.html.twig', [
'form' => $form->createView(),
'flow' => $flow,
]);
}
// in src/MyCompany/MyBundle/Form/CreateVehicleFlow.php
public function getFormOptions($step, array $options = []) {
$options = parent::getFormOptions($step, $options);
$formData = $this->getFormData();
if ($step === 2) {
$options['numberOfWheels'] = $formData->getNumberOfWheels();
}
return $options;
}
// in src/MyCompany/MyBundle/Form/CreateVehicleFlow.php
class CreateVehicleFlow extends FormFlow {
protected $allowDynamicStepNavigation = true;
// ...
}
// in src/MyCompany/MyBundle/Form/CreateVehicleFlow.php
class CreateVehicleFlow extends FormFlow {
protected $handleFileUploads = false;
// ...
}
// in src/MyCompany/MyBundle/Form/CreateVehicleFlow.php
class CreateVehicleFlow extends FormFlow {
protected $handleFileUploadsTempDir = '/path/for/flow/uploads';
// ...
}
// in src/MyCompany/MyBundle/Form/CreateVehicleFlow.php
class CreateVehicleFlow extends FormFlow {
protected $allowRedirectAfterSubmit = true;
// ...
}
// in src/MyCompany/MyBundle/Controller/VehicleController.php
public function createVehicleAction() {
// ...
$flow->bind($formData);
$form = $submittedForm = $flow->createForm();
if ($flow->isValid($submittedForm)) {
$flow->saveCurrentStepData($submittedForm);
// ...
}
if ($flow->redirectAfterSubmit($submittedForm)) {
$request = $this->getRequest();
$params = $this->get('craue_formflow_util')->addRouteParameters(array_merge($request->query->all(),
$request->attributes->get('_route_params')), $flow);
return $this->redirect($this->generateUrl($request->attributes->get('_route'), $params));
}
// ...
// return ...
}
// in src/MyCompany/MyBundle/Form/CreateVehicleFlow.php
use Craue\FormFlowBundle\Event\GetStepsEvent;
use Craue\FormFlowBundle\Event\PostBindFlowEvent;
use Craue\FormFlowBundle\Event\PostBindRequestEvent;
use Craue\FormFlowBundle\Event\PostBindSavedDataEvent;
use Craue\FormFlowBundle\Event\PostValidateEvent;
use Craue\FormFlowBundle\Event\PreBindEvent;
use Craue\FormFlowBundle\Form\FormFlowEvents;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CreateVehicleFlow extends FormFlow implements EventSubscriberInterface {
/**
* This method is only needed when _not_ using autoconfiguration. If it's there even with autoconfiguration enabled,
* the `removeSubscriber` call ensures that subscribed events won't occur twice.
* (You can remove the `removeSubscriber` call if you'll definitely never use autoconfiguration for that flow.)
*/
public function setEventDispatcher(EventDispatcherInterface $dispatcher) {
parent::setEventDispatcher($dispatcher);
$dispatcher->removeSubscriber($this);
$dispatcher->addSubscriber($this);
}
public static function getSubscribedEvents() {
return [
FormFlowEvents::PRE_BIND => 'onPreBind',
FormFlowEvents::GET_STEPS => 'onGetSteps',
FormFlowEvents::POST_BIND_SAVED_DATA => 'onPostBindSavedData',
FormFlowEvents::POST_BIND_FLOW => 'onPostBindFlow',
FormFlowEvents::POST_BIND_REQUEST => 'onPostBindRequest',
FormFlowEvents::POST_VALIDATE => 'onPostValidate',
];
}
public function onPreBind(PreBindEvent $event) {
// ...
}
public function onGetSteps(GetStepsEvent $event) {
// ...
}
public function onPostBindSavedData(PostBindSavedDataEvent $event) {
// ...
}
public function onPostBindFlow(PostBindFlowEvent $event) {
// ...
}
public function onPostBindRequest(PostBindRequestEvent $event) {
// ...
}
public function onPostValidate(PostValidateEvent $event) {
// ...
}
// ...
}
xml
<services>
<service id="myCompany.form.flow.createVehicle"
class="MyCompany\MyBundle\Form\CreateVehicleFlow"
autoconfigure="true">
</service>
</services>
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.