Example usage for org.springframework.validation ValidationUtils rejectIfEmptyOrWhitespace

List of usage examples for org.springframework.validation ValidationUtils rejectIfEmptyOrWhitespace

Introduction

In this page you can find the example usage for org.springframework.validation ValidationUtils rejectIfEmptyOrWhitespace.

Prototype

public static void rejectIfEmptyOrWhitespace(Errors errors, String field, String errorCode,
        @Nullable Object[] errorArgs) 

Source Link

Document

Reject the given field with the given error code and error arguments if the value is empty or just contains whitespace.

Usage

From source file:edu.psu.citeseerx.myciteseer.domain.logic.AccountValidator.java

/**
 * Email validation//from   w  w w.j  a v a 2 s  .c o  m
 * @param email email to be validated
 * @param errors Contains any error that might occur
 */
public void validateEmail(String email, Errors errors) {
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "email", "EMAIL_REQUIRED", "Email address is required.");
    if (errors.getAllErrors().size() > 0) {
        return;
    }
    try {
        new InternetAddress(email);
        if (!hasNameAndDomain(email)) {
            errors.rejectValue("email", "VALID_EMAIL_REQUIRED", "Invalid email address");
        }
    } catch (AddressException e) {
        errors.rejectValue("email", "VALID_EMAIL_REQUIRED", "Invalid email address");
    }
    if (errors.getAllErrors().size() > 0) {
        return;
    }
    try {
        Account loginAccount = MCSUtils.getLoginAccount();
        Account existingAccount = myciteseer.getAccountByEmail(email);
        if (existingAccount != null) {
            if ((loginAccount == null) || !loginAccount.getUsername().equals(existingAccount.getUsername())) {
                errors.rejectValue("email", "EMAIL_UNIQUE_REQUIRED", "This email address is already in use.");
            }
        }
    } catch (Exception e) {
        /* ignore */ }

}

From source file:edu.psu.citeseerx.myciteseer.domain.logic.AccountValidator.java

/**
 * Produces and error if first name is empty
 * @param firstName//from  w w w. jav  a  2  s  .c  o m
 * @param errors
 */
public void validateFirstName(String firstName, Errors errors) {
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "firstName", "FIRST_NAME_REQUIRED",
            "First name is required.");
}

From source file:edu.psu.citeseerx.myciteseer.domain.logic.AccountValidator.java

/**
 * Produces and error if the last name is empty 
 * @param lastName//from   ww w  .  j  a v  a  2s  . c  om
 * @param errors
 */
public void validateLastName(String lastName, Errors errors) {
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "lastName", "LAST_NAME_REQUIRED",
            "Last name is required.");
}

From source file:edu.psu.citeseerx.myciteseer.domain.logic.AccountValidator.java

/**
 * Produces and error if affiliation1 is empty
 * @param affiliation1//from   ww  w.j  a v  a 2 s.  co  m
 * @param errors
 */
public void validateAffiliation1(String affiliation1, Errors errors) {
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "affiliation1", "AFFILIATION_REQUIRED",
            "Affiliation is required.");
}

From source file:edu.psu.citeseerx.myciteseer.domain.logic.AccountValidator.java

/**
 * Produces and error if country is empty
 * @param country//from  w  w w  .  j  a  v a 2 s  .c  om
 * @param errors
 */
public void validateCountry(String country, Errors errors) {
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "country", "COUNTRY_REQUIRED", "Country is required.");
}

From source file:edu.psu.citeseerx.myciteseer.domain.logic.UrlSubmissionValidator.java

public void validateUrl(String url, String userid, Errors errors) {
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "url", "URL_REQUIRED", "Please specify a URL.");
    try {/*from w ww .jav a2 s . com*/
        new URL(url);
    } catch (MalformedURLException e) {
        errors.rejectValue("url", "URL_SUBMISSION_ERROR", "Invalid URL.");
    }
    if (errors.getAllErrors().size() == 0) {
        if (myciteseer.isUrlAlreadySubmitted(url, userid)) {
            errors.rejectValue("url", "URL_NEW_REQUIRED", "You have already submitted this url.");
        }
    }

}

From source file:org.openmrs.module.radiology.modality.RadiologyModalityValidator.java

/**
 * Checks the form object for any inconsistencies/errors.
 *
 * @see Validator#validate(Object, Errors)
 * @should fail validation if radiology modality is null
 * @should fail validation if ae title is null or empty or whitespaces only
 * @should fail validation if name is null or empty or whitespaces only
 * @should fail validation if retire reason is null or empty if retire is true and set retired to false
 * @should fail validation if field lengths are not correct
 * @should pass validation if all fields are correct
 *//*from  w  ww .j  av a  2s . c  o  m*/
@Override
public void validate(Object obj, Errors errors) {
    final RadiologyModality radiologyModality = (RadiologyModality) obj;
    if (radiologyModality == null) {
        errors.reject("error.general");
    } else {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "aeTitle", "error.null", "Cannot be empty or null");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "error.null", "Cannot be empty or null");
        if (radiologyModality.getRetired() && StringUtils.isBlank(radiologyModality.getRetireReason())) {
            radiologyModality.setRetired(false);
            errors.rejectValue("retireReason", "error.null");
        }
        ValidateUtil.validateFieldLengths(errors, obj.getClass(), "aeTitle", "name", "description",
                "retireReason");
    }
}

From source file:org.openmrs.module.radiology.report.RadiologyReportValidator.java

/**
 * Checks the form object for any inconsistencies/errors
 *
 * @see org.springframework.validation.Validator#validate(java.lang.Object, org.springframework.validation.Errors)
 * @should fail validation if radiologyReport is null
 * @should fail validation if principalResultsInterpreter is empty or whitespace
 * @should pass validation if all fields are correct
 *//*from ww  w . ja  v  a 2s  .  com*/
public void validate(Object obj, Errors errors) {
    RadiologyReport radiologyReport = (RadiologyReport) obj;
    if (radiologyReport == null) {
        errors.reject("error.general");
    } else {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "principalResultsInterpreter", "error.null",
                "Provider can not be null");
    }
}

From source file:org.openmrs.module.radiology.validator.RadiologyReportValidator.java

public void validate(Object obj, Errors errors) {
    RadiologyReport radiologyReport = (RadiologyReport) obj;
    if (radiologyReport == null) {
        errors.reject("error.general");
    } else {/*from   w ww. ja v a 2s  .co m*/
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "principalResultsInterpreter", "error.null",
                "Provider can not be null");
    }
}

From source file:org.openmrs.validator.FieldValidator.java

/**
 * Validates the given Field. /*from   w w  w  .j  a  v  a 2  s .  co m*/
 * Ensures that the field name is present and valid
 *
 * @param obj The Field to validate.
 * @param errors Errors
 * @see org.springframework.validation.Validator#validate(java.lang.Object,
 *      org.springframework.validation.Errors)
 * @should fail if field name is null
 * @should fail if field name is empty
 * @should fail if field name is all whitespace
 * @should fail if selectMultiple is null
 * @should fail if retired is null
 * @should pass if name is ok and fieldType, selectMultiple, and retired are non-null
 * @should pass validation if field lengths are correct
 * @should fail validation if field lengths are not correct
 * should not fail if fieldType is null
 */
public void validate(Object obj, Errors errors) throws APIException {
    if (log.isDebugEnabled()) {
        log.debug(this.getClass().getName() + ".validate...");
    }

    if (obj == null || !(obj instanceof Field)) {
        throw new IllegalArgumentException(
                "The parameter obj should not be null and must be of type " + Field.class);
    }

    Field field = (Field) obj;

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "error.null", "Field name is required");
    if (field.getSelectMultiple() == null) {
        errors.rejectValue("selectMultiple", "error.general");
    }
    if (field.getRetired() == null) {
        errors.rejectValue("retired", "error.general");
    }
    ValidateUtil.validateFieldLengths(errors, obj.getClass(), "name", "tableName", "attributeName",
            "retireReason");
}