使javax验证错误消息更具体
对不起,如果这个问题以前在什么地方讨论过。如果有,请联系我,我还没有找到一个满意的答案 我一直在四处寻找,试图找到一种方法,使javax验证提供的错误消息更加具体 在ValidationMessages.properties文件中指定了我当前用于@Min注释的消息:使javax验证错误消息更具体,java,bean-validation,min,Java,Bean Validation,Min,对不起,如果这个问题以前在什么地方讨论过。如果有,请联系我,我还没有找到一个满意的答案 我一直在四处寻找,试图找到一种方法,使javax验证提供的错误消息更加具体 在ValidationMessages.properties文件中指定了我当前用于@Min注释的消息: javax.validation.constraints.Min.message=The value of this variable must be less than {value}. Set<ConstraintVio
javax.validation.constraints.Min.message=The value of this variable must be less than {value}.
Set<ConstraintViolation<MyForm>> violations = validator.validate(form);
for (ConstraintViolation<MyForm> cv : violations) {
Class<?> annoClass = cv.getConstraintDescriptor().getAnnotation().getClass();
if (Min.class.isAssignableFrom(annoClass)) {
String errMsg = MessageFormat.format(
"The value of {0}.{1} was: {2} but must not be less than {3}",
cv.getRootBeanClass(),
cv.getPropertyPath().toString(),
cv.getInvalidValue(),
cv.getConstraintDescriptor().getAttributes().get("value"));
// Put errMsg back into the form as an error
}
}
这张照片会像预期的那样打印出来
The value of this variable must be less than 1
我想要的是消息还包括验证失败的变量(和类)的名称以及失败的变量的值。更像是
The value of class.variable was 0 but not must be less than 1
任何帮助都将不胜感激
Klee我认为您需要创建自定义约束。
您没有提到您正在使用的规范的哪个实现,但是有关于如何为Hibernate验证器创建自定义约束的很好的文档
该过程的一部分将涉及创建您自己的ValidationMessages.properties文件。烦人!在我看来,您有3种选择: