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:de.ingrid.interfaces.csw.admin.validation.AbstractValidator.java

public void rejectIfEmptyOrWhitespace(final Errors errors, final String field) {
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, field, getErrorKey(_typeClass, field, "empty"));
}

From source file:it.cilea.osd.jdyna.validator.DTOSoggettoValidator.java

/** 
 * Verify the uniqueness of the subject.
 *///w w w .j  a  v a2  s.  c  o m
public void validateVoce(Soggettario soggettario, String voce, Errors errors) {

    if (voce != null && soggettario != null) {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "voce", "error.message.fallita.campo.obbligatorio");
        ValidationResult result = validatorService.controllaVoceSuSoggettario(soggettario, voce);
        if (!result.isSuccess()) {
            errors.rejectValue("voce", result.getMessage());
        }

    }
}

From source file:com.apress.progwt.server.web.domain.validation.CreateUserRequestValidator.java

private void doStandardValidation(CreateUserRequestCommand comm, Errors errors) {

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "username", "required");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "required");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password2", "required");

    // username must have no '.' || '=' for openid compatibility
    if (userService.couldBeOpenID(comm.getUsername())) {
        errors.rejectValue("username", "invalid.username");
    }/*from   ww  w  . j  av a  2s . c om*/

    // spaces would break email functionality
    if (comm.getUsername().contains(" ")) {
        errors.rejectValue("username", "invalid.username.nospaces");
    }

    // generalemail compatibility
    if (!comm.getUsername().matches("([a-zA-Z0-9_\\-])+")) {
        errors.rejectValue("username", "invalid.username");
    }
    if (comm.getUsername().length() < MIN_LENGTH) {
        errors.rejectValue("username", "invalid.username.length");
    }
    if (comm.getPassword().length() < MIN_LENGTH) {
        errors.rejectValue("password", "invalid.password.length");
    }
    if (comm.getUsername().equals("anonymousUser")) {
        errors.rejectValue("username", "invalid.username");
    }

    // username != password
    if (comm.getPassword().equals(comm.getUsername())) {
        errors.rejectValue("username", "invalid.password.equalsuser");
    }

    // must have the same password
    if (!comm.getPassword().equals(comm.getPassword2())) {
        errors.rejectValue("password2", "invalid.password2");
    }

    if (userService.exists(comm.getUsername())) {
        errors.rejectValue("username", "invalid.username.exists");
    }
    if (UserServiceImpl.ANONYMOUS.equals(comm.getUsername())) {
        errors.rejectValue("username", "invalid.username.exists");
    }
}

From source file:com.cws.us.pws.validators.SearchValidator.java

/**
 * TODO: Add in the method description/comments
 *
 * @param target// w  w w . j  a  v  a2  s. c o  m
 * @param errors
 * @see org.springframework.validation.Validator#validate(java.lang.Object, org.springframework.validation.Errors)
 */
@Override
public void validate(final Object target, final Errors errors) {
    final String methodName = SearchValidator.CNAME + "#validate(final <Class> request)";

    if (DEBUG) {
        DEBUGGER.debug(methodName);
        DEBUGGER.debug("target: {}", target);
        DEBUGGER.debug("errors: {}", errors);
    }

    final Pattern pattern = Pattern.compile("^[A-Za-z0-9]+(?:[\\s-][A-Za-z0-9]+)*$");

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "firstName", "email.first.name.required");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "lastName", "email.last.name.required");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "messageTo", "email.source.addr.required");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "messageSubject", "email.message.subject.required");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "messageBody", "email.message.body.required");

    final SearchRequest request = (SearchRequest) target;

    if (DEBUG) {
        DEBUGGER.debug("SearchRequest: {}", request);
    }

    if (!(pattern.matcher(request.getSearchTerms()).matches())) {
        errors.reject("searchTerms");
    }
}

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

/**
 * Validate the {@link FileUploadBean} of the {@link ImportBean} for
 * erroneous input./*ww  w . jav a  2s.  co m*/
 * 
 * @param fileUploadBean the object container of the file to validate
 * @param errors the errors object
 */
private void validate(FileUploadBean fileUploadBean, Errors errors) {
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, FIELD_FILE_NAME, fileUploadBean.getFile().getName());
}

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

/**
 * Validates the object of the DonationType.
 * /*from w  w w . j  ava  2  s .c  o  m*/
 * @param object - Populated object of DonationType 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);
    DonationType donationType = (DonationType) object;
    Pattern stringOnly = Pattern
            .compile(ValidatorExpressionUtil.getValidatorPattern(REFERENCE_DONATIONTYPE_VALIDATOR));
    String descript = donationType.getDescription().trim();
    String description = descript.replaceAll(STRING_SPACE, STRING_EMPTY);
    Matcher makeMatch = stringOnly.matcher(description);

    if (makeMatch.find()) {

        err.rejectValue(DESCRIPTION, AkuraWebConstant.MISMATCH_ERROR);
    }

    if (descript.length() > LENGTH_DESCRIPTION) {

        err.rejectValue(DESCRIPTION, REF_UI_FIELD_LENGTH);
    }
}

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

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

    StudyMedium studyMedium = (StudyMedium) target;
    String name = studyMedium.getStudyMediumName().trim();
    // required field
    if (name.equals(EMPTY_STRING)) {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, PROPERTY, REF_UI_MANDATORY_FIELD_REQUIRED);
    }

    String validatorPattern = ValidatorExpressionUtil.getValidatorPattern(STUDYMEDIUM_VALIDATOR_PATTERN);
    Pattern stringOnly = Pattern.compile(validatorPattern);

    name = name.replaceAll(SPACE_STRING, EMPTY_STRING);

    Matcher makeMatch = stringOnly.matcher(name);
    if (makeMatch.find()) {
        errors.rejectValue(PROPERTY, AkuraWebConstant.MISMATCH_ERROR);
    }

}

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

/**
 * Validates the object of the BloodGroup.
 * /*from   ww w  . j av a2  s.com*/
 * @param target - Populated object of BloodGroup 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) {

    Matcher makeMatch = null;
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, FIELD_NAME_DESCRIPTION, ERROR_MSG_DESCRIPTION);
    BloodGroup bloodGroup = (BloodGroup) target;
    String description = bloodGroup.getDescription().trim();

    if (description.length() > 0) {

        if (!(description.endsWith(PLUS) || description.endsWith(MINUS))) {
            errors.rejectValue(FIELD_NAME_BLOOD_GROUP_ID, ERROR_MSG_FIELD_TYPE);
        }

        if (description.endsWith(PLUS) || description.endsWith(MINUS)) {
            String firstCharacters = description.substring(0, (description.length() - 1));

            if (firstCharacters.equals("")) {
                errors.rejectValue(FIELD_NAME_DESCRIPTION, ERROR_MSG_FIELD_TYPE);
            } else {
                Pattern stringOnly = Pattern
                        .compile(ValidatorExpressionUtil.getValidatorPattern(REFERENCE_BLOODGROUP_VALIDATOR));
                makeMatch = stringOnly.matcher(firstCharacters);
                if (makeMatch.find()) {
                    errors.rejectValue(FIELD_NAME_DESCRIPTION, ERROR_MSG_FIELD_TYPE);
                }
            }

        }
    }
}

From source file:it.cilea.osd.jdyna.validator.ClassificazioneValidator.java

/** 
 *  Verify that the code is filled and unique withing the tree. 
 *///from  www .j a va 2  s  .c om
public void validateTopLevel(Object object, Errors errors) {
    DTOGestioneAlberoClassificatore classificazione = (DTOGestioneAlberoClassificatore) object;

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "topNome", "error.message.fallita.campo.obbligatorio");

    Integer albero = classificazione.getId();
    String codice = classificazione.getTopCodice();
    if (codice != null && albero != null) {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "topCodice",
                "error.message.fallita.campo.obbligatorio");
        ValidationResult result = ((IValidatorClassificationService) getValidatorService())
                .controllaCodiceClassificazioneByIdAlberoECodice(albero, codice);
        if (!result.isSuccess()) {
            errors.rejectValue("topCodice", result.getMessage());
        }

    }
}

From source file:org.jasig.portlet.cms.controller.PostValidator.java

@Override
public void validate(final Object arg0, final Errors errors) {
    final Post post = (Post) arg0;

    if (logger.isDebugEnabled())
        logger.debug("Validaing post content " + errors.getFieldValue("content"));
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "content", "invalid.post.content.empty");
    if (errors.getFieldValue("content") != null)
        if (post.getContent().trim().isEmpty() && !errors.hasErrors())
            ValidationUtils.rejectIfEmptyOrWhitespace(errors, "content", "invalid.post.content.empty");

    final RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
    final PortletRequest request = ((PortletRequestAttributes) requestAttributes).getRequest();
    final PortletPreferencesWrapper pref = new PortletPreferencesWrapper(request);

    if (pref.isXssValidationEnabled())
        validatePostContent(post, errors);

    validatePostAttachments(post, errors);

    if (logger.isDebugEnabled())
        if (errors.getErrorCount() == 0)
            logger.debug("Validated post successfully without errors");
        else//from w  w w .  j  av a2 s . c om
            logger.debug("Rejected post with " + errors.getErrorCount() + " errors");
}