简体   繁体   English

如何使用Symfony2 / 3中的范围验证器显示单个超出范围的消息?

[英]How can I display a single out of range message with the Range validator in Symfony2/3?

The Range constraint in Symfony2 allows you to specify minimum and maximum values with messages to be shown depending on the input being over or under the limits. Symfony2中的Range约束允许您指定最小值和最大值,并根据输入超出或低于限制显示消息。

 /**
  * @Assert\Range(
  *      min = 120,
  *      max = 180,
  *      minMessage = "You must be at least {{ limit }}cm tall to enter",
  *      maxMessage = "You cannot be taller than {{ limit }}cm to enter"
  * )
  */

How can I specify a generic message that's not about the minimum or the maximum? 如何指定与最小值或最大值无关的通用消息? For eg.: 例如:

 /*
  * ...
  *      outOfRangeMessage = "You must be between {{ min }} and {{ max }} to enter."
  * ...
  */

There is no such outOfRangeMessage parameter. 没有这样的outOfRangeMessage参数。 There is an invalidMessage parameter, but that's only returned when the input isn't a number at all, which doesn't work for my purpose. 有一个invalidMessage参数,但只有在输入根本不是数字时才会返回,这对我的目的不起作用。

The worst way to do it is to hard-code the min and max values and repeat the message twice, like this: 最糟糕的方法是对minmax进行硬编码并重复两次消息,如下所示:

 /*
  * ...
  *      minMessage = "You must be between 120 and 180 to enter.",
  *      maxMessage = "You must be between 120 and 180 to enter."
  * ...
  */

but this is terrible. 但这太可怕了。

There is no way to do this. 没有办法做到这一点。 You can only use limit and value params inside the messages. 您只能在消息中使用limitvalue参数。 Also you can override RangeValidator:validate method to add more params 您还可以覆盖RangeValidator:validate方法以添加更多参数

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

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