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:
parent
794e5d9f64
commit
324a6ea3f9
1 changed files with 94 additions and 84 deletions
|
@ -57,9 +57,6 @@ public class BasicValidation {
|
|||
String validateMsg = validate(validationType,value);
|
||||
if( validateMsg != null) {
|
||||
errors.put(name,validateMsg);
|
||||
if (validateMsg == REQUIRED_FIELD_EMPTY_MSG) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -75,6 +72,8 @@ public class BasicValidation {
|
|||
Literal literal = varNamesToValues.get(name);
|
||||
List<String>validations = varsToValidations.get(name);
|
||||
if( validations != null ){
|
||||
// NB this is case-sensitive
|
||||
boolean isRequiredField = validations.contains("nonempty");
|
||||
for( String validationType : validations){
|
||||
String value = null;
|
||||
try{
|
||||
|
@ -83,14 +82,19 @@ public class BasicValidation {
|
|||
}catch(Throwable th){
|
||||
log.debug("could not convert literal to string" , th);
|
||||
}
|
||||
String validateMsg = validate(validationType, value );
|
||||
if( validateMsg != null) {
|
||||
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) {
|
||||
// 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);
|
||||
if( validateMsg != null) {
|
||||
errors.put(name,validateMsg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -117,11 +121,11 @@ public class BasicValidation {
|
|||
private String validate(String validationType, List<FileItem> fileItems) {
|
||||
if( "nonempty".equalsIgnoreCase(validationType)){
|
||||
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{
|
||||
FileItem fileItem = fileItems.get(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.
|
||||
*/
|
||||
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( value == null || value.trim().length() == 0 )
|
||||
if( isEmpty(value) )
|
||||
return REQUIRED_FIELD_EMPTY_MSG;
|
||||
}
|
||||
// Format validation
|
||||
|
@ -142,7 +148,7 @@ public class BasicValidation {
|
|||
if( isDate( value))
|
||||
return SUCCESS;
|
||||
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 ) {
|
||||
String datatypeURI = validationType.substring(9);
|
||||
String errorMsg = validateAgainstDatatype( value, datatypeURI );
|
||||
|
@ -176,7 +182,7 @@ public class BasicValidation {
|
|||
}
|
||||
Datatype dtype = ddao.getDatatypeByURI(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;
|
||||
|
@ -200,10 +206,14 @@ public class BasicValidation {
|
|||
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 */
|
||||
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" */
|
||||
private final String dateRegex = "((1[012])|([1-9]))/((3[10])|([12][0-9])|([1-9]))/[\\d]{4}";
|
||||
|
|
Loading…
Add table
Reference in a new issue