简体   繁体   中英

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.

 /**
  * @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. 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.

The worst way to do it is to hard-code the min and max values and repeat the message twice, like this:

 /*
  * ...
  *      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. Also you can override RangeValidator:validate method to add more params

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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