Php Symfony3表单错误参数不工作

Php Symfony3表单错误参数不工作,php,forms,translation,symfony,Php,Forms,Translation,Symfony,我在使用Symfony 3表单错误组件时遇到问题。我需要将我自己的错误添加到表单中,但参数不会替换为值 $form->get('price')->addError( new \Symfony\Component\Form\FormError("Maximal value is %price% %currency%.", null, array('%price%' => 100, '%currency%' => 'YPI'))); 我试着像在其

我在使用Symfony 3表单错误组件时遇到问题。我需要将我自己的错误添加到表单中,但参数不会替换为值

$form->get('price')->addError(
    new \Symfony\Component\Form\FormError("Maximal value is %price% %currency%.", 
    null, 
    array('%price%' => 100, '%currency%' => 'YPI')));
我试着像在其他验证器中一样,在
{{currency}}
{{price}}
中使用参数,但仍然不起作用

有这样一种方式:但这不是最好的方式——我仍然相信更好的解决方案,在这里我可以使用经典翻译,而不必把翻译服务的结果放在这里

FormError的最佳用法是什么?Symfony3手册处于静默状态


谢谢。

传递给
FormError
构造函数的消息必须已经翻译。否则,它们将按原样显示。因此,您的代码可以如下所示:

// $translator should be the "translator" service
$message = $translator->trans('Maximal value is %price% %currency%.', [
    '%price%' => 100,
    '%currency%' => 'YPI',
]);
$form->get('price')->addError(new FormError($message));

顺便说一下,我建议使用消息占位符而不是真正的消息作为要翻译的字符串(请参见)

这与我在原始查询中发布的解决方案相同。但似乎没有其他解决办法。现在我不知道为什么FormError有参数:-D.这样的参数。。