1
1
/*
2
- * Copyright 2002-2012 the original author or authors.
2
+ * Copyright 2002-2013 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
30
30
import javax .validation .ConstraintValidator ;
31
31
import javax .validation .ConstraintValidatorContext ;
32
32
import javax .validation .ConstraintViolation ;
33
+ import javax .validation .Payload ;
33
34
import javax .validation .Valid ;
34
35
import javax .validation .constraints .NotNull ;
35
36
36
37
import org .hibernate .validator .HibernateValidator ;
37
38
import org .junit .Test ;
38
39
39
40
import org .springframework .validation .BeanPropertyBindingResult ;
41
+ import org .springframework .validation .Errors ;
40
42
import org .springframework .validation .FieldError ;
41
43
import org .springframework .validation .ObjectError ;
42
44
@@ -193,6 +195,18 @@ public void testSpringValidationWithErrorInSetElement() throws Exception {
193
195
System .out .println (fieldError .getDefaultMessage ());
194
196
}
195
197
198
+ @ Test
199
+ public void testInnerBeanValidation () throws Exception {
200
+ LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean ();
201
+ validator .afterPropertiesSet ();
202
+
203
+ MainBean mainBean = new MainBean ();
204
+ Errors errors = new BeanPropertyBindingResult (mainBean , "mainBean" );
205
+ validator .validate (mainBean , errors );
206
+ Object rejected = errors .getFieldValue ("inner.value" );
207
+ assertNull (rejected );
208
+ }
209
+
196
210
197
211
@ NameAddressValid
198
212
public static class ValidPerson {
@@ -242,7 +256,6 @@ public void setAddressSet(Set<ValidAddress> addressSet) {
242
256
}
243
257
}
244
258
245
-
246
259
public static class ValidAddress {
247
260
248
261
@ NotNull
@@ -257,7 +270,6 @@ public void setStreet(String street) {
257
270
}
258
271
}
259
272
260
-
261
273
@ Target (ElementType .TYPE )
262
274
@ Retention (RetentionPolicy .RUNTIME )
263
275
@ Constraint (validatedBy = NameAddressValidator .class )
@@ -270,7 +282,6 @@ public void setStreet(String street) {
270
282
Class <?>[] payload () default {};
271
283
}
272
284
273
-
274
285
public static class NameAddressValidator implements ConstraintValidator <NameAddressValid , ValidPerson > {
275
286
276
287
@ Override
@@ -283,4 +294,53 @@ public boolean isValid(ValidPerson value, ConstraintValidatorContext constraintV
283
294
}
284
295
}
285
296
297
+
298
+ public static class MainBean {
299
+
300
+ @ InnerValid
301
+ private InnerBean inner = new InnerBean ();
302
+
303
+ public InnerBean getInner () {
304
+ return inner ;
305
+ }
306
+ }
307
+
308
+ public static class InnerBean {
309
+
310
+ private String value ;
311
+
312
+ public String getValue () {
313
+ return value ;
314
+ }
315
+ public void setValue (String value ) {
316
+ this .value = value ;
317
+ }
318
+ }
319
+
320
+ @ Retention (RetentionPolicy .RUNTIME )
321
+ @ Target (ElementType .FIELD )
322
+ @ Constraint (validatedBy =InnerValidator .class )
323
+ public static @interface InnerValid {
324
+ String message () default "NOT VALID" ;
325
+ Class <?>[] groups () default { };
326
+ Class <? extends Payload >[] payload () default {};
327
+ }
328
+
329
+ public static class InnerValidator implements ConstraintValidator <InnerValid , InnerBean > {
330
+
331
+ @ Override
332
+ public void initialize (InnerValid constraintAnnotation ) {
333
+ }
334
+
335
+ @ Override
336
+ public boolean isValid (InnerBean bean , ConstraintValidatorContext context ) {
337
+ context .disableDefaultConstraintViolation ();
338
+ if (bean .getValue () == null ) {
339
+ context .buildConstraintViolationWithTemplate ("NULL" ). addNode ("value" ).addConstraintViolation ();
340
+ return false ;
341
+ }
342
+ return true ;
343
+ }
344
+ }
345
+
286
346
}
0 commit comments