简体   繁体   English

Symfony3 Form-Error参数不起作用

[英]Symfony3 Form-Error Parameters not working

i have problem with using Symfony 3 Form Error component. 我在使用Symfony 3 Form Error组件时遇到问题。 I need to add my own error to form, but parameters are not replaced with values. 我需要将自己的错误添加到表单中,但是参数不能替换为值。

$form->get('price')->addError(
    new \Symfony\Component\Form\FormError("Maximal value is %price% %currency%.", 
    null, 
    array('%price%' => 100, '%currency%' => 'YPI')));

I tried use parameters with {{ currency }} and {{ price }} as in other validators but still not works. 与其他验证程序一样,我尝试在{{ currency }}{{ price }}使用参数,但仍然无法使用。

There is this way: http://quedig.com/questions/35271649/symfony-form-error-message-parameters-usage/ but it's not the best way - I still believe in better soloutions where i can use classic translations without placing result from translating service here. 有这样的方法: http : //quedig.com/questions/35271649/symfony-form-error-message-parameters-usage/,但这不是最好的方法-我仍然相信更好的解决方案,使我可以不用放置经典翻译此处翻译服务的结果。

What is best usage for FormError? FormError的最佳用法是什么? Symfony3 manual is silent. Symfony3手册是静默的。

Thanks. 谢谢。

Messages passed to the constructor of FormError have to be already translated. 传递给FormError的构造函数的消息必须已经翻译。 Otherwise, they will be shown as is. 否则,它们将按原样显示。 So your code could look something like this: 因此您的代码可能如下所示:

// $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));

By the way, I suggest to use message placeholders instead of real messages as the string to be translated (see http://symfony.com/doc/current/best_practices/i18n.html#translation-keys ) 顺便说一句,我建议使用消息占位符代替真实消息作为要翻译的字符串(请参见http://symfony.com/doc/current/best_practices/i18n.html#translation-keys

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM