Symfony 表单未显示正确的错误(待定星座)
Symfony Form not showing correct error (pending on constellation)
我对表单的约束检查和错误的正确显示有疑问。
问题描述
该表单有三个复选框,其中两个必须由用户选中(接受条款和条件)。
- 如果只有一个必填复选框(第一个或第二个)未选中,则错误显示正确
- 如果两个必填复选框都未选中,则根本不会显示错误
分析已经完成
- 遍历错误link表明确实没有错误
- 如果我在表单中添加一个额外的文本字段,上述问题就会消失(但我不想要那个字段)。
BookingTermsAndConditionsFormType.php
class BookingTermsAndConditionsFormType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('checkbox1', CheckboxType::class, [
'label' => 'accept <a href="#" target="_blank">Terms and Conditions</a>.*',
'label_html' => true,
'label_attr' => ['class' => 'switch-custom'],
'constraints' => [
new IsTrue([
'message' => 'Accepting Terms and Conditions are mandatory.',
]),
],
])
->add('checkbox2', CheckboxType::class, [
'label' => 'accept <a href="#" target="_blank">privacy policy</a>*',
'label_html' => true,
'label_attr' => ['class' => 'switch-custom'],
'constraints' => [
new IsTrue([
'message' => 'Accepting privacy policy is mandatory.',
]),
],
])
->add('checkbox3', CheckboxType::class, [
'label' => 'I want the newsletter.',
'label_html' => true,
'label_attr' => ['class' => 'switch-custom'],
]);
}
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefaults([]);
}
}
_formTermsAndConditions.html.twig
{{ form_start(bookingTermsAndConditionsForm, {
attr: {
class: 'form-general',
novalidate: 'novalidate',
id: 'form_booking_terms_and_conditions'
}
}) }}
<div class="row">
<div class="col-10 offset-2">
<div>
{{ form_row(bookingTermsAndConditionsForm.checkbox1) }}
{{ form_row(bookingTermsAndConditionsForm.checkbox2) }}
{{ form_row(bookingTermsAndConditionsForm.checkbox3) }}
</div>
</div>
</div>
{{ form_end(bookingTermsAndConditionsForm) }}
问题
这里有什么问题?
请在这里找到我的解决方法。
我添加了一个额外的字段并使用了 HiddenType
。这使得 form-errors 可以正常工作。不要问我为什么...
BookingTermsAndConditionsFormType.php
[...]
$builder
->add('dummy', HiddenType::class, [
'label' => 'i am stupid and only needed because constrain-errors are not
displayed correctly without me (e.g. in the case both mandatory
checkboxes are not checked).'
])
[...]
我对表单的约束检查和错误的正确显示有疑问。
问题描述
该表单有三个复选框,其中两个必须由用户选中(接受条款和条件)。
- 如果只有一个必填复选框(第一个或第二个)未选中,则错误显示正确
- 如果两个必填复选框都未选中,则根本不会显示错误
分析已经完成
- 遍历错误link表明确实没有错误
- 如果我在表单中添加一个额外的文本字段,上述问题就会消失(但我不想要那个字段)。
BookingTermsAndConditionsFormType.php
class BookingTermsAndConditionsFormType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('checkbox1', CheckboxType::class, [
'label' => 'accept <a href="#" target="_blank">Terms and Conditions</a>.*',
'label_html' => true,
'label_attr' => ['class' => 'switch-custom'],
'constraints' => [
new IsTrue([
'message' => 'Accepting Terms and Conditions are mandatory.',
]),
],
])
->add('checkbox2', CheckboxType::class, [
'label' => 'accept <a href="#" target="_blank">privacy policy</a>*',
'label_html' => true,
'label_attr' => ['class' => 'switch-custom'],
'constraints' => [
new IsTrue([
'message' => 'Accepting privacy policy is mandatory.',
]),
],
])
->add('checkbox3', CheckboxType::class, [
'label' => 'I want the newsletter.',
'label_html' => true,
'label_attr' => ['class' => 'switch-custom'],
]);
}
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefaults([]);
}
}
_formTermsAndConditions.html.twig
{{ form_start(bookingTermsAndConditionsForm, {
attr: {
class: 'form-general',
novalidate: 'novalidate',
id: 'form_booking_terms_and_conditions'
}
}) }}
<div class="row">
<div class="col-10 offset-2">
<div>
{{ form_row(bookingTermsAndConditionsForm.checkbox1) }}
{{ form_row(bookingTermsAndConditionsForm.checkbox2) }}
{{ form_row(bookingTermsAndConditionsForm.checkbox3) }}
</div>
</div>
</div>
{{ form_end(bookingTermsAndConditionsForm) }}
问题
这里有什么问题?
请在这里找到我的解决方法。
我添加了一个额外的字段并使用了 HiddenType
。这使得 form-errors 可以正常工作。不要问我为什么...
BookingTermsAndConditionsFormType.php
[...]
$builder
->add('dummy', HiddenType::class, [
'label' => 'i am stupid and only needed because constrain-errors are not
displayed correctly without me (e.g. in the case both mandatory
checkboxes are not checked).'
])
[...]