craue / CraueFormFlowBundle

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

Q: Is it possible to change form value based on selected data in first step? #281

Closed kironet closed 7 years ago

kironet commented 7 years ago

Hello,

I want to change select values, based on selected value in step one.

Is it possible to somehow return values from step one to step two, three, ...?

Thanks

This is what I tried:

protected function loadStepsConfig() {
        return array(
            array(
                'label' => '...',
                'form_type' => 'Web\AdminBundle\Form\InsertAccessoryForm',
            ),
            array(
                'label' => '...',
                'form_type' => 'Web\AdminBundle\Form\InsertAccessoryForm',

            ),
            array(
                'label' => '...',
            ),
        );
    }

    public function getFormOptions($step, array $options = array()) {
        $options = parent::getFormOptions($step, $options);

        $formData = $this->getFormData();

        if ($step === 2) {
            $options['category'] = $formData->getCategory();
        }

        return $options;
    }

FormType:

        switch ($options['flow_step']) {
            case 1:
                $validValues = [1, 2, 3, 4, 5];
                $builder->add('category', ChoiceType::class, [
                    'mapped' => false,
                    'choices' => array_combine($validValues, $validValues),
                    'placeholder' => 'Choose an option',
                ]);
                break;
            case 2:
               ...

Error:

The option "category" does not exist. Defined options are: ...

craue commented 7 years ago

Where does this error come from? I don't see that you try to actually access the option. Please provide more code, a Symfony standard edition fork demonstrating the issue, or even a failing test case.

craue commented 7 years ago

You'll need to define the option in your form type.