簡體   English   中英

在生產網站上提交表單時出現驗證錯誤“此值不應為空”

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

我正在使用php 7.4、symfony 5.4 和 twig開發一個網站。 該網站部署在多台服務器上。 在其中一台服務器 (RedHat) 上,無法提交表單。 我收到以下錯誤 4 次:“此值不應為空。 ”。 消息顯示在表單頂部,不附加到特定字段。

我無法在另一台服務器上重現此錯誤,也無法在我的開發環境中重現...

問題可能來自驗證器,但我不確定它是 symfony 還是 doctrine 錯誤。

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]:

我假設空字段submit不是問題,因為其他 forms 在同一字段為空時工作正常。

所有服務器上的數據庫結構都相同。

這是表單的代碼:

   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'),
            )
        );
    }

其他 forms 使用完全相同的代碼,但有更多或更少的選項。

我搜索了一種在生產服務器上調試它的方法(“空白”字段的列表/轉儲?)。

任何提示將不勝感激,謝謝!

事實上,我在一個實體的幾個列上有一些@Assert\NotBlank

為什么錯誤只出現在這台服務器上:

  • 此實體的實例(數據庫記錄)在這些列上為NULL (這是一種異常行為)。
  • 檢索填充表單下拉列表的所有實例(作為“默認”數據)。
  • 看起來驗證器正在檢查提交的“數據”那些“默認”值,因為它們是表單的一部分。
  • 有 4 個斷言列,所以這就是為什么我有 4 條錯誤消息。

我做了什么來找出這個:

  • 在處理提交表單的回調中添加了dump($this->form->getErrors())指令。 它顯示了 4 個實體的列,讓我很難過。
  • 進入數據庫查看損壞的記錄,並將其刪除。

為了防止將來出現這種情況,我可能會將這些列的默認值從 NULL 更改為其他內容,基本字符串或 0 值,並在 db 中搜索導致此損壞記錄的進程。

謝謝你們的提示!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM