org.springframework.validation.ValidationUtils Java Examples

The following examples show how to use org.springframework.validation.ValidationUtils. 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: IndexValidator.java    From Spring-MVC-Blueprints with MIT License 7 votes vote down vote up
@Override
public void validate(Object obj, Errors errors) {
	Home homeForm = (Home) obj;
	ValidationUtils.rejectIfEmptyOrWhitespace(errors, "message", "message.empty");
	ValidationUtils.rejectIfEmptyOrWhitespace(errors, "quote", "quote.empty");

	if(homeForm.getMessage().length() > 500){
		errors.rejectValue("message", "message.maxlength");
	}
	if(homeForm.getMessage().length() < 50){
		errors.rejectValue("message", "message.minlength");
	}
	
	
	if(homeForm.getQuote().length() > 500){
		errors.rejectValue("quote", "quote.maxlength");
	}
	if(homeForm.getQuote().length() < 50){
		errors.rejectValue("quote", "quote.minlength");
	}
}
 
Example #2
Source File: HarvestNowValidator.java    From webcurator with Apache License 2.0 7 votes vote down vote up
public void validate(Object aCommand, Errors aErrors) {
    TargetInstanceCommand cmd = (TargetInstanceCommand) aCommand;
    if (log.isDebugEnabled()) {
        log.debug("Validating harvest now target instance command.");
    }
    
    ValidationUtils.rejectIfEmptyOrWhitespace(aErrors, TargetInstanceCommand.PARAM_CMD, "required", getObjectArrayForLabel(TargetInstanceCommand.PARAM_CMD), "Action command is a required field.");
    
    if (TargetInstanceCommand.ACTION_HARVEST.equals(cmd.getCmd())) {                        
        ValidationUtils.rejectIfEmptyOrWhitespace(aErrors, TargetInstanceCommand.PARAM_AGENT, "required", getObjectArrayForLabel(TargetInstanceCommand.PARAM_AGENT), "Harvest agent is a required field.");
        ValidationUtils.rejectIfEmptyOrWhitespace(aErrors, TargetInstanceCommand.PARAM_OID, "required", getObjectArrayForLabel(TargetInstanceCommand.PARAM_OID), "Target Instance Id is a required field.");
        if (!aErrors.hasErrors()) {                
            ValidatorUtil.validateMinimumBandwidthAvailable(aErrors, cmd.getTargetInstanceId(), "no.minimum.bandwidth", getObjectArrayForLabel(TargetInstanceCommand.PARAM_OID), "Adding this target instance will reduce the bandwidth.");
            if (cmd.getBandwidthPercent() != null) {                	
            	ValidatorUtil.validateMaxBandwidthPercentage(aErrors, cmd.getBandwidthPercent().intValue(), "max.bandwidth.exeeded");
            }                                            
        }
        
        if (!aErrors.hasErrors()) {
        	ValidatorUtil.validateTargetApproved(aErrors, cmd.getTargetInstanceId(), "target.not.approved");
        }
    }
}
 
Example #3
Source File: IndexValidator.java    From Spring-MVC-Blueprints with MIT License 7 votes vote down vote up
@Override
public void validate(Object obj, Errors errors) {
	Home homeForm = (Home) obj;
	ValidationUtils.rejectIfEmptyOrWhitespace(errors, "message",
			"message.empty");
	ValidationUtils.rejectIfEmptyOrWhitespace(errors, "quote",
			"quote.empty");

	if (homeForm.getMessage().length() > 500) {
		errors.rejectValue("message", "message.maxlength");
	}
	if (homeForm.getMessage().length() < 50) {
		errors.rejectValue("message", "message.minlength");
	}

	if (homeForm.getQuote().length() > 500) {
		errors.rejectValue("quote", "quote.maxlength");
	}
	if (homeForm.getQuote().length() < 50) {
		errors.rejectValue("quote", "quote.minlength");
	}
}
 
Example #4
Source File: UserValidator.java    From registration-login-spring-hsql with MIT License 7 votes vote down vote up
@Override
public void validate(Object o, Errors errors) {
    User user = (User) o;

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "username", "NotEmpty");
    if (user.getUsername().length() < 6 || user.getUsername().length() > 32) {
        errors.rejectValue("username", "Size.userForm.username");
    }
    if (userService.findByUsername(user.getUsername()) != null) {
        errors.rejectValue("username", "Duplicate.userForm.username");
    }

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "NotEmpty");
    if (user.getPassword().length() < 8 || user.getPassword().length() > 32) {
        errors.rejectValue("password", "Size.userForm.password");
    }

    if (!user.getPasswordConfirm().equals(user.getPassword())) {
        errors.rejectValue("passwordConfirm", "Diff.userForm.passwordConfirm");
    }
}
 
Example #5
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 #6
Source File: UserValidator.java    From registration-login-spring-xml-maven-jsp-mysql with MIT License 6 votes vote down vote up
@Override
public void validate(Object o, Errors errors) {
    User user = (User) o;

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "username", "NotEmpty");
    if (user.getUsername().length() < 6 || user.getUsername().length() > 32) {
        errors.rejectValue("username", "Size.userForm.username");
    }
    if (userService.findByUsername(user.getUsername()) != null) {
        errors.rejectValue("username", "Duplicate.userForm.username");
    }

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "NotEmpty");
    if (user.getPassword().length() < 8 || user.getPassword().length() > 32) {
        errors.rejectValue("password", "Size.userForm.password");
    }

    if (!user.getPasswordConfirm().equals(user.getPassword())) {
        errors.rejectValue("passwordConfirm", "Diff.userForm.passwordConfirm");
    }
}
 
Example #7
Source File: SiteURLsValidator.java    From webcurator with Apache License 2.0 6 votes vote down vote up
public void validate(Object aCmd, Errors aErrors) {
	UrlCommand cmd = (UrlCommand) aCmd;
	
	if (UrlCommand.ACTION_ADD_URL.equals(cmd.getActionCmd())) {		
		ValidationUtils.rejectIfEmptyOrWhitespace(aErrors, "url", "required", new Object[] {"URL"}, "URL is a required field.");
		
		if (!aErrors.hasErrors()) {
			ValidatorUtil.validateURL(aErrors, cmd.getUrl(), "invalid.url", new Object[] { cmd.getUrl()}, "The URL provided is not valid");
		}			
		
		if (!aErrors.hasErrors()) {
			String url = cmd.getUrl();
			
			if (!strategy.isValidPattern(url)) {				
				aErrors.reject("invalid.url", new Object[] {cmd.getUrl()}, "The url provided is not valid.");
			}
		}
	}
}
 
Example #8
Source File: AgencyValidator.java    From webcurator with Apache License 2.0 6 votes vote down vote up
/** @see org.springframework.validation.Validator#validate(Object, Errors). */
public void validate(Object aCmd, Errors aErrors) {
    AgencyCommand cmd = (AgencyCommand) aCmd;
    
    ValidationUtils.rejectIfEmptyOrWhitespace(aErrors, AgencyCommand.PARAM_ACTION, "required", getObjectArrayForLabel(AgencyCommand.PARAM_ACTION), "Action command is a required field.");  
            
    if (AgencyCommand.ACTION_SAVE.equals(cmd.getActionCommand())) {   
        ValidationUtils.rejectIfEmptyOrWhitespace(aErrors, AgencyCommand.PARAM_NAME, "required", getObjectArrayForLabel(AgencyCommand.PARAM_NAME), "Agency name is a required field.");
        ValidationUtils.rejectIfEmptyOrWhitespace(aErrors, AgencyCommand.PARAM_ADDRESS, "required", getObjectArrayForLabel(AgencyCommand.PARAM_ADDRESS), "Agency address is a required field.");
        ValidatorUtil.validateStringMaxLength(aErrors, cmd.getName(),80,"string.maxlength",getObjectArrayForLabelAndInt(AgencyCommand.PARAM_NAME,80),"Name field too long");
        ValidatorUtil.validateStringMaxLength(aErrors, cmd.getAddress(),255,"string.maxlength",getObjectArrayForLabelAndInt(AgencyCommand.PARAM_ADDRESS,255),"Address field too long");
        
        if (cmd.getEmail() != null && cmd.getEmail().length() > 0) {
            ValidatorUtil.validateRegEx(aErrors, cmd.getEmail(), ValidatorUtil.EMAIL_VALIDATION_REGEX, "invalid.email",getObjectArrayForLabel(AgencyCommand.PARAM_EMAIL),"the email address is invalid" );
        }
        if (cmd.getAgencyURL() != null && cmd.getAgencyURL().length() > 0) {
            ValidatorUtil.validateURL(aErrors, cmd.getAgencyURL(),"invalid.url",new Object[] {cmd.getAgencyURL()},"Invalid URL");
        }
        if (cmd.getAgencyLogoURL() != null && cmd.getAgencyLogoURL().length() > 0) {
            ValidatorUtil.validateURL(aErrors, cmd.getAgencyLogoURL(),"invalid.url",new Object[] {cmd.getAgencyLogoURL()},"Invalid URL");
        }
    }
}
 
Example #9
Source File: CreateQaIndicatorValidator.java    From webcurator with Apache License 2.0 6 votes vote down vote up
/** @see org.springframework.validation.Validator#validate(Object, Errors). */
public void validate(Object aCmd, Errors aErrors) {
    CreateQaIndicatorCommand cmd = (CreateQaIndicatorCommand) aCmd;
    
    // protect the action
    ValidationUtils.rejectIfEmptyOrWhitespace(aErrors, CreateQaIndicatorCommand.PARAM_ACTION, "required", getObjectArrayForLabel(CreateQaIndicatorCommand.PARAM_ACTION), "Action command is a required field.");  
            
    // validate the remaining form fields
    if (CreateQaIndicatorCommand.ACTION_SAVE.equals(cmd.getAction())) {
        //If an Update or a Insert check the following
        ValidationUtils.rejectIfEmptyOrWhitespace(aErrors, CreateQaIndicatorCommand.PARAM_NAME, "required", getObjectArrayForLabel(CreateQaIndicatorCommand.PARAM_NAME), "Name is a required field.");
        ValidationUtils.rejectIfEmptyOrWhitespace(aErrors, CreateQaIndicatorCommand.PARAM_AGENCY_OID, "required", getObjectArrayForLabel(CreateQaIndicatorCommand.PARAM_AGENCY_OID), "Agency is a required field.");
        ValidatorUtil.validateStringMaxLength(aErrors, cmd.getName() ,100,"string.maxlength",getObjectArrayForLabelAndInt(CreateQaIndicatorCommand.PARAM_NAME,100),"Name field too long");
      
    }
}
 
Example #10
Source File: TargetGeneralValidator.java    From webcurator with Apache License 2.0 6 votes vote down vote up
public void validate(Object comm, Errors errors) {
	TargetGeneralCommand command = (TargetGeneralCommand) comm;
	
	// Name is required and must be less than 50 characters.
	if(command.getName() != null) {
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "required", getObjectArrayForLabel(TargetGeneralCommand.PARAM_NAME), "Name is a required field");
		ValidatorUtil.validateStringMaxLength(errors, command.getName(), Target.MAX_NAME_LENGTH, "string.maxlength", getObjectArrayForLabelAndInt(TargetGeneralCommand.PARAM_NAME, Target.MAX_NAME_LENGTH), "Name is too long");
	}
	
	// Description must be less than 255 characters.
	if(command.getDescription() != null) {
		ValidatorUtil.validateStringMaxLength(errors, command.getDescription(), Target.MAX_DESC_LENGTH, "string.maxlength", getObjectArrayForLabelAndInt(TargetGeneralCommand.PARAM_DESCRIPTION, Target.MAX_DESC_LENGTH), "Description is too long");
	}
	
	if(command.getState() == -1) { 
		errors.reject("target.bad_state");
	}
}
 
Example #11
Source File: TeapotValidator.java    From example-restful-project with MIT License 6 votes vote down vote up
/**
 * Validates or rejects the {@link Teapot} object.
 */
@Override
public void validate(Object obj, Errors errors) {

    /* Id must be defined */
    ValidationUtils.rejectIfEmptyOrWhitespace(
            errors, "id", "errIdUdefined", ERR_ID_UNDEFINED);

    /* Name must be defined */
    ValidationUtils.rejectIfEmptyOrWhitespace(
            errors, "name", "errNameUndefined", ERR_NAME_UNDEFINED);

    Teapot teapot = (Teapot) obj;

    /* Check id against pattern */
    if (!ID_PATTERN.matcher(teapot.getId()).matches()) {
        errors.rejectValue("id", "errIdBadFormat", ERR_ID_BAD_FORMAT);
    }

    /* Check if volume is in the list of valid volumes */
    if (!Arrays.asList(VALID_VOLUMES).contains(teapot.getVolume())) {
        errors.rejectValue("volume", "errBadVolume", ERR_BAD_VOLUME);
    }
}
 
Example #12
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 #13
Source File: UserValidator.java    From registration-login-spring-xml-maven-jsp-mysql with MIT License 6 votes vote down vote up
@Override
public void validate(Object o, Errors errors) {
    User user = (User) o;

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "username", "NotEmpty");
    if (user.getUsername().length() < 6 || user.getUsername().length() > 32) {
        errors.rejectValue("username", "Size.userForm.username");
    }
    if (userService.findByUsername(user.getUsername()) != null) {
        errors.rejectValue("username", "Duplicate.userForm.username");
    }

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "NotEmpty");
    if (user.getPassword().length() < 8 || user.getPassword().length() > 32) {
        errors.rejectValue("password", "Size.userForm.password");
    }

    if (!user.getPasswordConfirm().equals(user.getPassword())) {
        errors.rejectValue("passwordConfirm", "Diff.userForm.passwordConfirm");
    }
}
 
Example #14
Source File: LogReaderValidator.java    From webcurator with Apache License 2.0 6 votes vote down vote up
public void validate(Object comm, Errors errors) {
	LogReaderCommand command = (LogReaderCommand) comm;

	ValidationUtils.rejectIfEmptyOrWhitespace(errors, LogReaderCommand.PARAM_LINES, "required", getObjectArrayForLabel(LogReaderCommand.PARAM_LINES), "Number of Lines is a required field");
	ValidatorUtil.validateRegEx(errors, command.getNoOfLines(), "^[0-9]*$", "typeMismatch.java.lang.Integer",getObjectArrayForLabel(LogReaderCommand.PARAM_LINES),"Number of Lines must be an integer");
	
	if(LogReaderCommand.VALUE_FROM_LINE.equals(command.getFilterType())) {
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, LogReaderCommand.PARAM_FILTER, "required", getObjectArrayForLabel(LogReaderCommand.PARAM_FILTER), "Line Number is a required field");
		ValidatorUtil.validateRegEx(errors, command.getFilter(), "^[0-9]*$", "typeMismatch.java.lang.Integer",getObjectArrayForLabel(LogReaderCommand.PARAM_FILTER),"Line Number must be an integer");
	}
	if(LogReaderCommand.VALUE_TIMESTAMP.equals(command.getFilterType())) {
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, LogReaderCommand.PARAM_FILTER, "required", getObjectArrayForLabel(LogReaderCommand.PARAM_FILTER), "Date/Time is a required field");
	}
	if(LogReaderCommand.VALUE_REGEX_MATCH.equals(command.getFilterType()) ||
			LogReaderCommand.VALUE_REGEX_CONTAIN.equals(command.getFilterType()) ||
			LogReaderCommand.VALUE_REGEX_INDENT.equals(command.getFilterType()) ) {
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, LogReaderCommand.PARAM_FILTER, "required", getObjectArrayForLabel(LogReaderCommand.PARAM_FILTER), "Regular Expression is a required field");
	}
}
 
Example #15
Source File: TargetSeedsValidator.java    From webcurator with Apache License 2.0 6 votes vote down vote up
public void validate(Object comm, Errors errors) {
	SeedsCommand command = (SeedsCommand) comm;
	
	// Linking new seeds.
	if(command.isAction(SeedsCommand.ACTION_LINK_NEW_CONFIRM)) {
		if(command.getLinkPermIdentity() == null ||
		   command.getLinkPermIdentity().length == 0) {
			errors.reject("target.errors.link.noneselected");
		}
	}
	
	// Adding seeds.
	if(command.isAction(SeedsCommand.ACTION_ADD)) {
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "seed", "required", getObjectArrayForLabel("seed"), "Seed is a required field");
		
           if (command.getSeed() != null && command.getSeed().length() > 0) {
               ValidatorUtil.validateURL(errors, command.getSeed(), "target.errors.badUrl", getObjectArrayForLabel("seed"),"Invalid URL");
           }
	}
	
	// Searching for Permissions
	if( command.isAction(SeedsCommand.ACTION_LINK_NEW_SEARCH)) {
		validateLinkSearch(command, errors);
	}
}
 
Example #16
Source File: UserValidator.java    From yugastore-java with Apache License 2.0 6 votes vote down vote up
@Override
public void validate(Object o, Errors errors) {
    User user = (User) o;

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "username", "NotEmpty");
    if (user.getUsername().length() < 6 || user.getUsername().length() > 32) {
        errors.rejectValue("username", "Size.userForm.username");
    }
    if (userService.findByUsername(user.getUsername()) != null) {
        errors.rejectValue("username", "Duplicate.userForm.username");
    }

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "NotEmpty");
    if (user.getPassword().length() < 8 || user.getPassword().length() > 32) {
        errors.rejectValue("password", "Size.userForm.password");
    }

    if (!user.getPasswordConfirm().equals(user.getPassword())) {
        errors.rejectValue("passwordConfirm", "Diff.userForm.passwordConfirm");
    }
}
 
Example #17
Source File: CdmrFeatureQueryBeanValidator.java    From tds with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void validate(Object target, Errors errors) {
  ValidationUtils.rejectIfEmpty(errors, "req", "req.empty", "must have a req parameter");
  ValidationUtils.rejectIfEmpty(errors, "var", "var.empty", "data request must have a var paramater");

  CdmrFeatureQueryBean bean = (CdmrFeatureQueryBean) target;
  if (bean.getReq() == null)
    bean.addError("must have a req parameter");
  if (bean.getVar() == null)
    bean.addError("data request must have a var parameter");

  CdmrFeatureQueryBean.RequestType reqType;
  if (bean.getReq().equalsIgnoreCase("data"))
    reqType = CdmrFeatureQueryBean.RequestType.data;
  else if (bean.getReq().equalsIgnoreCase("header"))
    reqType = CdmrFeatureQueryBean.RequestType.header;
  else
    reqType = CdmrFeatureQueryBean.RequestType.data; // default

  bean.setReqType(reqType);
}
 
Example #18
Source File: UserValidator.java    From hellokoding-courses with MIT License 6 votes vote down vote up
@Override
public void validate(Object o, Errors errors) {
    User user = (User) o;

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "username", "NotEmpty");
    if (user.getUsername().length() < 6 || user.getUsername().length() > 32) {
        errors.rejectValue("username", "Size.userForm.username");
    }
    if (userService.findByUsername(user.getUsername()) != null) {
        errors.rejectValue("username", "Duplicate.userForm.username");
    }

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "NotEmpty");
    if (user.getPassword().length() < 8 || user.getPassword().length() > 32) {
        errors.rejectValue("password", "Size.userForm.password");
    }

    if (!user.getPasswordConfirm().equals(user.getPassword())) {
        errors.rejectValue("passwordConfirm", "Diff.userForm.passwordConfirm");
    }
}
 
Example #19
Source File: UserValidator.java    From hellokoding-courses with MIT License 6 votes vote down vote up
@Override
public void validate(Object o, Errors errors) {
    User user = (User) o;

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "username", "NotEmpty");
    if (user.getUsername().length() < 6 || user.getUsername().length() > 32) {
        errors.rejectValue("username", "Size.userForm.username");
    }
    if (userService.findByUsername(user.getUsername()) != null) {
        errors.rejectValue("username", "Duplicate.userForm.username");
    }

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "NotEmpty");
    if (user.getPassword().length() < 8 || user.getPassword().length() > 32) {
        errors.rejectValue("password", "Size.userForm.password");
    }

    if (!user.getPasswordConfirm().equals(user.getPassword())) {
        errors.rejectValue("passwordConfirm", "Diff.userForm.passwordConfirm");
    }
}
 
Example #20
Source File: TargetInstanceValidator.java    From webcurator with Apache License 2.0 5 votes vote down vote up
public void validate(Object aCommand, Errors aErrors) {
	TargetInstanceCommand cmd = (TargetInstanceCommand) aCommand;
	if (log.isDebugEnabled()) {
           log.debug("Validating target instance command.");
       }
	
	if (cmd.getCmd().equals(TargetInstanceCommand.ACTION_EDIT) && cmd.get_tab_current_page().equals("GENERAL")) {
		ValidationUtils.rejectIfEmptyOrWhitespace(aErrors, TargetInstanceCommand.PARAM_OWNER, "required", getObjectArrayForLabel(TargetInstanceCommand.PARAM_OWNER), "Owner is a required field.");
           ValidationUtils.rejectIfEmptyOrWhitespace(aErrors, TargetInstanceCommand.PARAM_OID, "required", getObjectArrayForLabel(TargetInstanceCommand.PARAM_OID), "Target Instance Id is a required field.");
           ValidationUtils.rejectIfEmptyOrWhitespace(aErrors, TargetInstanceCommand.PARAM_TIME, "required", getObjectArrayForLabel(TargetInstanceCommand.PARAM_TIME), "Scheduled Time is a required field.");
           
           if (!aErrors.hasErrors()) {
           	if (cmd != null && cmd.getBandwidthPercent() != null) {
           		ValidatorUtil.validateMaxBandwidthPercentage(aErrors, cmd.getBandwidthPercent(), "max.bandwidth.exeeded");
           	}
           }
	}
	else if (cmd.getCmd().equals(TargetInstanceCommand.ACTION_ADD_NOTE) ||
			 cmd.getCmd().equals(TargetInstanceCommand.ACTION_MODIFY_NOTE)) {
           ValidationUtils.rejectIfEmptyOrWhitespace(aErrors, TargetInstanceCommand.PARAM_OID, "required", getObjectArrayForLabel(TargetInstanceCommand.PARAM_OID), "Target Instance Id is a required field.");
           ValidationUtils.rejectIfEmptyOrWhitespace(aErrors, TargetInstanceCommand.PARAM_NOTE, "required", getObjectArrayForLabel(TargetInstanceCommand.PARAM_NOTE), "Annotation is a required field.");
           ValidatorUtil.validateStringMaxLength(aErrors, cmd.getNote(), TargetAnnotationCommand.CNST_MAX_NOTE_LENGTH, "string.maxlength", getObjectArrayForLabelAndInt(TargetAnnotationCommand.PARAM_NOTE, TargetAnnotationCommand.CNST_MAX_NOTE_LENGTH), "Annotation is too long");
	}
	else if (cmd.getCmd().equals(TargetInstanceCommand.ACTION_EDIT) && cmd.get_tab_current_page().equals("DISPLAY")) {
          ValidatorUtil.validateStringMaxLength(aErrors, cmd.getDisplayNote(), TargetInstance.MAX_DISPLAY_NOTE_LENGTH, "string.maxlength", getObjectArrayForLabelAndInt(TargetInstanceCommand.PARAM_DISPLAY_NOTE, TargetInstance.MAX_DISPLAY_NOTE_LENGTH), "Display note is too long");
          ValidatorUtil.validateStringMaxLength(aErrors, cmd.getDisplayChangeReason(), TargetInstance.MAX_DISPLAY_CHANGE_REASON_LENGTH, "string.maxlength", getObjectArrayForLabelAndInt(TargetInstanceCommand.PARAM_DISPLAY_CHANGE_REASON, TargetInstance.MAX_DISPLAY_CHANGE_REASON_LENGTH), "Display Change Reason is too long");
	}
}
 
Example #21
Source File: CustomerValidator.java    From tutorials with MIT License 5 votes vote down vote up
@Override
public void validate(Object target, Errors errors) {

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "customerId", "error.customerId", "Customer Id is required.");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "customerName", "error.customerName", "Customer Name is required.");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "customerContact", "error.customerNumber", "Customer Contact is required.");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "customerEmail", "error.customerEmail", "Customer Email is required.");

}
 
Example #22
Source File: ContactFormValidator.java    From podcastpedia-web with MIT License 5 votes vote down vote up
public void validate(Object target, Errors errors) {
	ContactForm contactForm = (ContactForm)target;
	
	ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "invalid.required.name");
	ValidationUtils.rejectIfEmptyOrWhitespace(errors, "email", "invalid.required.email"); 
	ValidationUtils.rejectIfEmptyOrWhitespace(errors, "message", "invalid.required.message"); 		
	if(!isValidEmail(contactForm.getEmail())){
		errors.rejectValue("email", "invalid.required.email");
	}		
	
}
 
Example #23
Source File: OrderValidator.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void validateShippingAddress(Order order, Errors errors) {
	ValidationUtils.rejectIfEmpty(errors, "shipToFirstName", "FIRST_NAME_REQUIRED", "Shipping Info: first name is required.");
	ValidationUtils.rejectIfEmpty(errors, "shipToLastName", "LAST_NAME_REQUIRED", "Shipping Info: last name is required.");
	ValidationUtils.rejectIfEmpty(errors, "shipAddress1", "ADDRESS_REQUIRED", "Shipping Info: address is required.");
	ValidationUtils.rejectIfEmpty(errors, "shipCity", "CITY_REQUIRED", "Shipping Info: city is required.");
	ValidationUtils.rejectIfEmpty(errors, "shipState", "STATE_REQUIRED", "Shipping Info: state is required.");
	ValidationUtils.rejectIfEmpty(errors, "shipZip", "ZIP_REQUIRED", "Shipping Info: zip/postal code is required.");
	ValidationUtils.rejectIfEmpty(errors, "shipCountry", "COUNTRY_REQUIRED", "Shipping Info: country is required.");
}
 
Example #24
Source File: GeneralValidator.java    From webcurator with Apache License 2.0 5 votes vote down vote up
/** @see org.springframework.validation.Validator#validate(java.lang.Object, org.springframework.validation.Errors) */
public void validate(Object comm, Errors errors) {
	GeneralCommand command = (GeneralCommand) comm;
	
	if(command.isEditMode()) {
		
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "required", getObjectArrayForLabel(GeneralCommand.PARAM_NAME), "Name is a required field");
		if(command.getName() != null)
		{
			ValidatorUtil.validateValueNotContained(errors, command.getName(), command.getSubGroupSeparator(), "string.contains", getObjectArrayForLabelAndValue(GeneralCommand.PARAM_NAME, command.getSubGroupSeparator()), "'"+command.getSubGroupSeparator()+"' cannot be a sub-string of Name");
		}
		
		if(command.getSubGroupType().equals(command.getType()) && !GeneralCommand.ACTION_ADD_PARENT.equals(command.getAction()))
		{
			//Attempt to move page without setting a parent
			ValidationUtils.rejectIfEmptyOrWhitespace(errors, "parentOid", "required", getObjectArrayForLabel(GeneralCommand.PARAM_PARENT_OID), "Parent Group is a required field");
		}
					
		ValidatorUtil.validateStringMaxLength(errors, command.getName(), GeneralCommand.CNST_MAX_LEN_NAME, "string.maxlength", getObjectArrayForLabelAndInt(GeneralCommand.PARAM_NAME, GeneralCommand.CNST_MAX_LEN_NAME), "Name is too long.");
		ValidatorUtil.validateStringMaxLength(errors, command.getDescription(), GeneralCommand.CNST_MAX_LEN_DESC, "string.maxlength", getObjectArrayForLabelAndInt(GeneralCommand.PARAM_DESCRIPTION, GeneralCommand.CNST_MAX_LEN_DESC), "Description is too long.");
		ValidatorUtil.validateStringMaxLength(errors, command.getOwnershipMetaData(), GeneralCommand.CNST_MAX_LEN_OWNER_INFO, "string.maxlength", getObjectArrayForLabelAndInt(GeneralCommand.PARAM_OWNER_INFO, GeneralCommand.CNST_MAX_LEN_OWNER_INFO), "Owner info is too long.");
		ValidatorUtil.validateStringMaxLength(errors, command.getType(), TargetGroup.MAX_TYPE_LENGTH, "string.maxlength", getObjectArrayForLabelAndInt(GeneralCommand.PARAM_TYPE, TargetGroup.MAX_TYPE_LENGTH), "Group Type is too long.");
		
		if (!errors.hasErrors()) {
			if (command.getFromDate() != null && command.getToDate() != null) {
				ValidatorUtil.validateStartBeforeEndTime(errors, command.getFromDate(), command.getToDate(), "time.range", new Object[] {"To date", "From date"}, "To date is before from date");
			}				
		}
	}
}
 
Example #25
Source File: GroupAnnotationValidator.java    From webcurator with Apache License 2.0 5 votes vote down vote up
public void validate(Object comm, Errors errors) {
	GroupAnnotationCommand command = (GroupAnnotationCommand) comm;
	
	if(command.isAction(TargetAnnotationCommand.ACTION_ADD_NOTE) ||
			command.isAction(TargetAnnotationCommand.ACTION_MODIFY_NOTE)) {
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, GroupAnnotationCommand.PARAM_NOTE, "required", getObjectArrayForLabel(TargetAnnotationCommand.PARAM_NOTE), "Note is a required field");
		ValidatorUtil.validateStringMaxLength(errors, command.getNote(), GroupAnnotationCommand.CNST_MAX_NOTE_LENGTH, "string.maxlength", getObjectArrayForLabelAndInt(GroupAnnotationCommand.PARAM_NOTE, GroupAnnotationCommand.CNST_MAX_NOTE_LENGTH), "Annotation is too long");
	}		
}
 
Example #26
Source File: CustomerValidator.java    From POC with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public void validate(@NonNull Object target, Errors errors) {
	ValidationUtils.rejectIfEmptyOrWhitespace(errors, "firstName", "message.firstName", "FirstName is Mandatory");
	final Customer customer = (Customer) target;
	final LocalDateTime dob = customer.getDateOfBirth();
	if (Objects.nonNull(dob) && dob.isAfter(LocalDateTime.now())) {
		errors.rejectValue("dateOfBirth", ERROR_CODE, "Date Of Birth Should be before today");
		errors.reject(ERROR_CODE, "Entity Not Processable");
	}
}
 
Example #27
Source File: WidgetValidator.java    From attic-rave with Apache License 2.0 5 votes vote down vote up
/**
 * Checks if the required fields contain a value
 *
 * @param errors {@link Errors}
 */
private void validateRequiredFields(Errors errors) {
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "title", "widget.title.required");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, FIELD_URL, "widget.url.required");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "type", "widget.type.required");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "description", "widget.description.required");

}
 
Example #28
Source File: ResetPasswordValidator.java    From webcurator with Apache License 2.0 5 votes vote down vote up
/** @see org.springframework.validation.Validator#validate(java.lang.Object, org.springframework.validation.Errors) */
public void validate(Object aCmd, Errors aErrors) {
    ResetPasswordCommand cmd = (ResetPasswordCommand) aCmd;
    
    ValidationUtils.rejectIfEmptyOrWhitespace(aErrors, ResetPasswordCommand.PARAM_ACTION, "required", getObjectArrayForLabel(ResetPasswordCommand.PARAM_ACTION), "Action command is a required field.");               
    if (ResetPasswordCommand.ACTION_SAVE.equals(cmd.getAction())) {            
        ValidationUtils.rejectIfEmptyOrWhitespace(aErrors, ResetPasswordCommand.PARAM_NEW_PWD, "required", getObjectArrayForLabel(ResetPasswordCommand.PARAM_NEW_PWD), "Password is a required field.");
        ValidationUtils.rejectIfEmptyOrWhitespace(aErrors, ResetPasswordCommand.PARAM_CONFIRM_PWD, "required", getObjectArrayForLabel(ResetPasswordCommand.PARAM_CONFIRM_PWD), "Confirm password is a required field.");
        
        ValidatorUtil.validateValueMatch(aErrors, cmd.getNewPwd(), cmd.getConfirmPwd(), "string.match", getObjectArrayForTwoLabels(ResetPasswordCommand.PARAM_NEW_PWD, ResetPasswordCommand.PARAM_CONFIRM_PWD), "Your confirmation password did not match your new password.");
        ValidatorUtil.validateNewPassword(aErrors,cmd.getNewPwd(),"password.strength.failure", new Object[] {}, "Your password must have at least 1 Upper case letter, 1 lower case letter and a number.");
    }
}
 
Example #29
Source File: AccountValidator.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void validate(Object obj, Errors errors) {
	ValidationUtils.rejectIfEmpty(errors, "firstName", "FIRST_NAME_REQUIRED", "First name is required.");
	ValidationUtils.rejectIfEmpty(errors, "lastName", "LAST_NAME_REQUIRED", "Last name is required.");
	ValidationUtils.rejectIfEmpty(errors, "email", "EMAIL_REQUIRED", "Email address is required.");
	ValidationUtils.rejectIfEmpty(errors, "phone", "PHONE_REQUIRED", "Phone number is required.");
	ValidationUtils.rejectIfEmpty(errors, "address1", "ADDRESS_REQUIRED", "Address (1) is required.");
	ValidationUtils.rejectIfEmpty(errors, "city", "CITY_REQUIRED", "City is required.");
	ValidationUtils.rejectIfEmpty(errors, "state", "STATE_REQUIRED", "State is required.");
	ValidationUtils.rejectIfEmpty(errors, "zip", "ZIP_REQUIRED", "ZIP is required.");
	ValidationUtils.rejectIfEmpty(errors, "country", "COUNTRY_REQUIRED", "Country is required.");
}
 
Example #30
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()); 
}