|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2016 the original author or authors. |
| 2 | + * Copyright 2002-2017 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.
|
|
41 | 41 | import org.springframework.beans.factory.annotation.Autowired;
|
42 | 42 | import org.springframework.context.ConfigurableApplicationContext;
|
43 | 43 | import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
| 44 | +import org.springframework.core.convert.support.DefaultConversionService; |
44 | 45 | import org.springframework.core.env.Environment;
|
45 | 46 | import org.springframework.validation.BeanPropertyBindingResult;
|
46 | 47 | import org.springframework.validation.Errors;
|
@@ -255,6 +256,23 @@ public void testValidationWithOptionalField() throws Exception {
|
255 | 256 | assertNull(rejected);
|
256 | 257 | }
|
257 | 258 |
|
| 259 | + @Test |
| 260 | + public void testListValidation() throws Exception { |
| 261 | + LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean(); |
| 262 | + validator.afterPropertiesSet(); |
| 263 | + |
| 264 | + ListContainer listContainer = new ListContainer(); |
| 265 | + listContainer.addString("A"); |
| 266 | + listContainer.addString("X"); |
| 267 | + |
| 268 | + BeanPropertyBindingResult errors = new BeanPropertyBindingResult(listContainer, "listContainer"); |
| 269 | + errors.initConversion(new DefaultConversionService()); |
| 270 | + validator.validate(listContainer, errors); |
| 271 | + |
| 272 | + FieldError fieldError = errors.getFieldError("list[1]"); |
| 273 | + assertEquals("X", errors.getFieldValue("list[1]")); |
| 274 | + } |
| 275 | + |
258 | 276 |
|
259 | 277 | @NameAddressValid
|
260 | 278 | public static class ValidPerson {
|
@@ -424,4 +442,53 @@ public boolean isValid(InnerBean bean, ConstraintValidatorContext context) {
|
424 | 442 | }
|
425 | 443 | }
|
426 | 444 |
|
| 445 | + |
| 446 | + public static class ListContainer { |
| 447 | + |
| 448 | + @NotXList |
| 449 | + private List<String> list = new LinkedList<>(); |
| 450 | + |
| 451 | + public void addString(String value) { |
| 452 | + list.add(value); |
| 453 | + } |
| 454 | + |
| 455 | + public List<String> getList() { |
| 456 | + return list; |
| 457 | + } |
| 458 | + } |
| 459 | + |
| 460 | + |
| 461 | + @Retention(RetentionPolicy.RUNTIME) |
| 462 | + @Target(ElementType.FIELD) |
| 463 | + @Constraint(validatedBy = NotXListValidator.class) |
| 464 | + public @interface NotXList { |
| 465 | + |
| 466 | + String message() default "Should not be X"; |
| 467 | + |
| 468 | + Class<?>[] groups() default { }; |
| 469 | + |
| 470 | + Class<? extends Payload>[] payload() default {}; |
| 471 | + } |
| 472 | + |
| 473 | + |
| 474 | + public static class NotXListValidator implements ConstraintValidator<NotXList, List<String>> { |
| 475 | + |
| 476 | + @Override |
| 477 | + public void initialize(NotXList constraintAnnotation) { |
| 478 | + } |
| 479 | + |
| 480 | + @Override |
| 481 | + public boolean isValid(List<String> list, ConstraintValidatorContext context) { |
| 482 | + context.disableDefaultConstraintViolation(); |
| 483 | + boolean valid = true; |
| 484 | + for (int i = 0; i < list.size(); i++) { |
| 485 | + if ("X".equals(list.get(i))) { |
| 486 | + context.buildConstraintViolationWithTemplate(context.getDefaultConstraintMessageTemplate()).addBeanNode().inIterable().atIndex(i).addConstraintViolation(); |
| 487 | + valid = false; |
| 488 | + } |
| 489 | + } |
| 490 | + return valid; |
| 491 | + } |
| 492 | + } |
| 493 | + |
427 | 494 | }
|
0 commit comments