craue / CraueFormFlowBundle

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

EntityType in child form not setting selected option in select #305

Closed matt-halliday closed 6 years ago

matt-halliday commented 6 years ago

I'm building a form for creating support cases for vehicles. The form contains a child form type for vehicle details which has a Brand dropdown. Data for this dropdown is a Doctrine Entity. When using EntityType, the selected choice is not shown when navigating back to this step though it is set correctly in the data. If use a standard ChoiceType and hardcode vehicle makes, it does select the option.

FormFlow Version: 3.0.2 (also occurs in 3.0.1) Symfony Version: 2.8.26 Steps to reproduce:

Code Example

#Acme\SupportBundle\Form\VehicleDetailsType

class VehicleDetailsType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     * @return void
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('vin_number', TextType::class);
        $builder->add('make', EntityType::class, [
            'placeholder'  => 'Please select...',
            'class'        => DoctrineBrand::class,
        ]);
        $builder->add('year', NumberType::class, [
            'required' => false,
        ]);
        $builder->add('registration', TextType::class, [
            'required' => false,
        ]);
        $builder->add('mileage', NumberType::class, [
            'required' => false,
        ]);
        $builder->add('mileage_units', ChoiceType::class, [
            'required'    => false,
            'placeholder' => false,
            'expanded'    => true,
            'choices'     => VehicleDetails::MILEAGE_UNITS_CHOICES,
        ]);
    }
}
#Acme\SupportBundle\Form\SupportCaseType

class SupportCaseType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     * @return void
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        switch ($options['flow_step']) {
            case 1:
                $builder->add('vehicle_details', new VehicleDetailsType(), [
                    'required' => true,
                ]);
                break;
            case 2:
                $builder->add('contact_details', new ContactDetailsType(), [
                    'required' => true
                ]);
                break;
            case 3:
                $builder->add('symptoms', TextareaType::class, [
                    'required' => false
                ]);
                break;
            case 4:
                $builder->add('fault_codes', TextareaType::class, [
                    'required' => false
                ]);
                break;
            case 5:
                $builder->add('work_done', TextareaType::class, [
                    'required' => false
                ]);
                break;
            case 6:
                $builder->add('additional_information', TextareaType::class, [
                    'required' => false
                ]);
                break;
        }
    }
}

#Acme\SupportBundle\Form\SupportCaseFlow

class SupportCaseFlow extends FormFlow
{
    protected $allowDynamicStepNavigation = true;

    /**
     * @return array
     */
    protected function loadStepsConfig()
    {
        return [
            [
                'label' => 'Vehicle Information',
                'form_type' => 'Acme\SupportBundle\Form\SupportCaseType',
            ],
            [
                'label' => 'Contact Details',
                'form_type' => 'Acme\SupportBundle\Form\SupportCaseType',
            ],
            [
                'label' => 'Symptoms',
                'form_type' => 'Acme\SupportBundle\Form\SupportCaseType',
            ],
            [
                'label' => 'Fault Codes',
                'form_type' => 'Acme\SupportBundle\Form\SupportCaseType',
            ],
            [
                'label' => 'Work Carried Out',
                'form_type' => 'Acme\SupportBundle\Form\SupportCaseType',
            ],
            [
                'label' => 'Additional Information',
                'form_type' => 'Acme\SupportBundle\Form\SupportCaseType',
            ],
            [
                'label' => 'Confirmation',
            ],
        ];
    }
}
matt-halliday commented 6 years ago

False alarm!