嵌套验证组,Spring,JSR 303
Nested validation groups, Spring, JSR 303
我正在尝试在我的 Spring 应用程序中进行嵌套验证。
public class Parent{
public interface MyGroup{}
@NotNull(groups = MyGroup.class)
private Child child;
// getters and setters omited
}
public class Child{
public interface OptionalGroup {}
@NotBlank(groups = OptionalGroup.class)
private String aField;
}
我已经看过 javax.validation 包中的@Valid,但它不支持组。我还检查了来自 spring 的 @Validated 注释,但我无法将其应用于字段。
我想做类似的事情:
public class Parent{
public interface MyGroup{}
@NotNull(groups = MyGroup.class)
@CascadeValidate(groups = MyGroup.class, cascadeGroups = OptionalGroup.class)
// 'groups' correspond to parent group and 'cascadeGroups' to a group that needs to be apply to the Child field.
private Child child;
}
然后我可以务实地做任何我想做的事:
@Inject SmartValidator validator;
public void validationMethod(Parent parent, boolean condition) throws ValidationException {
if(condition){
MapBindingResult errors= new MapBindingResult(new HashMap<>(), target.getClass().getSimpleName());
validator.validate(parent, errors, Parent.MyGroup.class); // validate all constraints associated to MyGroup
if(errors.hasErrors()) throw new ValidationException(errors); // A custom exception
}
}
知道怎么做吗?
非常感谢
我终于找到了解决办法。其实我误解了 @Valid
的用途。
关键是为父子属性声明相同的组。
解决方案:
public class Parent{
public interface MyGroup{}
@NotNull(groups = MyGroup.class)
@Valid // This annotation will launch nested validation
private Child child;
// getters and setters omited
}
public class Child{
@NotBlank(groups = Parent.MyGroup.class) // Declare same group
private String aField;
}
在这种情况下,当我这样做时:
@Inject SmartValidator validator;
public void validationMethod(Parent parent, boolean condition) throws ValidationException {
if(condition){
MapBindingResult errors= new MapBindingResult(new HashMap<>(), target.getClass().getSimpleName());
// validate all constraints associated to MyGroup (even in my nested objects)
validator.validate(parent, errors, Parent.MyGroup.class);
if(errors.hasErrors()) throw new ValidationException(errors); // A custom exception
}
}
如果在我的子字段 'aField' 中检测到验证错误,第一个关联的验证错误代码(参见 FieldError.codes)将为 'NotBlank.Parent.afield'。
我应该更好地检查 @Valid
文档。
我正在尝试在我的 Spring 应用程序中进行嵌套验证。
public class Parent{
public interface MyGroup{}
@NotNull(groups = MyGroup.class)
private Child child;
// getters and setters omited
}
public class Child{
public interface OptionalGroup {}
@NotBlank(groups = OptionalGroup.class)
private String aField;
}
我已经看过 javax.validation 包中的@Valid,但它不支持组。我还检查了来自 spring 的 @Validated 注释,但我无法将其应用于字段。
我想做类似的事情:
public class Parent{
public interface MyGroup{}
@NotNull(groups = MyGroup.class)
@CascadeValidate(groups = MyGroup.class, cascadeGroups = OptionalGroup.class)
// 'groups' correspond to parent group and 'cascadeGroups' to a group that needs to be apply to the Child field.
private Child child;
}
然后我可以务实地做任何我想做的事:
@Inject SmartValidator validator;
public void validationMethod(Parent parent, boolean condition) throws ValidationException {
if(condition){
MapBindingResult errors= new MapBindingResult(new HashMap<>(), target.getClass().getSimpleName());
validator.validate(parent, errors, Parent.MyGroup.class); // validate all constraints associated to MyGroup
if(errors.hasErrors()) throw new ValidationException(errors); // A custom exception
}
}
知道怎么做吗?
非常感谢
我终于找到了解决办法。其实我误解了 @Valid
的用途。
关键是为父子属性声明相同的组。
解决方案:
public class Parent{
public interface MyGroup{}
@NotNull(groups = MyGroup.class)
@Valid // This annotation will launch nested validation
private Child child;
// getters and setters omited
}
public class Child{
@NotBlank(groups = Parent.MyGroup.class) // Declare same group
private String aField;
}
在这种情况下,当我这样做时:
@Inject SmartValidator validator;
public void validationMethod(Parent parent, boolean condition) throws ValidationException {
if(condition){
MapBindingResult errors= new MapBindingResult(new HashMap<>(), target.getClass().getSimpleName());
// validate all constraints associated to MyGroup (even in my nested objects)
validator.validate(parent, errors, Parent.MyGroup.class);
if(errors.hasErrors()) throw new ValidationException(errors); // A custom exception
}
}
如果在我的子字段 'aField' 中检测到验证错误,第一个关联的验证错误代码(参见 FieldError.codes)将为 'NotBlank.Parent.afield'。
我应该更好地检查 @Valid
文档。