java 使 javax 验证错误消息更具体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5226797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Making javax validation error message more specific
提问by Klee
Sorry if this question has been covered somewhere before. If it has please link me to it, I haven't been able to find a satisfactory answer as yet.
对不起,如果这个问题之前已经在某处讨论过。如果有请把我链接到它,我还没有找到满意的答案。
I've been looking around trying to find a way to have the error messages provided by my javax validation more specific.
我一直在环顾四周,试图找到一种方法让我的 javax 验证提供的错误消息更具体。
The message I currently have for my @Min annotation is specified in the ValidationMessages.properties file:
在 ValidationMessages.properties 文件中指定了我目前为 @Min 注释所拥有的消息:
javax.validation.constraints.Min.message=The value of this variable must be less than {value}.
and this prints out as would be expected
这会按预期打印出来
The value of this variable must be less than 1
What I would like to have is the message also include the name of the variable (and class) that has failed the validation as well as the value of the variable that failed. So more like.
我想要的是消息还包括验证失败的变量(和类)的名称以及失败的变量的值。所以更喜欢。
The value of class.variable was 0 but not must be less than 1
Any help would be greatly appreciated.
任何帮助将不胜感激。
Klee
克利
回答by millhouse
Hmmm. Annoying! As I see it you have 3 options:
嗯。恼人的!在我看来,您有 3 个选择:
You could write a custom MessageInterpolatorand substitute it into your validation configuration, but that's seems really brittle.
You could declare your own custom annotation in-the-style of @Min (see how to do a custom validator here) ...
.. But the information you need is actually held within the ConstraintViolation object that comes out of the Validator instance. It's just that it doesn't get put into the default message.
您可以编写自定义MessageInterpolator并将其替换到您的验证配置中,但这似乎非常脆弱。
您可以以@Min 的风格声明您自己的自定义注释(请参阅如何在此处执行自定义验证器)...
.. 但是您需要的信息实际上保存在来自 Validator 实例的 ConstraintViolation 对象中。只是它没有被放入默认消息中。
I'm guessing you're currently using some kind of web framework to validate a form. If that's the case, it should be quite straightforward to override the validation and do something like this (quick hacky version, you should be able to make this very tidy by using an external properties file):
我猜您目前正在使用某种 Web 框架来验证表单。如果是这种情况,覆盖验证并执行类似操作应该非常简单(快速 hacky 版本,您应该能够通过使用外部属性文件使其非常整洁):
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 } }
回答by Sean
I believe you need to create a custom constraint.
You haven't mentioned which implementation of the spec you are using, but there is good documentation on how to create a custom constraint for the Hibernate Validator here.
我相信您需要创建一个自定义约束。
您还没有提到哪一个实现你使用的是规范的,但对如何创建的Hibernate验证的自定义约束良好的文档在这里。
Part of the process will involve creating your own ValidationMessages.properties file
该过程的一部分将涉及创建您自己的 ValidationMessages.properties 文件