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) 

Source Link

Document

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

Usage

From source file:org.openmrs.module.adminui.location.AdminUiLocationValidator.java

private void validateNameField(Errors errors, Location location) {
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "error.name");
    if (verifyIfNameHasMoreThan100Characters(location.getName())) {//TODO adjust to not longer than 255
        errors.rejectValue("name", "adminui.location.longName.errorMessage");
    }//from   w w  w  . ja v a 2  s . c o  m
}

From source file:com.virtusa.akura.common.validator.StaffServiceCategoryValidator.java

/**
 * Validates the object of the StaffServiceCategory.
 * //from  w ww  .  j a  v a  2s.c  o m
 * @param object Validates the object of the StaffServiceCategory.
 * @param err - Errors object that is building. May contain errors for the fields relating to types.
 */
@Override
public void validate(Object object, Errors err) {

    ValidationUtils.rejectIfEmptyOrWhitespace(err, FIELD_NAME, AkuraWebConstant.MANDATORY_FIELD_ERROR_CODE);
    StaffServiceCategory staffServiceCategory = (StaffServiceCategory) object;
    Pattern stringOnly = Pattern
            .compile(ValidatorExpressionUtil.getValidatorPattern(REFERENCE_STAFFSERVICECATEGORY_VALIDATOR));
    String description = staffServiceCategory.getDescription().trim();
    Matcher makeMatch = stringOnly.matcher(description);

    if (makeMatch.find()) {
        err.rejectValue(FIELD_NAME, AkuraWebConstant.MISMATCH_ERROR);
    }
}

From source file:org.pdfgal.pdfgalweb.validators.BookmarkValidator.java

@Override
public void validate(final Object target, final Errors errors) {

    final BookmarkForm bookmarkForm = (BookmarkForm) target;

    // File validation
    final MultipartFile file = bookmarkForm.getFile();
    this.validatorUtils.validateFile(file, errors, PDFEncryptionType.NON_ENCRYPTED);

    // Title validation
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "title", "common.validator.required");

    // Bookmarks validation
    validateBookmarks(file, bookmarkForm.getPagesList(), bookmarkForm.getTextsList(), errors);
}

From source file:com.virtusa.akura.common.validator.CityValidator.java

/**
 * Validates the object of the City./* w w w.  ja  va2  s.c  o m*/
 *
 * @param target - Populated object of City to validate
 * @param errors - Errors object that is building. May contain errors for the fields relating to types.
 */
public void validate(Object target, Errors errors) {

    City city = (City) target;

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, DESCRIPTION, AkuraWebConstant.MANDATORY_FIELD_ERROR_CODE);

    Pattern stringOnly = Pattern.compile(REGE_EXP);
    String description = city.getDescription().trim();

    if (city.getDescription().trim().length() > 0) {

        description = description.replaceAll(STRING_REGEX, STRING_SPACE);
        Matcher makeMatch = stringOnly.matcher(description);

        if (makeMatch.find()) {
            errors.rejectValue(DESCRIPTION, AkuraWebConstant.MISMATCH_ERROR);
        }
    }
}

From source file:com.epam.training.storefront.forms.validation.SbgSopPaymentDetailsValidator.java

@Override
public void validate(final Object object, final Errors errors) {
    final SopPaymentDetailsForm form = (SopPaymentDetailsForm) object;

    final Calendar start = parseDate(form.getStartMonth(), form.getStartYear());
    final Calendar expiration = parseDate(form.getExpiryMonth(), form.getExpiryYear());
    final Calendar current = Calendar.getInstance();

    if (start != null) {
        if (start.after(current)) {
            errors.rejectValue("startMonth", "payment.startDate.past.invalid");
        }/*  w  w w.j  a va 2s .c  om*/

        if (expiration != null) {
            if (start.after(expiration)) {
                errors.rejectValue("startMonth", "payment.startDate.invalid");
            }
        }
    }

    if (expiration != null) {
        if (expiration.before(current)) {
            errors.rejectValue("expiryMonth", "payment.expiryDate.future.invalid");
        }
    }

    if (Boolean.TRUE.equals(form.getNewBillingAddress())) {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "billingAddress.titleCode", "address.title.invalid");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "billingAddress.firstName",
                "address.firstName.invalid");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "billingAddress.lastName",
                "address.lastName.invalid");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "billingAddress.line1", "address.line1.invalid");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "billingAddress.townCity",
                "address.townCity.invalid");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "billingAddress.postcode",
                "address.postcode.invalid");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "billingAddress.countryIso",
                "address.country.invalid");
    }
}

From source file:org.pdfgal.pdfgalweb.validators.MergeValidator.java

@Override
public void validate(final Object target, final Errors errors) {

    final MergeForm mergeForm = (MergeForm) target;

    // File name validator
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "fileName", "merge.validator.file.name.required");
    if (!this.validatorUtils.validateFileName(mergeForm.getFileName())) {
        errors.rejectValue("fileName", "merge.validator.file.name.invalid");
    }//from   w ww  .j a  v a  2  s  . co  m

    // Uploaded files validator
    final List<MultipartFile> files = mergeForm.getFiles();

    if (this.isEmpty(files)) {
        // There are no files uploaded
        errors.rejectValue("files", "merge.validator.files.required");

    } else if (files.size() == 1) {
        // There is only one file uploaded
        this.validateZipFile(files.get(0), errors);

    } else {
        // There are more than one file uploaded
        final PDFEncryptionType validation = this.validatePDF(files);
        if (PDFEncryptionType.NON_PDF.equals(validation)) {
            errors.rejectValue("files", "merge.validator.files.incorrect.pdf");
        } else if (PDFEncryptionType.ENCRYPTED.equals(validation)) {
            errors.rejectValue("files", "merge.validator.files.incorrect.encrypted");
        }
    }
}

From source file:com.epam.cme.storefront.forms.validation.PaymentDetailsValidator.java

@Override
public void validate(final Object object, final Errors errors) {
    final PaymentDetailsForm form = (PaymentDetailsForm) object;

    if (StringUtils.isNotBlank(form.getStartMonth()) && StringUtils.isNotBlank(form.getStartYear())
            && StringUtils.isNotBlank(form.getExpiryMonth()) && StringUtils.isNotBlank(form.getExpiryYear())) {
        final Calendar start = Calendar.getInstance();
        start.set(Calendar.DAY_OF_MONTH, 0);
        start.set(Calendar.MONTH, Integer.parseInt(form.getStartMonth()) - 1);
        start.set(Calendar.YEAR, Integer.parseInt(form.getStartYear()) - 1);

        final Calendar expiration = Calendar.getInstance();
        expiration.set(Calendar.DAY_OF_MONTH, 0);
        expiration.set(Calendar.MONTH, Integer.parseInt(form.getExpiryMonth()) - 1);
        expiration.set(Calendar.YEAR, Integer.parseInt(form.getExpiryYear()) - 1);

        if (start.after(expiration)) {
            errors.rejectValue("startMonth", "payment.startDate.invalid");
        }//from  w w  w .  j  a v  a 2  s .co m
    }

    final boolean editMode = StringUtils.isNotBlank(form.getPaymentId());
    if (editMode || Boolean.TRUE.equals(form.getNewBillingAddress())) {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "billingAddress.titleCode", "address.title.invalid");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "billingAddress.firstName",
                "address.firstName.invalid");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "billingAddress.lastName",
                "address.lastName.invalid");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "billingAddress.line1", "address.line1.invalid");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "billingAddress.townCity",
                "address.townCity.invalid");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "billingAddress.postcode",
                "address.postcode.invalid");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "billingAddress.countryIso",
                "address.country.invalid");
        // ValidationUtils.rejectIfEmptyOrWhitespace(errors, "billingAddress.line2",
        // "address.line2.invalid"); // for some addresses this field is required by cybersource
    }
}

From source file:com.persistent.cloudninja.validator.ManageUsersCreateValidator.java

public void validate(Object target, Errors errors) {

    CreateMangeUserDTO createMangeUserDTO = (CreateMangeUserDTO) target;

    // Google /Yahoo / LiveId
    if (!createMangeUserDTO.getIdentityProvider().equals("CloudNinjaSts")) {
        if (!CommonUtility
                .emailValidation(createMangeUserDTO.getMember().getMemberCompoundKey().getMemberId())) {

            errors.rejectValue("member.memberCompoundKey.memberId", "Incorrect Email Format",
                    "Incorrect Email Format");
        }//from   w w w .j ava2  s  . c  o  m
    } else {
        // member id required
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "member.memberCompoundKey.memberId",
                "manageusers.create.memberid.required");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "member.name",
                "manageusers.create.memberrealname.required");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "member.password",
                "manageusers.create.memberpassword.required");
    }
}

From source file:com.virtusa.akura.common.validator.SportValidator.java

/**
 * Validates the object of the Sport.//from   w ww. j  av  a 2  s.c  o  m
 *
 * @param object - Populated object of Sport to validate
 * @param err - Errors object that is building. May contain errors for the fields relating to types.
 */
@Override
public void validate(Object object, Errors err) {

    ValidationUtils.rejectIfEmptyOrWhitespace(err, DESCRIPTION, AkuraWebConstant.MANDATORY_FIELD_ERROR_CODE);
    Sport sport = (Sport) object;
    String validatorPattern = ValidatorExpressionUtil.getValidatorPattern(SPORT_VALIDATOR_PATTERN);
    Pattern stringOnly = Pattern.compile(validatorPattern);
    String descript = sport.getDescription().trim();
    String description = descript.replaceAll(STRING_SPACE, STRING_EMPTY);
    Matcher makeMatch = stringOnly.matcher(description);
    if (makeMatch.find()) {
        err.rejectValue(DESCRIPTION, AkuraWebConstant.MISMATCH_ERROR);
    }

}

From source file:com.virtusa.akura.staff.validator.StaffCategoryValidator.java

/**
 * Validates the object of the StaffCategory.
 *
 * @param target - Populated object of StaffCategory to validate
 * @param errors - Errors object that is building. May contain errors for the fields relating to types.
 *//*from w  ww .  j a  va2s  . com*/
public void validate(Object target, Errors errors) {

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, DESCRIPTION, AkuraWebConstant.MANDATORY_FIELD_ERROR_CODE);
    StaffCategory staffCategory = (StaffCategory) target;

    String validatorPattern = ValidatorExpressionUtil.getValidatorPattern("STAFF.CATEGORY.VALIDATOR");
    Pattern stringOnly = Pattern.compile(validatorPattern);
    String description = staffCategory.getDescription().trim();
    description = description.replaceAll(STRING_CHECK, STRING_SPACE);
    Matcher makeMatch = stringOnly.matcher(description);
    if (makeMatch.find()) {
        errors.rejectValue(DESCRIPTION, AkuraWebConstant.MISMATCH_ERROR);
    }
}