Symfony 4. 为什么提交的表单只是部分填充模型? [英] Symfony 4. Why does submitted Form just partially populate the Model?

查看:25
本文介绍了Symfony 4. 为什么提交的表单只是部分填充模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有点恐慌 - 我正在为复杂的搜索生成 Symfony 表单,即映射到实体的数据将仅用于搜索查询构建.

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.

我通过某种逻辑创建了简单的表单、模型和来自 ChoiceType 的一些扩展类型,用于预填充选择.表单使用GET方法提交.

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

在模型中,您可以找到例如 makermodel 字段.选择 maker 后,后者使用 AJAX 填充到前端.当我提交表单时,makermodel 有非默认值,handleRequest 只填充 maker 模型的属性,但 model 为空.如果选中,复选框也会正确填充.总而言之,$form->getData() 只返回 Maker 和复选框,其他字段为空.$request->query 包含所有参数.

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.

数据映射器在这里毫无意义.而且数据中没有什么可以转换的,模型主要来自标量值.请求包含所有内容,但未正确处理.我试图实现 ChoiceLoaderInterface,但这对我不起作用,因为在加载选项期间我必须访问表单的 options,而我没有(我使用了这篇文章 https://speakerdeck.com/heahdude/symfony-forms-use-cases-and-optimization).

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).

我使用的是 Symfony 4.2.4;PHP 7.2.

I am using Symfony 4.2.4; PHP 7.2.

控制器的方法

/**
     * @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;
}

CarSimpleSearch 输入表单

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 字段

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 字段

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 字段

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 字段

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;
    }
}

所以,我可以使用 Request 中的原始数据并手动验证填充模型并发送到进一步处理,但我想这不是正确的方法,我想填充表单通过框架.我该怎么办?...

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 ?..

推荐答案

就我而言,我有一个依赖的 EntityType 由 ajax 填充,它最初是禁用的.由于 choices 其中 null,它在提交时返回 InvalidValueException.我必须做的是创建一个 EventListener 并为当前的main"字段添加有效的 choices.基本上就是这样,或多或少适合您的情况.

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.

原始形式:

// 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 );
            }
        }
    }
}

这篇关于Symfony 4. 为什么提交的表单只是部分填充模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆