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:com.saint.spring.saml.web.MetadataValidator.java

public void validate(Object target, Errors errors) {

    MetadataForm metadata = (MetadataForm) target;

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "entityId", "required", "Entity id must be set.");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "baseURL", "required", "Base URL is required.");

    if (metadata.getSecurityProfile() == null) {
        errors.rejectValue("securityProfile", null, "Security profile must be specified.");
    } else if (!"pkix".equalsIgnoreCase(metadata.getSecurityProfile())
            && !"metaiop".equals(metadata.getSecurityProfile())) {
        errors.rejectValue("securityProfile", null, "Selected value is not supported.");
    }//  ww w  . ja v  a 2  s .c  om

    if (metadata.getSslSecurityProfile() == null) {
        errors.rejectValue("sslSecurityProfile", null, "SSL/TLS Security profile must be specified.");
    } else if (!"pkix".equalsIgnoreCase(metadata.getSslSecurityProfile())
            && !"metaiop".equals(metadata.getSslSecurityProfile())) {
        errors.rejectValue("sslSecurityProfile", null, "Selected value is not supported.");
    }

    if (metadata.isIncludeDiscovery() && metadata.getCustomDiscoveryURL() != null
            && metadata.getCustomDiscoveryURL().length() > 0) {
        try {
            new URL(metadata.getCustomDiscoveryURL());
        } catch (MalformedURLException e) {
            errors.rejectValue("customDiscoveryURL", null, "Value is not a valid URL.");
        }
    }

    if (metadata.isIncludeDiscovery() && metadata.getCustomDiscoveryResponseURL() != null
            && metadata.getCustomDiscoveryResponseURL().length() > 0) {
        try {
            new URL(metadata.getCustomDiscoveryResponseURL());
        } catch (MalformedURLException e) {
            errors.rejectValue("customDiscoveryResponseURL", null, "Value is not a valid URL.");
        }
    }

    // Bindings
    if (metadata.getSsoBindings() == null || metadata.getSsoBindings().length == 0) {
        errors.rejectValue("ssoBindings", null, "At least one binding must be specified.");
    }

    // Default binding
    if (metadata.getSsoDefaultBinding() != null && metadata.getSsoBindings() != null) {
        boolean found = false;
        for (String binding : metadata.getSsoBindings()) {
            if (binding.equals(metadata.getSsoDefaultBinding())) {
                found = true;
                break;
            }
        }
        if (!found) {
            errors.rejectValue("ssoDefaultBinding", null, "Default binding must be selected as included.");
        }
    }

    if (metadata.getNameID() == null || metadata.getNameID().length == 0) {
        errors.rejectValue("nameID", null, "At least one NameID must be selected.");
    }

    try {
        if (!errors.hasErrors() && metadata.isStore()) {
            EntityDescriptor entityDescriptor = manager.getEntityDescriptor(metadata.getEntityId());
            if (entityDescriptor != null) {
                errors.rejectValue("entityId", null, "Selected entity ID is already used.");
            }
            String idForAlias = manager.getEntityIdForAlias(metadata.getAlias());
            if (idForAlias != null) {
                errors.rejectValue("alias", null, "Selected alias is already used.");
            }
        }
    } catch (MetadataProviderException e) {
        throw new RuntimeException("Error loading alias data", e);
    }

}

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

public void validate(Object clazz, Errors e) {
    PasswordReset obj = (PasswordReset) clazz;
    ValidationUtils.rejectIfEmptyOrWhitespace(e, "emailAddress", "error.required",
            new Object[] { "Email Address" });
    if (!StringUtil.isEmpty(obj.getEmailAddress()) && !userDao.isRegisteredByEmail(obj.getEmailAddress())) {
        e.rejectValue("emailAddress", "msg.email-address-is-not-registered",
                "Sorry, but your email address is not registered.");
    }//from  w  w w.  j  a v  a2 s.  co  m
    ValidationUtils.rejectIfEmptyOrWhitespace(e, "password", "error.required", new Object[] { "Password" });
    ValidationUtils.rejectIfEmptyOrWhitespace(e, "confirmPassword", "error.required",
            new Object[] { "Confirm Password" });
    if (!StringUtil.isEmpty(obj.getPassword()) && (obj.getPassword().length() < 6)) {
        e.reject("err.your-password-should-be-at-least-6-characters-long",
                "Your password should be at least 6 characters long.");
    }
    if (!StringUtil.isEmpty(obj.getPassword()) && !StringUtil.isEmpty(obj.getConfirmPassword())
            && !obj.getPassword().equals(obj.getConfirmPassword())) {
        e.reject("err.your-password-confirmation-did-not-match-with-password",
                "Your password confirmation did not match with password.");
    }
}

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

public void validate(Object object, Errors errors) {
    UserGroup userGroup = (UserGroup) object;
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "error.required", new Object[] { "Name" });
    UserGroup lookFor = new UserGroup();
    lookFor.setName(userGroup.getName());
    List<UserGroup> groups = userGroupService.findByExample(lookFor);
    if (groups != null && !groups.isEmpty()) {
        if (userGroup.isNew()) {
            errors.reject("error.user-group.duplicate", new Object[] { userGroup.getName() },
                    userGroup.getName());
        } else {//  w  w w.  java2  s  . co m
            UserGroup found = groups.get(0);
            if (!found.getId().equals(userGroup.getId())) {
                errors.reject("error.user-group.duplicate", new Object[] { userGroup.getName() },
                        userGroup.getName());
            }
        }
    }
}

From source file:com.oscgc.security.saml.idp.web.contoller.MetadataValidator.java

public void validate(Object target, Errors errors) {

    MetadataForm metadata = (MetadataForm) target;

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "entityId", "required", "Entity id must be set.");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "alias", "required", "Alias must be set.");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "baseURL", "required", "Base URL is required.");

    if (metadata.getSecurityProfile() == null) {
        errors.rejectValue("securityProfile", null, "Security profile must be specified.");
    } else if (!"pkix".equalsIgnoreCase(metadata.getSecurityProfile())
            && !"metaiop".equals(metadata.getSecurityProfile())) {
        errors.rejectValue("securityProfile", null, "Selected value is not supported.");
    }/* ww w.  j a  va  2  s. co m*/

    if (metadata.getSslSecurityProfile() == null) {
        errors.rejectValue("sslSecurityProfile", null, "SSL/TLS Security profile must be specified.");
    } else if (!"pkix".equalsIgnoreCase(metadata.getSslSecurityProfile())
            && !"metaiop".equals(metadata.getSslSecurityProfile())) {
        errors.rejectValue("sslSecurityProfile", null, "Selected value is not supported.");
    }

    if (metadata.isIncludeDiscovery() && metadata.getCustomDiscoveryURL() != null
            && metadata.getCustomDiscoveryURL().length() > 0) {
        try {
            new URL(metadata.getCustomDiscoveryURL());
        } catch (MalformedURLException e) {
            errors.rejectValue("customDiscoveryURL", null, "Value is not a valid URL.");
        }
    }

    if (metadata.isIncludeDiscovery() && metadata.getCustomDiscoveryResponseURL() != null
            && metadata.getCustomDiscoveryResponseURL().length() > 0) {
        try {
            new URL(metadata.getCustomDiscoveryResponseURL());
        } catch (MalformedURLException e) {
            errors.rejectValue("customDiscoveryResponseURL", null, "Value is not a valid URL.");
        }
    }

    // Bindings
    if (metadata.getSsoBindings() == null || metadata.getSsoBindings().length == 0) {
        errors.rejectValue("ssoBindings", null, "At least one binding must be specified.");
    }

    // Default binding
    if (metadata.getSsoDefaultBinding() != null && metadata.getSsoBindings() != null) {
        boolean found = false;
        for (String binding : metadata.getSsoBindings()) {
            if (binding.equals(metadata.getSsoDefaultBinding())) {
                found = true;
                break;
            }
        }
        if (!found) {
            errors.rejectValue("ssoDefaultBinding", null, "Default binding must be selected as included.");
        }
    }

    if (metadata.getNameID() == null || metadata.getNameID().length == 0) {
        errors.rejectValue("nameID", null, "At least one NameID must be selected.");
    }

    try {
        if (!errors.hasErrors() && metadata.isStore()) {
            EntityDescriptor entityDescriptor = manager.getEntityDescriptor(metadata.getEntityId());
            if (entityDescriptor != null) {
                errors.rejectValue("entityId", null, "Selected entity ID is already used.");
            }
            String idForAlias = manager.getEntityIdForAlias(metadata.getAlias());
            if (idForAlias != null) {
                errors.rejectValue("alias", null, "Selected alias is already used.");
            }
        }
    } catch (MetadataProviderException e) {
        throw new RuntimeException("Error loading alias data", e);
    }

}

From source file:commun.UserValidator.java

@Override
public void validate(Object o, Errors errors) {
    UserEntity user = (UserEntity) o;//from  ww  w  .j ava2s . com
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "profile.firstName",
            "NotEmpty.editUser.profile.firstName", "Can not be empty");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "profile.lastName", "NotEmpty.editUser.profile.lastName",
            "Can not be empty");

    String phone = user.getProfile().getPhone();
    if (!phone.equals("") && !validPhoneNumber(user.getProfile().getPhone())) {
        errors.rejectValue("profile.phone", "Pattern.editUser.profile.phone", "Invalid mobile number");
    }
    String description = user.getProfile().getDescription();
    if (!description.equals("") && user.getProfile().getDescription().length() < 5) {
        errors.rejectValue("profile.description", "Pattern.editUser.profile.description",
                "Minimun 5 characters");
    }

    try {
        Double height = user.getProfile().getPhysical().getHeight();
        if (height != null && (height < 1.0 || height > 3.0)) {
            errors.rejectValue("profile.physical.height", "Pattern.editUser.profile.physical.height",
                    "Must be between 1.0 and 3.0 m");
        }
    } catch (NumberFormatException e) {
        errors.rejectValue("profile.physical.height", "Pattern.editUser.profile.physical.height",
                "Must be between a number");

    }
    try {
        Double weight = user.getProfile().getPhysical().getWeight();
        if (weight != null && (weight < 10 || weight > 400)) {
            errors.rejectValue("profile.physical.weight", "Pattern.editUser.profile.physical.weight",
                    "Must be between 10 and 400 kg");
        }
    } catch (NumberFormatException e) {
        errors.rejectValue("profile.physical.weight", "Pattern.editUser.profile.physical.weight",
                "Must be between a number");

    }
}

From source file:org.jasig.schedassist.web.owner.statistics.VisitorHistoryFormBackingObjectValidator.java

public void validate(Object target, Errors errors) {
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "visitorUsername", "visitorUsername.blank",
            "No person matching your input was found.");

    VisitorHistoryFormBackingObject fbo = (VisitorHistoryFormBackingObject) target;
    if (null == fbo.getStartTime()) {
        errors.rejectValue("startTime", "startTime.notset", "Start time must be set (format MM/dd/YYYY).");
    }/*from w w  w  .java 2  s .  c  om*/

    if (null == fbo.getEndTime()) {
        errors.rejectValue("endTime", "endTime.notset", "End time must be set (format MM/dd/YYYY).");
    }
}

From source file:tld.mydomain.example.site.form.ContactFormValidator.java

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

    //ContactForm contactForm = (ContactForm) target;

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "required.name", "Field name is required.");

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "email", "required.email", "Field email is required.");

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "message", "required.message",
            "Field message is required.");
}

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

public void validate(Object clazz, Errors e) {
    PasswordReset obj = (PasswordReset) clazz;
    if (StringUtil.isEmpty(obj.getCipher())) {
        ValidationUtils.rejectIfEmptyOrWhitespace(e, "emailAddress", "error.required",
                new Object[] { "Email Address" });
        ValidationUtils.rejectIfEmptyOrWhitespace(e, "token", "error.required",
                new Object[] { "Confirmation Code" });
    } else {//  www. j a  v a 2s. c  o  m
        ValidationUtils.rejectIfEmptyOrWhitespace(e, "cipher", "error.required",
                new Object[] { "Link Cipher Code" });
    }
}

From source file:com.firstclarity.magnolia.study.blossom.sample.ContactFormValidator.java

@Override
public void validate(Object target, Errors errors) {
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "required", "Name is required");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "email", "required", "E-mail is required");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "message", "required", "Message is required");
}

From source file:com.iana.dver.controller.validators.RegistrationValidator.java

public void validateStep1(Object target, Errors errors) {
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "dverUserVO.companyName", "required.companyName",
            "Company Name can not be blank.");

    if (Integer.parseInt(errors.getFieldValue("dverUserVO.userType").toString()) == 0) {
        errors.rejectValue("dverUserVO.userType", "required.user.type", "Please Select User Type.");
    }//  ww  w.  jav  a2s .c  om

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "dverUserVO.scac", "required.scac",
            "SCAC Code can not be blank.");
    // unique SCAC validation;
    if (errors.getFieldError("dverUserVO.scac") == null
            && errors.getFieldError("dverUserVO.userType") == null) {
        String scac = errors.getFieldValue("dverUserVO.scac").toString();
        String userType = Integer.parseInt(errors.getFieldValue("dverUserVO.userType").toString()) == 2 ? "IEP"
                : "MC";
        if (isScacExists(scac, userType)) {
            errors.rejectValue("dverUserVO.scac", "scac.nonunique",
                    "The provided SCAC code already exists for " + userType + " user.");
        }
    }

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "dverUserVO.usDOT", "required.usDOT",
            "USDOT can not be blank.");
    // unique USDOT validation;
    if (errors.getFieldError("dverUserVO.usDOT") == null
            && errors.getFieldError("dverUserVO.userType") == null) {
        String usdot = errors.getFieldValue("dverUserVO.usDOT").toString();
        String userType = Integer.parseInt(errors.getFieldValue("dverUserVO.userType").toString()) == 2 ? "IEP"
                : "MC";

        if (isUsDotExists(usdot, userType)) {
            errors.rejectValue("dverUserVO.usDOT", "usdot.nonunique",
                    "The provided DOT code already exists for " + userType + " user.");
        }
    }

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "dverUserVO.firstName", "required.firstName",
            "First Name can not be blank.");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "dverUserVO.lastName", "required.lastName",
            "Last Name can not be blank.");

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "dverUserVO.title", "required.title",
            "Title can not be blank.");

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "dverUserVO.email", "required.email",
            "Email can not be blank.");

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "dverUserVO.phone", "required.phone",
            "Phone No. can not be blank.");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "dverUserVO.fax", "required.fax",
            "Fax No. can not be blank.");

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "dverUserVO.address1", "required.address1",
            "Address can not be blank.");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "dverUserVO.city", "required.city",
            "City can not be blank.");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "dverUserVO.state", "required.state",
            "State Code can not be blank.");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "dverUserVO.zipCode", "required.zipCode",
            "ZIP Code can not be blank.");
}