【问题标题】:Play constraint messages with arguments播放带参数的约束消息
【发布时间】:2014-03-17 01:21:52
【问题描述】:

我正在尝试在播放框架中创建一个通用外部化消息列表,但无法找到或找出将参数传递给我正在创建的对象中的这些消息的语法。我可以成功使用我创建的不带参数的错误消息。

我尝试了几种不同的方法来声明参数,但没有任何效果 示例:

配置/消息: error.number.fixed={0} 必须是 {1} 位

public class Customer {
@Required(message = "error.number.fixed('Phone', '10')" )
public String phone;
}

输出: {"id":["error.number.fixed('Phone', '10'"]}

没有参数,以下工作:

public class Customer {
@Required(message = "error.number.fixed" )
public String phone;
}

输出: {"id":["[] 必须是 {1} 位数字"]}

【问题讨论】:

    标签: validation playframework internationalization constraints externalizing


    【解决方案1】:

    想出了这个。

    必须创建一个以 phoneType 作为属性的电话号码注解。

    public @interface PhoneNumber { String message() default "error.phoneNumber";

    ...

    String phoneType();
    

    ...

    }

    然后在消息中设置第一个参数 ({0}): error.phoneNumber={0} must be 10 digits.

    那么当使用注解时,设置 phonetype 作为注解的第一个(在这种情况下)唯一的参数。

    @PhoneNumber(phoneType="MDN")

    如果有人看到更好的处理方法,请纠正我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-06
      相关资源
      最近更新 更多