/** * Invoke the given {@link Validator} for the supplied object and * {@link Errors} instance. * @param validator the {@code Validator} to be invoked * @param target the object to bind the parameters to * @param errors the {@link Errors} instance that should store the errors * @throws IllegalArgumentException if either of the {@code Validator} or {@code Errors} * arguments is {@code null}, or if the supplied {@code Validator} does not * {@link Validator#supports(Class) support} the validation of the supplied object's type */ public static void invokeValidator(Validator validator, Object target, Errors errors) { invokeValidator(validator, target, errors, (Object[]) null); }
/** * Invoke the given {@link Validator} for the supplied object and * {@link Errors} instance. * @param validator the {@code Validator} to be invoked * @param target the object to bind the parameters to * @param errors the {@link Errors} instance that should store the errors * @throws IllegalArgumentException if either of the {@code Validator} or {@code Errors} * arguments is {@code null}, or if the supplied {@code Validator} does not * {@link Validator#supports(Class) support} the validation of the supplied object's type */ public static void invokeValidator(Validator validator, Object target, Errors errors) { invokeValidator(validator, target, errors, (Object[]) null); }
@Test(expected = IllegalArgumentException.class) public void testInvokeValidatorWithNullValidator() throws Exception { TestBean tb = new TestBean(); Errors errors = new BeanPropertyBindingResult(tb, "tb"); ValidationUtils.invokeValidator(null, tb, errors); }
@Test(expected = IllegalArgumentException.class) public void testInvokeValidatorWithNullErrors() throws Exception { TestBean tb = new TestBean(); ValidationUtils.invokeValidator(new EmptyValidator(), tb, null); }
@Test public void testInvokeValidatorSunnyDay() throws Exception { TestBean tb = new TestBean(); Errors errors = new BeanPropertyBindingResult(tb, "tb"); ValidationUtils.invokeValidator(new EmptyValidator(), tb, errors); assertTrue(errors.hasFieldErrors("name")); assertEquals("EMPTY", errors.getFieldError("name").getCode()); }
private Errors validate(String event, Object entity) { if (entity == null) { return null; } Errors errors = new ValidationErrors(entity, persistentEntitiesFactory.getObject()); for (Validator validator : getValidatorsForEvent(event)) { if (validator.supports(entity.getClass())) { LOGGER.debug("{}: {} with {}", event, entity, validator); ValidationUtils.invokeValidator(validator, entity, errors); } } if (errors.hasErrors()) { throw new RepositoryConstraintViolationException(errors); } return errors; }
/** * Invoke the given {@link Validator} for the supplied object and * {@link Errors} instance. * @param validator the {@code Validator} to be invoked * @param target the object to bind the parameters to * @param errors the {@link Errors} instance that should store the errors * @throws IllegalArgumentException if either of the {@code Validator} or {@code Errors} * arguments is {@code null}, or if the supplied {@code Validator} does not * {@link Validator#supports(Class) support} the validation of the supplied object's type */ public static void invokeValidator(Validator validator, Object target, Errors errors) { invokeValidator(validator, target, errors, (Object[]) null); }
try { errors.pushNestedPath("drugReferenceMaps[" + index + "].conceptReferenceTerm"); ValidationUtils.invokeValidator(new ConceptReferenceTermValidator(), referenceTerm, errors); try { errors.pushNestedPath("drugReferenceMaps[" + index + "].conceptMapType"); ValidationUtils.invokeValidator(new ConceptMapTypeValidator(), mapType, errors);
ValidationUtils.invokeValidator(new ConceptReferenceTermValidator(), map.getConceptReferenceTerm(), errors);
try { errors.pushNestedPath("addresses[" + index + "]"); ValidationUtils.invokeValidator(personAddressValidator, address, errors);
@RequestMapping(value = "/user", method = RequestMethod.POST) public String validate(@ModelAttribute User user, Errors errors) { ValidationUtils.invokeValidator(validator, user, errors, ValidationOrder.class); if (errors.hasErrors()) { return "manual/user"; } // success return "manual/user"; } }
/** * Bind the parameters from the given request to the given object, * invoking the given validator. * @param request request containing the parameters * @param object object to bind the parameters to * @param objectName name of the bind object * @param validator validator to be invoked, or <code>null</code> if no validation * @return the binder used (can be treated as Errors instance) */ public static BindException bindAndValidate(ServletRequest request, Object object, String objectName, Validator validator) { BindException binder = bind(request, object, objectName); ValidationUtils.invokeValidator(validator, object, binder); return binder; }
/** * Bind request parameters onto the given command bean * @param request request from which parameters will be bound * @param command command object, that must be a JavaBean * @throws Exception in case of invalid state or arguments */ protected void bind(HttpServletRequest request, Object command) throws Exception { logger.debug("Binding request parameters onto MultiActionController command"); ServletRequestDataBinder binder = createBinder(request, command); binder.bind(request); if (this.validators != null) { for (Validator validator : this.validators) { if (validator.supports(command.getClass())) { ValidationUtils.invokeValidator(validator, command, binder.getBindingResult()); } } } binder.closeNoCatch(); }
@Override protected boolean onUnmarshalRequest(MessageContext messageContext, Object requestObject) throws Exception { Validator[] validators = getValidators(); if (validators != null) { Errors errors = new BindException(requestObject, getRequestName()); for (Validator validator : validators) { ValidationUtils.invokeValidator(validator, requestObject, errors); } if (errors.hasErrors()) { return onValidationErrors(messageContext, requestObject, errors); } } return true; }
@Override protected boolean onUnmarshalRequest(MessageContext messageContext, Object requestObject) throws Exception { Validator[] validators = getValidators(); if (validators != null) { Errors errors = new BindException(requestObject, getRequestName()); for (Validator validator : validators) { ValidationUtils.invokeValidator(validator, requestObject, errors); } if (errors.hasErrors()) { return onValidationErrors(messageContext, requestObject, errors); } } return true; }
@Override protected boolean onUnmarshalRequest(MessageContext messageContext, Object requestObject) throws Exception { Validator[] validators = getValidators(); if (validators != null) { Errors errors = new BindException(requestObject, getRequestName()); for (Validator validator : validators) { ValidationUtils.invokeValidator(validator, requestObject, errors); } if (errors.hasErrors()) { return onValidationErrors(messageContext, requestObject, errors); } } return true; }
@Override protected boolean onUnmarshalRequest(MessageContext messageContext, Object requestObject) throws Exception { Validator[] validators = getValidators(); if (validators != null) { Errors errors = new BindException(requestObject, getRequestName()); for (Validator validator : validators) { ValidationUtils.invokeValidator(validator, requestObject, errors); } if (errors.hasErrors()) { return onValidationErrors(messageContext, requestObject, errors); } } return true; }
@Test public void a_valid_product_should_not_get_any_error_during_validation() { //Arrange Product product = new Product("P9876","iPhone 5s", new BigDecimal(500)); product.setCategory("Tablet"); BindException bindException = new BindException(product, " product"); //Act ValidationUtils.invokeValidator(productValidator, product, bindException); //Assert Assert.assertEquals(0, bindException.getErrorCount()); }
@Test public void product_with_existing_productId_invalid() { //Arrange Product product = new Product("P1234","iPhone 5s", new BigDecimal(500)); product.setCategory("Tablet"); BindException bindException = new BindException(product, " product"); //Act ValidationUtils.invokeValidator(productValidator, product, bindException); //Assert Assert.assertEquals(1, bindException.getErrorCount()); Assert.assertTrue(bindException.getLocalizedMessage().contains("A product already exists with this product id.")); }
@Test public void product_without_UnitPrice_should_be_invalid() { //Arrange Product product = new Product(); BindException bindException = new BindException(product, " product"); //Act ValidationUtils.invokeValidator(productValidator, product, bindException); //Assert Assert.assertEquals(1, bindException.getErrorCount()); Assert.assertTrue(bindException.getLocalizedMessage().contains("Unit price is Invalid. It cannot be empty.")); }