【问题标题】:Symfony Form Event add Error to specific fieldSymfony 表单事件将错误添加到特定字段
【发布时间】:2017-06-25 19:30:53
【问题描述】:

我的情况如下:

如果用户从 "maxRedemptionForDiscount" 中选择 true 并在 "maxRedemptionForDiscountValue" 中输入 "0",则应该有一个错误消息呈现到特定字段(在 TextType 字段的位置)

这是带有 eventListener 的表单:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add(
        'maxRedemptionForDiscount',
        ChoiceType::class,
        [
            'placeholder'        => false,
            'multiple'           => false,
            'choices'            => [
                true  => 'discount.form_fields.set_max_redemptions',
                false => 'discount.form_fields.unlimited',
            ],
            'label'              => 'discount.form_fields.max_redemption_for_discount',
            'translation_domain' => 'entities',
            'required'           => false,
            'error_bubbling'     => true,
            'attr'               => [
                'class' => 'maxRedemptionForDiscountSelect',
            ],
        ]
    )->add(
        'maxRedemptionForDiscountValue',
        TextType::class,
        [
            'label'              => 'discount.form_fields.set_max_redemptions',
            'translation_domain' => 'entities',
            'required'           => false,
        ]
    )->addEventListener(
        FormEvents::PRE_SUBMIT,
        [$this, 'onPreSubmit']
    );
}

这是 onPreSubmit 函数:

/**
 * @param FormEvent $event
 */
public function onPreSubmit(FormEvent $event)
{
    $data = $event->getData();
    $form = $event->getForm();

    if ($data['maxRedemptionForDiscount'] == 1) {
        if ($data['maxRedemptionForDiscountValue'] == 0) {
            $form->addError(new FormError('error message'));
        }
    }
    $event->setData($data);
}

这是树枝代码:

{{ form_row(form.maxRedemptionForDiscount) }}

<div id="maxRedemptionForDiscountValue">
    {{ form_row(form.maxRedemptionForDiscountValue) }}
</div>

这会在表单上方呈现一条错误消息。 但我希望我向特定字段呈现错误消息。

这不起作用:

$form->get('maxRedemptionForDiscountValue')->addError(new FormError('error message'));

如果我尝试这样做,错误消息将在我的表单顶部消失,但不会显示在特定字段位置。

我在这里做错了什么?

【问题讨论】:

    标签: php forms symfony events


    【解决方案1】:

    首先,您应该将error_bubbling 设置为false(或将其删除为默认行为)。

    如文档所述

    如果为 true,则此字段的任何错误都将传递给父字段或表单。例如,如果在普通字段上设置为 true,则该字段的任何错误都将附加到主表单,而不是特定字段。

    特别是ChoiceType

    设置该字段上的错误必须附加到该字段而不是父字段(大多数情况下是表单)。

    其次,你应该在特定的表单字段中添加错误

    $form
      ->get('maxRedemptionForDiscountValue')
      ->addError(new FormError('error message'));
    

    第三,你应该编辑你的模板

    <div id="maxRedemptionForDiscountValue">
        {{ form_errors(form.maxRedemptionForDiscountValue) }}
        {{ form_row(form.maxRedemptionForDiscountValue) }}
    </div>
    

    【讨论】:

    • 好的,谢谢我删除了error_bubling。正如我在问题中所写,我尝试了您的解决方案“$form ->get('maxRedemptionForDiscountValue') ->addError(new FormError('error message'));”但这对我不起作用。如果我使用它,错误只会从表单顶部删除,不会显示在特定字段中。
    • @goldlife 您是否尝试了两种解决方案:error_bubbling 已删除 + 将错误添加到特定字段?
    • @goldlife 结果如何?
    • 正如我所说,错误消息将在我的表单顶部消失,但不会显示在特定字段位置。
    • 这很遗憾行不通。如果我尝试这个 - 尽管我选择 true 并输入 0,但表单是有效的。如果我删除 ->get('maxRedemptionForDiscountValue') 并仅使用 $form->addError 错误消息将显示在表单上方。
    猜你喜欢
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 2011-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-23
    • 1970-01-01
    相关资源
    最近更新 更多