java 使用 Hibernate Validator 在错误消息中包含字段名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11431437/
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
Include field name inside error message using Hibernate Validator
提问by BartoszMiller
I'm using Hibernate Validator 4.2.0.Final and I'm looking for the simplest way to include class field name in my error message.
我正在使用 Hibernate Validator 4.2.0.Final 并且我正在寻找在我的错误消息中包含类字段名称的最简单方法。
What I found is the following thread Using a custom ResourceBundle with Hibernate Validator. According to this I should create my custom annotation for each constraint annotation adding one property to each one.
我发现的是以下线程Using a custom ResourceBundle with Hibernate Validator。据此,我应该为每个约束注释创建我的自定义注释,为每个约束注释添加一个属性。
Is there a cleaner way to achieve this?
有没有更清洁的方法来实现这一目标?
The following code:
以下代码:
@Size(max = 5)
private String myField;
produces default error: size must be between 0 and 5.
产生默认错误:大小必须在 0 到 5 之间。
I would like it to be: myField size must be between 0 and 5.
我希望它是:myField 大小必须介于 0 和 5 之间。
采纳答案by BartoszMiller
For all those who are looking for a way to access class inside your validator. Putting hibernate annotating on a class level instead of variable level gives you access to a class object (assuming that you have defined a custom validator).
对于所有正在寻找访问验证器内部类的方法的人。将 hibernate 注释放在类级别而不是变量级别使您可以访问类对象(假设您已经定义了自定义验证器)。
public class myCustomValidator implements ContraintValidator <MyAnnotation, MyAnnotatedClass> {
public void initialize (...){ ... };
public boolean isValid (MyAnnotatedClass myAnnotatedClass) {
// access to elements of your myAnnotatedClass
}
}
回答by brunov
You can get the name of the field with the getPropertyPath()
method from the ConstraintViolation
class.
您可以使用类中的getPropertyPath()
方法获取字段的名称。ConstraintViolation
A good default error message can be:
一个好的默认错误消息可以是:
violation.getPropertyPath() + " " + violation.getMessage();
Which will give you "foo may not be null", or "foo.bar may not be null" in the case of nested objects.
在嵌套对象的情况下,这会给您“foo 可能不为空”或“foo.bar 可能不为空”。
回答by user3640709
If your messages are in .properties
file then there is no interpolation variable for accessing property name but one way you can achieve that is
如果您的消息在.properties
文件中,则没有用于访问属性名称的插值变量,但您可以实现的一种方法是
//in ValidationMessages.properties
app.validation.size.msg=size must be between {min} and {max}
@Size(min=10, max=15, message = "myField {app.validation.size.msg})
private String myField;
OR
或者
//in ValidationMessages.properties
app.validation.size.msg=size must be between {min} and {max} but provided ${validatedValue}
@Size(min=10, max=15, message = "myField {app.validation.size.msg})
private String myField;
Reference: message interpolation
参考:消息插值
回答by Rocky Hu
I put every field validation message into the properties file, like this:
我将每个字段验证消息放入属性文件中,如下所示:
field.student.name.size.message = Student name size is not valid.
and in the bean, use it like this:
在 bean 中,像这样使用它:
@Size(max = 5, message = "${field.student.name.size.message}")
private String myField;
I know it isn't a perfect solution, but I also don't find a better way.
我知道这不是一个完美的解决方案,但我也找不到更好的方法。
回答by xyz
I am not aware of any generic way but you can define custom error message and include field name in it.
我不知道任何通用方式,但您可以定义自定义错误消息并在其中包含字段名称。
@Size(max = 5, message = "myField size must be between 0 and 5")
private String myField;
回答by Jigar Parekh
回答by zui-coding
use this method(ex is ConstraintViolationException instance):
使用此方法(例如是 ConstraintViolationException 实例):
Set<ConstraintViolation<?>> set = ex.getConstraintViolations();
List<ErrorField> errorFields = new ArrayList<>(set.size());
ErrorField field = null;
for (Iterator<ConstraintViolation<?>> iterator = set.iterator();iterator.hasNext(); ) {
ConstraintViolation<?> next = iterator.next();
System.out.println(((PathImpl)next.getPropertyPath())
.getLeafNode().getName() + " " +next.getMessage());
}