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.medici.bia.validator.peoplebase.EditDetailsPersonValidator.java

/**
 * /*from ww w.jav  a2 s  .  c o  m*/
 * @param personId
 * @param errors
 */
public void validatePersonId(Integer personId, Errors errors) {
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "personId", "error.personId.null");

    if (!errors.hasErrors()) {
        if (personId > 0) {
            try {
                if (getPeopleBaseService().findPerson(personId) == null) {
                    errors.reject("peopleId", "error.peopleId.notfound");
                }
            } catch (ApplicationThrowable ath) {

            }
        }
    }
}

From source file:org.medici.bia.validator.peoplebase.ShowUploadPortraitPersonValidator.java

/**
 * //  www.j a v a  2s.  com
 * @param personId
 * @param errors
 */
public void validatePersonId(Integer personId, Errors errors) {
    if (errors.hasErrors()) {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "personId", "error.personId.null");

        if (!errors.hasErrors()) {
            if (personId > 0) {
                try {
                    if (getPeopleBaseService().findPerson(personId) == null) {
                        errors.reject("peopleId", "error.peopleId.notfound");
                    }
                } catch (ApplicationThrowable ath) {

                }
            }
        }
    }
}

From source file:org.medici.bia.validator.user.ShowUploadPortraitUserValidator.java

/**
 * /*from  w  w  w .  j a v a 2  s.  c o  m*/
 * @param personId
 * @param errors
 */
public void validateAccount(String account, Errors errors) {
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "account", "error.account.null");

    if (!errors.hasErrors()) {
        if (account != null) {
            try {
                if (getUserService().findUser(account) == null) {
                    errors.reject("account", "error.account.notfound");
                }
            } catch (ApplicationThrowable ath) {

            }
        }
    }
}

From source file:org.openmrs.module.kenyaui.form.ValidatingCommandObject.java

/**
 * Rejects the specified property if it is empty
 * @param errors the errors/*w  ww  . ja  v a  2  s  .  com*/
 * @param field the field name
 */
public void require(Errors errors, String field) {
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, field, KenyaUiConstants.MODULE_ID + ".error.required");
}

From source file:org.openmrs.module.radiology.report.web.VoidRadiologyReportRequestValidator.java

/**
 * Checks the form object for any inconsistencies/errors
 *
 * @see Validator#validate(Object, Errors)
 * @should fail validation if void reason is null or empty or whitespaces only
 * @should pass validation if all fields are correct
 *///www .ja  v  a 2  s.  co  m
@Override
public void validate(Object obj, Errors errors) {
    final VoidRadiologyReportRequest voidRadiologyReportRequest = (VoidRadiologyReportRequest) obj;
    if (voidRadiologyReportRequest == null) {
        errors.reject("error.general");
    } else {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "voidReason", "error.null");
    }
}

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

/**
 * @see org.springframework.validation.Validator#validate(java.lang.Object, org.springframework.validation.Errors)
 * @should fail validation if Alert Text is null or empty or whitespace
 * @should pass validation if all required values are set
 * @should pass validation if field lengths are correct
 * @should fail validation if field lengths are not correct
 *///w w w.  j  av  a2  s  .  c o  m
public void validate(Object obj, Errors errors) {
    Alert alert = (Alert) obj;
    if (alert == null) {
        errors.rejectValue("alert", "error.general");
    } else {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "text", "Alert.text.required");
        ValidateUtil.validateFieldLengths(errors, obj.getClass(), "text");
    }
}

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

/**
 * @see org.springframework.validation.Validator#validate(java.lang.Object, org.springframework.validation.Errors)
 * @should require name/*from w w  w  .ja  va 2  s .  c om*/
 * @should require minOccurs
 * @should not allow maxOccurs less than 1
 * @should not allow maxOccurs less than minOccurs
 * @should require datatypeClassname
 * @should require DatatypeConfiguration if Datatype equals Regex-Validated Text
 * @should pass validation if all required values are set
 * @should pass validation if field lengths are correct
 * @should fail validation if field lengths are not correct
 */
@Override
public void validate(Object target, Errors errors) {
    @SuppressWarnings("unchecked")
    T attributeType = (T) target;

    if (attributeType == null) {
        errors.reject("error.general");
    } else {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "error.name");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "minOccurs", "error.null");

        Integer minOccurs = attributeType.getMinOccurs();
        Integer maxOccurs = attributeType.getMaxOccurs();

        if (minOccurs != null && minOccurs < 0) {
            errors.rejectValue("minOccurs", "AttributeType.minOccursShouldNotBeLessThanZero");
        }

        if (maxOccurs != null) {
            if (maxOccurs < 1) {
                errors.rejectValue("maxOccurs", "AttributeType.maxOccursShouldNotBeLessThanOne");
            } else if (maxOccurs < minOccurs) {
                errors.rejectValue("maxOccurs", "AttributeType.maxOccursShouldNotBeLessThanMinOccurs");
            }
        }

        if (StringUtils.isBlank(attributeType.getDatatypeClassname())) {
            errors.rejectValue("datatypeClassname", "error.null");
        } else {
            try {
                CustomDatatype<?> datatype = CustomDatatypeUtil.getDatatype(attributeType);
                if (datatype instanceof RegexValidatedTextDatatype
                        && StringUtils.isBlank(attributeType.getDatatypeConfig())) {
                    errors.rejectValue("datatypeConfig", "error.null");
                }
            } catch (Exception ex) {
                errors.rejectValue("datatypeConfig", "AttributeType.datatypeConfig.invalid",
                        new Object[] { ex.getMessage() }, "Invalid");
            }
        }

        // ensure that handler is suitable for datatype
        if (StringUtils.isNotEmpty(attributeType.getPreferredHandlerClassname())) {
            try {
                CustomDatatype<?> datatype = CustomDatatypeUtil.getDatatype(attributeType);
                CustomDatatypeHandler<?, ?> handler = CustomDatatypeUtil.getHandler(attributeType);
                if (!CustomDatatypeUtil.isCompatibleHandler(handler, datatype)) {
                    errors.rejectValue("preferredHandlerClassname",
                            "AttributeType.preferredHandlerClassname.wrongDatatype");
                }
            } catch (Exception ex) {
                errors.rejectValue("handlerConfig", "AttributeType.handlerConfig.invalid",
                        new Object[] { ex.getMessage() }, "Invalid");
            }
        }
        ValidateUtil.validateFieldLengths(errors, target.getClass(), "datatypeConfig", "handlerConfig");
    }
}

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

/**
 * Checks the form object for any inconsistencies/errors
 * //from  ww w.  ja v  a  2 s.c o m
 * @see org.springframework.validation.Validator#validate(java.lang.Object,
 *      org.springframework.validation.Errors)
 * @should fail validation if user is null or empty or whitespace
 * @should fail validation if name is already exist in non retired concept class
 * @should pass validation if description is null or empty or whitespace
 * @should pass validation if all required fields have proper values
 * @should pass validation if field lengths are correct
 * @should fail validation if field lengths are not correct
 */

public void validate(Object obj, Errors errors) {
    ConceptClass cc = (ConceptClass) obj;
    if (cc == null) {
        errors.rejectValue("conceptClass", "error.general");
    } else {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "error.name");
        if (!errors.hasErrors()) {
            ConceptClass exist = Context.getConceptService().getConceptClassByName(cc.getName());
            if (exist != null && !exist.isRetired()
                    && !OpenmrsUtil.nullSafeEquals(cc.getUuid(), exist.getUuid())) {
                errors.rejectValue("name", "conceptclass.duplicate.name");
            }
        }
        ValidateUtil.validateFieldLengths(errors, obj.getClass(), "name", "description", "retireReason");
    }
}

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

/**
 * Checks the form object for any inconsistencies/errors
 * /*from  w  ww.  ja v  a2 s  .  c  o  m*/
 * @see org.springframework.validation.Validator#validate(java.lang.Object,
 *      org.springframework.validation.Errors)
 * @should pass validation if description is null or empty or whitespace    *      
 * @should fail validation if name is null or empty or whitespace
 * @should pass validation if all required fields have proper values
 * @should pass validation if field lengths are correct
 * @should fail validation if field lengths are not correct
 */
public void validate(Object obj, Errors errors) {
    ConceptDatatype cd = (ConceptDatatype) obj;
    if (cd == null) {
        errors.rejectValue("conceptDatatype", "error.general");
    } else {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "error.name");
        ValidateUtil.validateFieldLengths(errors, obj.getClass(), "name", "hl7Abbreviation", "description",
                "retireReason");
    }
}

From source file:org.openmrs.validator.ConceptNameTagValidator.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 conceptNameTag is null
 * @should fail validation if tag is null or empty or whitespace
 * @should pass validation if tag does not exist and is not null, empty or whitespace
 * @should fail if the concept name tag is a duplicate
 * @should pass validation if field lengths are correct
 * @should fail validation if field lengths are not correct
 *//*w w  w .j ava 2 s  . c  o m*/

public void validate(Object obj, Errors errors) {
    ConceptNameTag cnt = (ConceptNameTag) obj;
    if (cnt == null) {
        throw new IllegalArgumentException("The parameter obj should not be null");
    } else {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "tag", "error.name");

        if (cnt.getTag() != null) {
            ConceptNameTag currentTag = Context.getConceptService().getConceptNameTagByName(cnt.getTag());
            if (currentTag != null && OpenmrsUtil.nullSafeEqualsIgnoreCase(currentTag.getTag(), cnt.getTag())) {
                errors.rejectValue("tag", "Concept.name.tag.duplicate");
            }
        }
        ValidateUtil.validateFieldLengths(errors, obj.getClass(), "tag", "voidReason");
    }
}