简体   繁体   English

在生产网站上提交表单时出现验证错误“此值不应为空”

[英]Validation error "This value should not be blank" when submitting a form on production website

I'm developing a website using php 7.4, symfony 5.4 and twig .我正在使用php 7.4、symfony 5.4 和 twig开发一个网站。 This website is deployed on several servers.该网站部署在多台服务器上。 On one of the servers (RedHat), a form cannot be submitted.在其中一台服务器 (RedHat) 上,无法提交表单。 I get the following error 4 times: " This value should not be blank. ".我收到以下错误 4 次:“此值不应为空。 ”。 The messages appear on top of the form and aren't attached to a particular field.消息显示在表单顶部,不附加到特定字段。

I can't reproduce this error on another server, nor on my development environment...我无法在另一台服务器上重现此错误,也无法在我的开发环境中重现...

The problem might comes from a validator but I'm not sure whether it's a symfony or a doctrine error.问题可能来自验证器,但我不确定它是 symfony 还是 doctrine 错误。

The POST data is identical on production server and dev environment: POST 数据在生产服务器和开发环境中是相同的:

report_selection[iFrame]: 1
report_selection[dteFrom]: 2023-01-30 07:00
report_selection[dteTo]: 2023-01-31 07:00 
report_selection[reportType]: 1
report_selection[size]: 200
report_selection[product]: 1
report_selection[submit]:

I assume that the empty field submit is not a problem since other forms work fine while having the same field empty.我假设空字段submit不是问题,因为其他 forms 在同一字段为空时工作正常。

The database structure is the same on all servers.所有服务器上的数据库结构都相同。

Here is the form's code:这是表单的代码:

   public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $bDisplaySize = $options['bDisplaySize'];
        $bDisplayReportType = $options['bDisplayReportType'];
        $bDisplayProduct = $options['bDisplayProduct'];
        $defaultValue = $options['defaultValue'];
        $em = $options['entity_manager'];

        list($H, $m) = explode(":", $iShiftStart);
        $initialFromDate = (new DateTime())->modify('-'.$H.' hour');
        $initialFromDate = $initialFromDate->modify('-1 day');
        $initialFromDate->setTime((int)$iShiftStart, (int)$m, 0);
        $initialToDate = clone $initialFromDate;
        $initialToDate = $initialToDate->modify('+1 day');


        $builder->add(
            'iFrame',
            ChoiceType::class,
            array(
                'label' => 'master.preselection',
                'choices' => [
                    'master.yesterday' => false,
                    'master.today' => false,
                    'master.thisWeek' => false,
                    'master.lastWeek' => false,
                    'master.thisMonth' => false,
                    'master.lastMonth' => false,
                    'master.memomryDate' => false,
                ],
                'attr' => ['onchange' => 'refreshPreselectedChoices()'],
                'choice_attr' => [
                    'master.yesterday' => [],
                    'master.today' => ['selected' => 'selected'],
                    'master.thisWeek' => [],
                    'master.lastWeek' => [],
                    'master.thisMonth' => [],
                    'master.lastMonth' => [],
                    'master.memomryDate' => ['disabled' => true],
                ],

            )
        );

        $builder->add(
            'dteFrom',
            TextType::class,
            array(
                'label' => 'form.from',
                'data' => $initialFromDate->format('Y-m-d H:i'),
                'attr' => array(
                    'style' => 'width:150px;',
                    'oninput' => 'dteFromToCustom()',
                    'onchange' => 'dteFromToCustom()',
                ),
            )
        );

        $builder->add(
            'dteTo',
            TextType::class,
            array(
                'label' => 'form.to',
                'data' => $initialToDate->format('Y-m-d H:i'),
                'attr' => array(
                    'label' => 'form.to',
                    'style' => 'width:150px;',
                    'oninput' => 'dteFromToCustom()',
                    'onchange' => 'dteFromToCustom()',
                ),
            )
        );

        if ($bDisplayReportType) {
            $builder->add(
                'reportType',
                ChoiceType::class,
                array(
                    'label' => 'summaryReport.data',
                    'choices' => array(
                        'summaryReport.type1' => '1',
                        'summaryReport.type2' => '2',
                    ),
                )
            );
        }

        if ($bDisplaySize) {
            $builder->add(
                'size',
                EntityType::class,
                array(
                    'class' => ProductsSizeSpecs::class,
                    'choice_label' => 'rSize',
                    'choice_value' => 'rSize',
                    'placeholder' => '',
                    'label' => 'form.size',
                    'required' => false,
                    'mapped' => false,
                    'query_builder' => function (EntityRepository $er) {
                        return $er->createQueryBuilder('e')
                            ->groupBy('e.rSize')
                            ->orderBy('e.rSize', 'ASC');
                    },

                )
            );
        }

        if ($bDisplayProduct) {
            $builder->add(
                'product',
                EntityType::class,
                array(
                    'class' => Products::class,
                    'choice_label' => 'sNumber',
                    'choice_value' => 'sNumber',
                    'placeholder' => '',
                    'label' => 'master.product',
                    'required' => false,
                    'query_builder' => function (EntityRepository $er) {
                        return $er->createQueryBuilder('e')
                            ->groupBy('e.sNumber')
                            ->orderBy('e.sNumber', 'ASC');
                    },

                )
            );
        }

        $builder->add(
            'submit',
            SubmitType::class,
            array(
                'label' => 'form.submit',
                'attr' => array('class' => 'btn btn-primary'),
            )
        );
    }

Other forms use the exact same code with more or less options.其他 forms 使用完全相同的代码,但有更多或更少的选项。

I search a way to debug this on the production server (list/dump of 'blank' fields?).我搜索了一种在生产服务器上调试它的方法(“空白”字段的列表/转储?)。

Any hint will be appreciated, thanks !任何提示将不胜感激,谢谢!

Indeed I had some @Assert\NotBlank on several columns of an Entity.事实上,我在一个实体的几个列上有一些@Assert\NotBlank

Why the error was only on this server:为什么错误只出现在这台服务器上:

  • An instance (db record) of this Entity was NULL on those columns (which is an anormal behavior).此实体的实例(数据库记录)在这些列上为NULL (这是一种异常行为)。
  • All the instances where retrieved to populate the form's dropdowns (as 'default' data).检索填充表单下拉列表的所有实例(作为“默认”数据)。
  • It looks like the validator is checking the submitted 'data' AND those 'default' values since they are part of the form.看起来验证器正在检查提交的“数据”那些“默认”值,因为它们是表单的一部分。
  • There were 4 asserted columns, so that's why I had 4 errors messages.有 4 个断言列,所以这就是为什么我有 4 条错误消息。

What I've done to find this out:我做了什么来找出这个:

  • Added a dump($this->form->getErrors()) instruction on the callback processing the submitted form.在处理提交表单的回调中添加了dump($this->form->getErrors())指令。 It displayed the 4 entity's columns giving me hard time.它显示了 4 个实体的列,让我很难过。
  • Went into db to see the corrupted record, and deleted it.进入数据库查看损坏的记录,并将其删除。

To prevent this in the future I might change the default values of these columns from NULL to something else, a basic string or a 0 value, and search the process that led to this corrupted record in db.为了防止将来出现这种情况,我可能会将这些列的默认值从 NULL 更改为其他内容,基本字符串或 0 值,并在 db 中搜索导致此损坏记录的进程。

Thanks for your hints guys !谢谢你们的提示!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM