craue / CraueFormFlowBundle

Multi-step forms for your Symfony project.
MIT License
734 stars 118 forks source link

Where are stored "not mapped" properties ? #399

Open fdiedler opened 2 years ago

fdiedler commented 2 years ago

Hi,

I have a custom form type Foo::Type with unmapped values :

$builder
              ->add('propertyA', TextType::class)
              ->add('propertyB', ChoiceType::class, [
                    'choices' => ....
                ])
                ->add("propertyC", ChoiceType::class, [
                    'choices' => ....
                    'mapped' => false,
                ])
;

When I submit the current step, where is stored the propertyC value ? I try to dump form data after bind in the Controller :

$fooEntity= new fooEntity(); // Has to be an object, won't work properly with an array.        
$flow->bind($fooEntity);
\dump($fooEntity);

But, there are only data mapped to the fooEntity inside.

Thanks,

craue commented 2 years ago

It's still part of the request data. probably in the POST parameters. Check the profiler or dump($request->request->all());.

fdiedler commented 2 years ago

@craue Arf, it seems that there are not saved into session. So parameters are lost if I navigate through steps.

In fact, the first step sets some options (not mapped properties) that influence other steps. Example : if I answer "yes" to a question of the first step, the second step has more fields.

protected function loadStepsConfig()
    {
        $dataNotMapped = $this->getRequest()->get('property_options');
         return [
        [
        'label' => 'Onboarding',
        'form_type' => PropertyOptionsType::class,
                'skip' => function($estimatedCurrentStepNumber, FormFlowInterface $flow) use($dataNotMapped) {
            return ($estimatedCurrentStepNumber != 1 || $dataNotMapped !== null);
                },
        ],
            [
        'label' => 'Propriétaire',
        'form_type' => PropertyType::class,
                'form_options' => [
                    'host_with_company' => ($dataNotMapped && isset($dataNotMapped['hostWithCompany']) && $dataNotMapped['hostWithCompany']),
                 ]
        ],
  ];
}

But, if I click on the "step 2" link label, parameters are lost because there are not in the request anymore.

How can I do that ?

craue commented 2 years ago

What's the reason for not mapping the field? If it's because you're using the flow to "fill" an entity with data but you need additional form fields for proceeding within the flow, you should create a form data class dedicated for this flow which will contain the entity and the additional properties.

fdiedler commented 2 years ago

@craue Yes that is the reason. That is what I done but it does not work. because all data stored in Request are lost if there are an error in the current step and I have an error mesage "Form cannot have extra fields"

protected function loadStepsConfig()
    {
        $dataNotMapped = $this->getRequest()->get('property');

        return [
            [
                'label' => 'Onboarding',
                'form_type' => PropertyType::class,
                'skip' => function($estimatedCurrentStepNumber, FormFlowInterface $flow) use($dataNotMapped) {
                    return false;
                },
            ],
            [
                'label' => 'Propriétaire',
                'form_type' => PropertyType::class,
                'form_options' => [
                    'host_with_company' => ($dataNotMapped && isset($dataNotMapped['hostWithCompany']) && $dataNotMapped['hostWithCompany']),
                ]
            ],
      ]
}

craue2

After validating the current step

craue3

Because not mapped data stored in Request are lost...

Thanks,

craue commented 2 years ago

If you need submitted form data outside of the current request, map the fields. Take a closer look at property addDriver in the "create vehicle" flow in the demo bundle: https://github.com/craue/CraueFormFlowDemoBundle/tree/782c3cec1dc1a964afad8a2c5884541bccbf7913/Form