Java Code Examples for org.springframework.validation.ValidationUtils#invokeValidator()

The following examples show how to use org.springframework.validation.ValidationUtils#invokeValidator() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CollectionValidator.java    From springlets with Apache License 2.0 6 votes vote down vote up
/**
 * Validate each element inside the supplied {@link Collection}.
 * 
 * The supplied errors instance is used to report the validation errors.
 * 
 * @param target the collection that is to be validated
 * @param errors contextual state about the validation process
 */
@Override
@SuppressWarnings("rawtypes")
public void validate(Object target, Errors errors) {
  Collection collection = (Collection) target;
  int index = 0;

  for (Object object : collection) {
    BeanPropertyBindingResult elementErrors = new BeanPropertyBindingResult(object,
        errors.getObjectName());
    elementErrors.setNestedPath("[".concat(Integer.toString(index++)).concat("]."));
    ValidationUtils.invokeValidator(validator, object, elementErrors);

    errors.addAllErrors(elementErrors);
  }
}
 
Example 2
Source File: SurveyPageValidator.java    From JDeSurvey with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public void validate(Object obj, Errors errors) {
	SurveyPage surveyPage= (SurveyPage) obj;
	
	QuestionAnswerValidator questionAnswerValidator;
	int i = 0;
	for (QuestionAnswer questionAnswer : surveyPage.getQuestionAnswers()) {
		log.info("Validating question answer" +  questionAnswer.getQuestion().getQuestionText());
		errors.pushNestedPath("questionAnswers[" + i +"]");
		questionAnswerValidator = new QuestionAnswerValidator(dateFormat,
																validcontentTypes,
																validImageTypes,
																maximunFileSize,
																invalidContentMessage,
																invalidFileSizeMessage);
		ValidationUtils.invokeValidator(questionAnswerValidator, questionAnswer, errors);
		errors.popNestedPath();
		i++;
	}
}
 
Example 3
Source File: MultiActionController.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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();
}
 
Example 4
Source File: MultiActionController.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * 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();
}
 
Example 5
Source File: ProductValidatorTest.java    From maven-framework-project with MIT License 5 votes vote down vote up
@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."));
}
 
Example 6
Source File: ProductValidatorTest.java    From maven-framework-project with MIT License 5 votes vote down vote up
@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."));
}
 
Example 7
Source File: ProductValidatorTest.java    From maven-framework-project with MIT License 5 votes vote down vote up
@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()); 
}
 
Example 8
Source File: ProductValidatorTest.java    From maven-framework-project with MIT License 5 votes vote down vote up
@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."));
}
 
Example 9
Source File: ProductValidatorTest.java    From maven-framework-project with MIT License 5 votes vote down vote up
@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."));
}
 
Example 10
Source File: ProductValidatorTest.java    From maven-framework-project with MIT License 5 votes vote down vote up
@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()); 
}
 
Example 11
Source File: ControllerInputValidatorAspect.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
/**
 * Validate param using param specific validator and validate all strings using a blacklist validator
 * @param arg
 * @param argName
 */
private void validateArg(Object arg, String argName) {
    BindingResult result = new BeanPropertyBindingResult(arg, argName);
    ValidationUtils.invokeValidator(getValidator(), arg, result);
    // force string validation for bad chars
    if (arg instanceof String) {
        ValidationUtils.invokeValidator(getValidator(), new DefaultStringValidatable((String) arg), result);
    }
    if (result.hasErrors()) {
        throw new HttpMessageConversionException("Invalid input parameter " + argName, new BindException(result));
    }
}