Search in sources :

Example 16 with Field

use of org.openmrs.Field in project openmrs-core by openmrs.

the class FieldValidator method validate.

/**
 * Validates the given Field.
 * Ensures that the field name is present and valid
 *
 * @param obj The Field to validate.
 * @param errors Errors
 * @see org.springframework.validation.Validator#validate(java.lang.Object,
 *      org.springframework.validation.Errors)
 * @should fail if field name is null
 * @should fail if field name is empty
 * @should fail if field name is all whitespace
 * @should fail if selectMultiple is null
 * @should fail if retired is null
 * @should pass if name is ok and fieldType, selectMultiple, and retired are non-null
 * @should pass validation if field lengths are correct
 * @should fail validation if field lengths are not correct
 * should not fail if fieldType is null
 */
@Override
public void validate(Object obj, Errors errors) throws APIException {
    if (log.isDebugEnabled()) {
        log.debug(this.getClass().getName() + ".validate...");
    }
    if (obj == null || !(obj instanceof Field)) {
        throw new IllegalArgumentException("The parameter obj should not be null and must be of type " + Field.class);
    }
    Field field = (Field) obj;
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "error.null", "Field name is required");
    if (field.getSelectMultiple() == null) {
        errors.rejectValue("selectMultiple", "error.general");
    }
    if (field.getRetired() == null) {
        errors.rejectValue("retired", "error.general");
    }
    ValidateUtil.validateFieldLengths(errors, obj.getClass(), "name", "tableName", "attributeName", "retireReason");
}
Also used : Field(org.openmrs.Field)

Aggregations

Field (org.openmrs.Field)16 Test (org.junit.Test)13 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)13 FieldType (org.openmrs.FieldType)9 BindException (org.springframework.validation.BindException)8 Errors (org.springframework.validation.Errors)8 FormField (org.openmrs.FormField)7 Concept (org.openmrs.Concept)3 Form (org.openmrs.Form)2 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 ComplexObsHandler (org.openmrs.obs.ComplexObsHandler)1 SerializableComplexObsHandler (org.openmrs.obs.SerializableComplexObsHandler)1