Symfony 4. Why does submitted Form just partially populate the Model?

这一生的挚爱 提交于 2019-12-13 05:32:44

问题


A bit in the panic - I am generating Symfony form for a complex search, i.e. mapped data to the entity will be used just for a search query building.

I create simple form, model, some extended types from ChoiceType for prepopulation choices by some logic. The form is submitted with GET method.

In the model you find maker and model fields for example. The latter populated on the frontend with AJAX, after maker has been selected. When I do submit the form, and maker and model have non-default value, the handleRequest only populates the maker property of the Model, but the model is left empty. Also the checkboxes are correctly populated if checked. All in all, $form->getData() returns just Maker and checkboxes, other fields are null. $request->query has all parameters.

The data mappers are senseless here. And also there is nothing to transform in the data, the Model is mostly from scalar values. The request contains everything, but it is not handled correctly. I tried to implement ChoiceLoaderInterface, but that doesn't work for me, because during loading choices I have to have access to the options of the form, which I don't (I used this article https://speakerdeck.com/heahdude/symfony-forms-use-cases-and-optimization).

I am using Symfony 4.2.4; PHP 7.2.

Controller's method

/**
     * @Route("/search/car", name="car_search", methods={"GET"})
     * @param Request $request
     */
    public function carSearchAction(Request $request)
    {
        $carModel = new CarSimpleSearchModel();
        $form     = $this->createForm(CarSimpleSearchType::class, $carModel);
        $form->handleRequest($request);

        $form->getData();

        .....
    }

CarSimpleSearchModel

class CarSimpleSearchModel
{
    public $maker;
    public $model;
    public $priceFrom;
    public $priceTo;
    public $yearFrom;
    public $yearTo;
    public $isCompanyOwner;
    public $isPrivateOwners;
    public $isRoublePrice;
}

CarSimpleSearchType the form

class CarSimpleSearchType extends AbstractType
{
    protected $urlGenerator;

    public function __construct(UrlGeneratorInterface $urlGenerator)
    {
        $this->urlGenerator = $urlGenerator;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('vehicle_type', HiddenType::class, [
                'data' => VehicleTypeType::CAR,
                'mapped' => false,
            ])
            ->add('maker', CarMakerSelectType::class)
            ->add('model', CarModelsSelectType::class)
            ->add(
                'priceFrom',
                VehiclePriceRangeType::class,
                [
                    'vehicle_type' => VehicleTypeType::CAR,
                ]
            )
            ->add(
                'priceTo',
                VehiclePriceRangeType::class,
                [
                    'vehicle_type' => VehicleTypeType::CAR,
                ]
            )
            ->add(
                'yearFrom',
                VehicleYearRangeType::class,
                [
                    'vehicle_type' => VehicleTypeType::CAR,
                ]
            )
            ->add(
                'yearTo',
                VehicleYearRangeType::class,
                [
                    'vehicle_type' => VehicleTypeType::CAR,
                ]
            )
            ->add('isCompanyOwner', CheckboxType::class)
            ->add('isPrivateOwners', CheckboxType::class)
            ->add('isRoublePrice', CheckboxType::class)
            ->add('submit', SubmitType::class);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(
            [
                'data_class' => CarSimpleSearchModel::class,
                'compound'   => true,
                'method'     => 'GET',
                'required'   => false,
                'action'     => $this->urlGenerator->generate('car_search'),
            ]
        );
    }

    public function getBlockPrefix()
    {
        return 'car_search_form';
    }
}

CarMakerSelectType field

class CarMakerSelectType extends AbstractType
{
    /**
     * @var VehicleExtractorService
     */
    private $extractor;

    /**
     * VehicleMakerSelectType constructor.
     *
     * @param VehicleExtractorService $extractor
     */
    public function __construct(VehicleExtractorService $extractor)
    {
        $this->extractor = $extractor;
    }

    public function getParent()
    {
        return ChoiceType::class;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(
            [
                'placeholder'  => null,
                'vehicle_type' => null,
                'choices'      => $this->getVariants(),
            ]
        );
    }

    private function getVariants()
    {
        $makers  = $this->extractor->getMakersByVehicleType(VehicleTypeType::CAR);
        $choices = [];

        foreach ($makers as $maker) {
            $choices[$maker['name']] = $maker['id'];
        }

        return $choices;
    }
}

CarModelSelectType field

class CarModelsSelectType extends AbstractType
{
    private $extractor;
    public function __construct(VehicleExtractorService $extractor)
    {
        $this->extractor = $extractor;
    }

    public function getParent()
    {
        return ChoiceType::class;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(
            [
                'disabled'    => true,
            ]
        );
    }
}

VehiclePriceRangeType field

class VehiclePriceRangeType extends AbstractType
{
    private $extractor;

    public function __construct(VehicleExtractorService $extractor)
    {
        $this->extractor = $extractor;
    }

    public function getParent()
    {
        return ChoiceType::class;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(
            [
                'vehicle_type' => null,
            ]
        );
    }

    public function buildView(FormView $view, FormInterface $form, array $options)
    {
        foreach ($this->getRange($options['vehicle_type']) as $value) {
            $view->vars['choices'][] = new ChoiceView($value, $value, $value);
        }
    }

    private function getRange(int $vehicleType)
    {
        return PriceRangeGenerator::generate($this->extractor->getMaxVehiclePrice($vehicleType));
    }
}

VehicleYearRangeType field

class VehicleYearRangeType extends AbstractType
{
    private $extractor;

    public function __construct(VehicleExtractorService $extractorService)
    {
        $this->extractor = $extractorService;
    }

    public function getParent()
    {
        return ChoiceType::class;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(
            [
                'vehicle_type' => null,
            ]
        );
    }

    public function buildView(FormView $view, FormInterface $form, array $options)
    {
        foreach ($this->getRange($options['vehicle_type']) as $value) {
            $view->vars['choices'][] = new ChoiceView($value, $value, $value);
        }
    }

    protected function getRange(int $vehicleType): array
    {
        $yearRange = RangeGenerator::generate(
            $this->extractor->getMinYear($vehicleType),
            $this->extractor->getMaxYear($vehicleType),
            1,
            true,
            true
        );

        return $yearRange;
    }
}

So, I can use the raw data from the Request and manually validate-populate the model and send to further processing, but I guess that's not the Right Way, and I want to populated the form by the framework. How can I ?..


回答1:


In my case, I had a dependent EntityType populated by ajax that is initially disabled. Since choices where null, it was returning an InvalidValueException on submission. What I had to do is create an EventListener and add the valid choices for the current 'main' field. This is basically it, more or less adapted to your case.

Original form:

// Setup Fields
$builder
    ->add('maker', CarMakerSelectType::class)
    ->add('model', CarModelsSelectType::class, [
            'choices' => [],
            // I was setting the disabled on a Event::PRE_SET_DATA if previous field was null
            // since I could be loading values from the database but I guess you can do it here
            'attr' => ['disabled' => 'disabled'],
        ]
    );
$builder->addEventSubscriber(new ModelListener($this->extractor));

Event Subscriber that adds back valid choices:

class ModelListener implements EventSubscriberInterface
{
    public function __construct(VehicleExtractorService $extractor)
    {
        $this->extractor = $extractor;
    }

    public static function getSubscribedEvents()
    {
        return [
            FormEvents::PRE_SUBMIT => 'onPreSubmitData',
        ];
    }

    public function onPreSubmitData(FormEvent $event)
    {
        // At this point you get only the scalar values, Model hasn't been transformed yet
        $data = $event->getData();
        $form = $event->getForm();

        $maker_id = $data['maker'];
            $model= $form->get('model');
            $options = $model->getConfig()->getOptions();

            if (!empty($maker_id)) {
                unset($options['attr']['disabled']);
                $options['choices'] = $this->extractor->getModelsFor($maker_id);

                $form->remove('model');
                $form->add('model', CarModelsSelectType::class, $options );
            }
        }
    }
}


来源:https://stackoverflow.com/questions/56639112/symfony-4-why-does-submitted-form-just-partially-populate-the-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!