提问人:Topaze85 提问时间:1/31/2023 最后编辑:Topaze85 更新时间:2/1/2023 访问量:383
在生产网站上提交表单时出现验证错误“此值不应为空”
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还是教义错误。
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
所有服务器上的数据库结构都是相同的。
以下是表单的代码:
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'),
)
);
}
其他表单使用完全相同的代码,但选项或多或少。
我搜索了一种在生产服务器上调试它的方法(“空白”字段的列表/转储?
任何提示将不胜感激,谢谢!
答:
0赞
Topaze85
2/1/2023
#1
事实上,我在一个实体的几列上有一些。@Assert\NotBlank
为什么错误只出现在此服务器上:
- 此实体的实例(数据库记录)位于这些列上(这是一种异常行为)。
NULL
- 检索到的所有实例以填充表单的下拉列表(作为“默认”数据)。
- 看起来验证者正在检查提交的“数据”和那些“默认”值,因为它们是表单的一部分。
- 有 4 个断言列,所以这就是为什么我有 4 条错误消息的原因。
什么我已经做到了这一点:
- 在处理提交表单的回调中添加了一条指令。它显示了 4 个实体的列,这让我很难过。
dump($this->form->getErrors())
- 进入数据库查看损坏的记录,并将其删除。
为了防止将来出现这种情况,我可能会将这些列的默认值从 NULL 更改为其他值、基本字符串或 0 值,并在 db 中搜索导致此损坏记录的过程。
谢谢你的提示伙计们!
评论