Php 如何使用Symfony2/3中的范围验证程序显示单个超出范围的消息?
Symfony2中的约束允许您指定最小值和最大值,并根据超出或低于限制的输入显示消息Php 如何使用Symfony2/3中的范围验证程序显示单个超出范围的消息?,php,validation,symfony,annotations,doctrine,Php,Validation,Symfony,Annotations,Doctrine,Symfony2中的约束允许您指定最小值和最大值,并根据超出或低于限制的输入显示消息 /** * @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 ent
/**
* @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
参数,但只有当输入根本不是数字时才会返回,这不符合我的目的
最糟糕的方法是硬编码min
和max
值,并重复消息两次,如下所示:
/*
* ...
* minMessage = "You must be between 120 and 180 to enter.",
* maxMessage = "You must be between 120 and 180 to enter."
* ...
*/
但这太可怕了。没有办法做到这一点。您只能在消息中使用
limit
和value
参数。您还可以重写RangeValidator:validate
方法来添加更多参数无法做到这一点。您只能在消息中使用limit
和value
参数。您还可以重写RangeValidator:validate
方法来添加更多参数除了创建自己的验证器,您别无选择。@Alsatian这主意不错。除了创建自己的验证器,您别无选择。@Alsatian这主意不错。听起来很有趣。你能详细说明最重要的部分吗?你能举个例子吗?这听起来很有趣。你能详细说明最重要的部分吗?你能举个例子吗?