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, @Nullable String defaultMessage) 

Source Link

Document

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

Usage

From source file:org.opentides.web.validator.UserValidator.java

public void validate(Object object, Errors e) {

    BaseUser user = (BaseUser) object;//from w w w . java2  s .  co  m

    ValidationUtils.rejectIfEmptyOrWhitespace(e, "firstName", "error.required", new Object[] { "First Name" },
            "First name is required.");

    ValidationUtils.rejectIfEmptyOrWhitespace(e, "lastName", "error.required", new Object[] { "Last Name" },
            "Last name is required.");

    ValidationUtils.rejectIfEmptyOrWhitespace(e, "credential.username", "error.required",
            new Object[] { "Username" }, "Username is required.");

    ValidationUtils.rejectIfEmptyOrWhitespace(e, "groups", "error.required.at-least-one",
            new Object[] { "Groups" }, "At least one Usergroup is required.");

    if (isDuplicateUsername(user)) {
        e.rejectValue("username", "error.duplicate-field",
                new Object[] { user.getCredential().getUsername(), "username" }, "User name already exists.");
    }

    ValidationUtils.rejectIfEmptyOrWhitespace(e, "emailAddress", "error.required",
            new Object[] { "Email Address" }, "Email address is required.");
    if (!StringUtil.isEmpty(user.getEmailAddress())) {
        if (!ValidatorUtil.isEmail(user.getEmailAddress())) {
            e.rejectValue("emailAddress", "error.invalid-email-address",
                    new Object[] { user.getEmailAddress() }, "Email Address is invalid.");
        }
        if (isDuplicateEmail(user)) {
            e.rejectValue("emailAddress", "error.duplicate-field",
                    new Object[] { user.getEmailAddress(), "email" }, "Email address already exists.");
        }
    }

    if (user.getIsNew()) {
        ValidationUtils.rejectIfEmptyOrWhitespace(e, "credential.newPassword", "error.required",
                new Object[] { "Password" }, "Password is required.");
        ValidationUtils.rejectIfEmptyOrWhitespace(e, "credential.confirmPassword", "error.required",
                new Object[] { "Confirm Password" }, "Confirm password is required.");
    }

    if (!StringUtil.isEmpty(user.getCredential().getNewPassword())
            && !StringUtil.isEmpty(user.getCredential().getConfirmPassword())
            && !user.getCredential().getNewPassword().equals(user.getCredential().getConfirmPassword())) {
        e.rejectValue("credential.confirmPassword", "error.password-confirmation-did-not-match",
                "Password confirmation did not match.");
    }
}

From source file:org.opentides.web.validator.TenantValidator.java

public void validate(Object object, Errors e) {

    Tenant tenant = (Tenant) object;/*from   ww  w . ja va2  s .c  o m*/
    BaseUser user = tenant.getOwner();

    ValidationUtils.rejectIfEmptyOrWhitespace(e, "company", "error.required", new Object[] { "Company" },
            "Company is required.");

    ValidationUtils.rejectIfEmptyOrWhitespace(e, "accountType", "error.required",
            new Object[] { "Account Type" }, "Account type is required.");

    ValidationUtils.rejectIfEmptyOrWhitespace(e, "owner.firstName", "error.required",
            new Object[] { "First Name" }, "First name is required.");

    ValidationUtils.rejectIfEmptyOrWhitespace(e, "owner.lastName", "error.required",
            new Object[] { "Last Name" }, "Last name is required.");

    ValidationUtils.rejectIfEmptyOrWhitespace(e, "owner.credential.username", "error.required",
            new Object[] { "Username" }, "Username is required.");

    if (isDuplicateUsername(user)) {
        e.reject("error.duplicate-field", new Object[] { user.getCredential().getUsername(), "username" },
                "Username already exists.");
    }

    ValidationUtils.rejectIfEmptyOrWhitespace(e, "owner.emailAddress", "error.required",
            new Object[] { "Email Address" }, "Email address is required.");
    if (!StringUtil.isEmpty(user.getEmailAddress())) {
        if (!ValidatorUtil.isEmail(user.getEmailAddress())) {
            e.rejectValue("owner.emailAddress", "error.invalid-email-address",
                    new Object[] { user.getEmailAddress() }, "Email address is invalid.");
        }
        if (isDuplicateEmail(user)) {
            e.rejectValue("owner.emailAddress", "error.duplicate-field",
                    new Object[] { user.getEmailAddress(), "email" }, "Email address already exists.");
        }
    }

    if (user.getIsNew()) {
        ValidationUtils.rejectIfEmptyOrWhitespace(e, "owner.credential.newPassword", "error.required",
                new Object[] { "Password" }, "Password is required.");
        ValidationUtils.rejectIfEmptyOrWhitespace(e, "owner.credential.confirmPassword", "error.required",
                new Object[] { "Confirm Password" }, "Confirm password is required.");
    }

    if ((!StringUtil.isEmpty(user.getCredential().getNewPassword())
            || !StringUtil.isEmpty(user.getCredential().getConfirmPassword()))
            && !user.getCredential().getNewPassword().equals(user.getCredential().getConfirmPassword())) {
        e.rejectValue("owner.credential.confirmPassword", "error.password-confirmation-did-not-match",
                "Password confirmation did not match.");
    }
}

From source file:org.tonguetied.web.KeywordValidator.java

public void validate(Object target, Errors errors) {
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, FIELD_KEYWORD, "error.keyword.required", null, "default");
    final Keyword keyword = (Keyword) target;
    validateDuplicates(keyword, errors);
    validateDuplicates(keyword.getTranslations(), errors);
}

From source file:org.tonguetied.web.LanguageValidator.java

/**
 * This validation method check if the all mandatory fields on a 
 * {@link Country} object have been set.
 * /*w  ww.j a  v a 2  s.c  o m*/
 * @param language the {@link Language} object to validate
 * @param errors contextual state about the validation process (never null)
 */
private void validateMandatoryFields(final Language language, Errors errors) {
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, FIELD_NAME, "error.language.name.required", null,
            "default");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, FIELD_CODE, "error.language.code.required", null,
            "default");
}

From source file:org.tonguetied.web.UserValidator.java

/**
 * This validation method check if the all mandatory fields on a
 * {@link User} object have been set./*  www.j av  a  2  s .c om*/
 * 
 * @param user the {@link User} object to validate
 * @param errors contextual state about the validation process (never null)
 */
private void validateMandatoryFields(final User user, Errors errors) {
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, FIELD_USER_USERNAME, "error.username.required", null,
            "default");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, FIELD_USER_FIRSTNAME, "error.first.name.required", null,
            "default");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, FIELD_USER_LASTNAME, "error.last.name.required", null,
            "default");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, FIELD_USER_EMAIL, "error.email.required", null,
            "default");
}

From source file:org.tonguetied.web.BundleValidator.java

/**
 * This validation method check if the all mandatory fields on a
 * {@link Bundle} object have been set.//  w  w w  . j  a  v a  2s  .c  o m
 * 
 * @param bundle the {@link Bundle} object to validate
 * @param errors contextual state about the validation process (never null)
 */
private void validateMandatoryFields(final Bundle bundle, Errors errors) {
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, FIELD_NAME, "error.bundle.name.required", null,
            "default");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, FIELD_RESOURCE_NAME,
            "error.bundle.resource.name.required", null, "default");
}

From source file:org.webcurator.ui.admin.validator.TemplateValidator.java

/** @see org.springframework.validation.Validator#validate(Object, Errors). */
public void validate(Object aCmd, Errors aErrors) {
    TemplateCommand tempCmd = (TemplateCommand) aCmd;

    ValidationUtils.rejectIfEmptyOrWhitespace(aErrors, TemplateCommand.PARAM_ACTION, "required",
            getObjectArrayForLabel(TemplateCommand.PARAM_ACTION), "Action command is a required field.");

    if (AgencyCommand.ACTION_SAVE.equals(tempCmd.getAction())) {
        ValidationUtils.rejectIfEmptyOrWhitespace(aErrors, TemplateCommand.PARAM_AGENCY_OID, "required",
                getObjectArrayForLabel(TemplateCommand.PARAM_AGENCY_OID),
                "A specified Agency is a required field.");
        ValidationUtils.rejectIfEmptyOrWhitespace(aErrors, TemplateCommand.PARAM_TEMPLATE_NAME, "required",
                getObjectArrayForLabel(TemplateCommand.PARAM_TEMPLATE_NAME),
                "The template name is a required field.");
        ValidationUtils.rejectIfEmptyOrWhitespace(aErrors, TemplateCommand.PARAM_TEMPLATE_TYPE, "required",
                getObjectArrayForLabel(TemplateCommand.PARAM_TEMPLATE_TYPE),
                "The template type is a required field.");
        ValidationUtils.rejectIfEmptyOrWhitespace(aErrors, TemplateCommand.PARAM_TEMPLATE_TEXT, "required",
                getObjectArrayForLabel(TemplateCommand.PARAM_TEMPLATE_TEXT),
                "The template text is a required field.");

        if (tempCmd.getTemplateType().equals(PermissionTemplate.EMAIL_TYPE_TEMPLATE)) {
            ValidationUtils.rejectIfEmptyOrWhitespace(aErrors, TemplateCommand.PARAM_TEMPLATE_SUBJECT,
                    "required", getObjectArrayForLabel(TemplateCommand.PARAM_TEMPLATE_SUBJECT),
                    "The template subject is a required field.");
            ValidationUtils.rejectIfEmptyOrWhitespace(aErrors, TemplateCommand.PARAM_TEMPLATE_OVERWRITE_FROM,
                    "required", getObjectArrayForLabel(TemplateCommand.PARAM_TEMPLATE_OVERWRITE_FROM),
                    "The template overwrite from is a required field.");
            if (tempCmd.getTemplateOverwriteFrom())
                ValidationUtils.rejectIfEmptyOrWhitespace(aErrors, TemplateCommand.PARAM_TEMPLATE_FROM,
                        "required", getObjectArrayForLabel(TemplateCommand.PARAM_TEMPLATE_FROM),
                        "The template from is a required field when the template overwrite option is selected.");

            if (tempCmd.getTemplateFrom() != null && tempCmd.getTemplateFrom().length() > 0) {
                ValidatorUtil.validateRegEx(aErrors, tempCmd.getTemplateFrom(),
                        ValidatorUtil.EMAIL_VALIDATION_REGEX, "invalid.email",
                        getObjectArrayForLabel(TemplateCommand.PARAM_TEMPLATE_FROM),
                        "the template from email address is invalid");
            }/*from  w  w w . j av a  2  s .  c o m*/

            checkEmails(aErrors, tempCmd.getTemplateCc(), TemplateCommand.PARAM_TEMPLATE_CC, "template CC");
            checkEmails(aErrors, tempCmd.getTemplateBcc(), TemplateCommand.PARAM_TEMPLATE_BCC, "template BCC");
        }

        TemplateValidatorHelper validatorHelper = new TemplateValidatorHelper(tempCmd.getTemplateText(),
                tempCmd.getTemplateType());
        validatorHelper.parseForErrors(aErrors);
    }
}

From source file:org.webcurator.ui.agent.validator.BandwidthRestrictionValidator.java

/** @see org.springframework.validation.Validator#validate(java.lang.Object, org.springframework.validation.Errors) */
public void validate(Object aCommand, Errors aErrors) {
    BandwidthRestrictionsCommand cmd = (BandwidthRestrictionsCommand) aCommand;
    if (log.isDebugEnabled()) {
        log.debug("Validating bandwidth restrictions command.");
    }/* ww  w  .  j a  va  2  s.c om*/

    ValidationUtils.rejectIfEmptyOrWhitespace(aErrors, BandwidthRestrictionsCommand.PARAM_ACTION, "required",
            getObjectArrayForLabel(BandwidthRestrictionsCommand.PARAM_ACTION),
            "Action command is a required field.");
    if (BandwidthRestrictionsCommand.ACTION_EDIT.equals(cmd.getActionCmd())) {
        ValidationUtils.rejectIfEmptyOrWhitespace(aErrors, BandwidthRestrictionsCommand.PARAM_DAY, "required",
                getObjectArrayForLabel(BandwidthRestrictionsCommand.PARAM_DAY),
                "Day of the week is a required field.");
        ValidationUtils.rejectIfEmptyOrWhitespace(aErrors, BandwidthRestrictionsCommand.PARAM_START, "required",
                getObjectArrayForLabel(BandwidthRestrictionsCommand.PARAM_START),
                "Start Time is a required field.");
        ValidationUtils.rejectIfEmptyOrWhitespace(aErrors, BandwidthRestrictionsCommand.PARAM_END, "required",
                getObjectArrayForLabel(BandwidthRestrictionsCommand.PARAM_END),
                "End Time is a required field.");
        ValidationUtils.rejectIfEmptyOrWhitespace(aErrors, BandwidthRestrictionsCommand.PARAM_LIMIT, "required",
                getObjectArrayForLabel(BandwidthRestrictionsCommand.PARAM_LIMIT),
                "Bandwidth limit is a required field.");
    } else if (BandwidthRestrictionsCommand.ACTION_SAVE.equals(cmd.getActionCmd())) {
        ValidationUtils.rejectIfEmptyOrWhitespace(aErrors, BandwidthRestrictionsCommand.PARAM_DAY, "required",
                getObjectArrayForLabel(BandwidthRestrictionsCommand.PARAM_DAY),
                "Day of the week is a required field.");
        ValidationUtils.rejectIfEmptyOrWhitespace(aErrors, BandwidthRestrictionsCommand.PARAM_START, "required",
                getObjectArrayForLabel(BandwidthRestrictionsCommand.PARAM_START),
                "Start Time is a required field.");
        ValidationUtils.rejectIfEmptyOrWhitespace(aErrors, BandwidthRestrictionsCommand.PARAM_END, "required",
                getObjectArrayForLabel(BandwidthRestrictionsCommand.PARAM_END),
                "End Time is a required field.");
        ValidationUtils.rejectIfEmptyOrWhitespace(aErrors, BandwidthRestrictionsCommand.PARAM_LIMIT, "required",
                getObjectArrayForLabel(BandwidthRestrictionsCommand.PARAM_LIMIT),
                "Bandwidth limit is a required field.");
        ValidatorUtil.validateMinNumber(aErrors, cmd.getLimit(), BandwidthRestrictionsCommand.CNSNT_LOW_LIMIT,
                "positive.number", getObjectArrayForLabelAndValue(BandwidthRestrictionsCommand.PARAM_LIMIT,
                        cmd.getLimit().toString()),
                "Bandwidth is set too low");
        if (!aErrors.hasErrors()) {
            ValidatorUtil
                    .validateStartBeforeEndTime(aErrors, cmd.getStart(), cmd.getEnd(), "time.range",
                            getObjectArrayForTwoLabels(BandwidthRestrictionsCommand.PARAM_START,
                                    BandwidthRestrictionsCommand.PARAM_END),
                            "The end time is before the start time.");
        }

        if (!aErrors.hasErrors()) {
            ValidatorUtil.validateNoBandwidthPeriodOverlaps(aErrors, cmd, "time.overlap",
                    getObjectArrayForTwoLabels(BandwidthRestrictionsCommand.PARAM_START,
                            BandwidthRestrictionsCommand.PARAM_END),
                    "The specified time period overlaps with an existing one.");
        }
    } else if (BandwidthRestrictionsCommand.ACTION_DELETE.equals(cmd.getActionCmd())) {
        ValidationUtils.rejectIfEmptyOrWhitespace(aErrors, BandwidthRestrictionsCommand.PARAM_OID, "required",
                getObjectArrayForLabel(BandwidthRestrictionsCommand.PARAM_OID), "oid is a required field.");
    }
}

From source file:org.webcurator.ui.target.controller.QaTiSummaryController.java

private void validateschedule(TargetSchedulesCommand scheduleCommand, BindException errors) {

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "startDate", "",
            getObjectArrayForLabel(TargetInstanceSummaryCommand.PARAM_START_DATE),
            "From Date is a required field");
    ValidatorUtil.validateStartBeforeOrEqualEndTime(errors, scheduleCommand.getStartDate(),
            scheduleCommand.getEndDate(), "time.range",
            getObjectArrayForTwoLabels(TargetInstanceSummaryCommand.PARAM_START_DATE,
                    TargetInstanceSummaryCommand.PARAM_END_DATE),
            "The start time must be before the end time.");

}

From source file:org.webcurator.ui.target.validator.HarvestNowValidator.java

public void validate(Object aCommand, Errors aErrors) {
    TargetInstanceCommand cmd = (TargetInstanceCommand) aCommand;
    if (log.isDebugEnabled()) {
        log.debug("Validating harvest now target instance command.");
    }/*from   w w w  .ja v  a 2s.  c om*/

    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");
        }
    }
}