NIHVIVO-138 Distinguish format validation from nonempty validation: specification of format validation now does not entail a requirement that the field be nonempty.

This commit is contained in:
rjy7 2010-03-05 17:51:00 +00:00
parent 794e5d9f64
commit 324a6ea3f9

View file

@ -57,9 +57,6 @@ public class BasicValidation {
String validateMsg = validate(validationType,value); String validateMsg = validate(validationType,value);
if( validateMsg != null) { if( validateMsg != null) {
errors.put(name,validateMsg); errors.put(name,validateMsg);
if (validateMsg == REQUIRED_FIELD_EMPTY_MSG) {
continue;
}
} }
} }
} }
@ -75,6 +72,8 @@ public class BasicValidation {
Literal literal = varNamesToValues.get(name); Literal literal = varNamesToValues.get(name);
List<String>validations = varsToValidations.get(name); List<String>validations = varsToValidations.get(name);
if( validations != null ){ if( validations != null ){
// NB this is case-sensitive
boolean isRequiredField = validations.contains("nonempty");
for( String validationType : validations){ for( String validationType : validations){
String value = null; String value = null;
try{ try{
@ -83,14 +82,19 @@ public class BasicValidation {
}catch(Throwable th){ }catch(Throwable th){
log.debug("could not convert literal to string" , th); log.debug("could not convert literal to string" , th);
} }
// Empty field: if required, include only the empty field
// error message, not a format validation message. If non-required,
// don't do format validation, since that is both unnecessary and may
// incorrectly generate errors.
if (isEmpty(value)) {
if (isRequiredField) {
errors.put(name, REQUIRED_FIELD_EMPTY_MSG);
}
break;
}
String validateMsg = validate(validationType, value); String validateMsg = validate(validationType, value);
if( validateMsg != null) { if( validateMsg != null) {
errors.put(name,validateMsg); errors.put(name,validateMsg);
// If it's a required field that is empty, do not continue with
// other validations for this field.
if (validateMsg == REQUIRED_FIELD_EMPTY_MSG) {
break;
}
} }
} }
} }
@ -117,11 +121,11 @@ public class BasicValidation {
private String validate(String validationType, List<FileItem> fileItems) { private String validate(String validationType, List<FileItem> fileItems) {
if( "nonempty".equalsIgnoreCase(validationType)){ if( "nonempty".equalsIgnoreCase(validationType)){
if( fileItems == null || fileItems.size() == 0 ){ if( fileItems == null || fileItems.size() == 0 ){
return "a file must be entered for this field"; return "a file must be entered for this field.";
}else{ }else{
FileItem fileItem = fileItems.get(0); FileItem fileItem = fileItems.get(0);
if( fileItem == null || fileItem.getName() == null || fileItem.getName().length() < 1 || fileItem.getSize() < 0){ if( fileItem == null || fileItem.getName() == null || fileItem.getName().length() < 1 || fileItem.getSize() < 0){
return "a file must be entered for this field"; return "a file must be entered for this field.";
} }
} }
} }
@ -132,9 +136,11 @@ public class BasicValidation {
error message. error message.
*/ */
public String validate(String validationType, String value){ public String validate(String validationType, String value){
// Required field validation // Required field validation.
// For literals, testing empty required values in validateLiterals.
// This case may be needed for validation of other field types.
if( "nonempty".equalsIgnoreCase(validationType)){ if( "nonempty".equalsIgnoreCase(validationType)){
if( value == null || value.trim().length() == 0 ) if( isEmpty(value) )
return REQUIRED_FIELD_EMPTY_MSG; return REQUIRED_FIELD_EMPTY_MSG;
} }
// Format validation // Format validation
@ -142,7 +148,7 @@ public class BasicValidation {
if( isDate( value)) if( isDate( value))
return SUCCESS; return SUCCESS;
else else
return "must be in valid date format mm/dd/yyyy"; return "must be in valid date format mm/dd/yyyy.";
}else if( validationType.indexOf("datatype:") == 0 ) { }else if( validationType.indexOf("datatype:") == 0 ) {
String datatypeURI = validationType.substring(9); String datatypeURI = validationType.substring(9);
String errorMsg = validateAgainstDatatype( value, datatypeURI ); String errorMsg = validateAgainstDatatype( value, datatypeURI );
@ -176,7 +182,7 @@ public class BasicValidation {
} }
Datatype dtype = ddao.getDatatypeByURI(datatypeURI); Datatype dtype = ddao.getDatatypeByURI(datatypeURI);
String dtypeMsg = (dtype != null) ? dtype.getName() : datatypeURI; String dtypeMsg = (dtype != null) ? dtype.getName() : datatypeURI;
return " Please correct this value. (Must be a valid " + dtypeMsg + ")"; return " Please correct this value: must be a valid " + dtypeMsg + ".";
} }
} }
return null; return null;
@ -200,10 +206,14 @@ public class BasicValidation {
throw new Error( "Unknown basic validators: " + unknown.toArray()); throw new Error( "Unknown basic validators: " + unknown.toArray());
} }
private static boolean isEmpty(String value) {
return (value == null || value.trim().isEmpty());
}
/** we use null to indicate success */ /** we use null to indicate success */
public final static String SUCCESS = null; public final static String SUCCESS = null;
public final static String REQUIRED_FIELD_EMPTY_MSG = "This field must not be empty"; public final static String REQUIRED_FIELD_EMPTY_MSG = "This field must not be empty.";
/** regex for strings like "12/31/2004" */ /** regex for strings like "12/31/2004" */
private final String dateRegex = "((1[012])|([1-9]))/((3[10])|([12][0-9])|([1-9]))/[\\d]{4}"; private final String dateRegex = "((1[012])|([1-9]))/((3[10])|([12][0-9])|([1-9]))/[\\d]{4}";