簡體   English   中英

如何使用Symfony2 / 3中的范圍驗證器顯示單個超出范圍的消息?

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

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"
  * )
  */

如何指定與最小值或最大值無關的通用消息? 例如:

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

沒有這樣的outOfRangeMessage參數。 有一個invalidMessage參數,但只有在輸入根本不是數字時才會返回,這對我的目的不起作用。

最糟糕的方法是對minmax進行硬編碼並重復兩次消息,如下所示:

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

但這太可怕了。

沒有辦法做到這一點。 您只能在消息中使用limitvalue參數。 您還可以覆蓋RangeValidator:validate方法以添加更多參數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM