From 324a6ea3f91e9aadc8e7cb303a7f86a654708f4c Mon Sep 17 00:00:00 2001 From: rjy7 Date: Fri, 5 Mar 2010 17:51:00 +0000 Subject: [PATCH] NIHVIVO-138 Distinguish format validation from nonempty validation: specification of format validation now does not entail a requirement that the field be nonempty. --- .../edit/n3editing/BasicValidation.java | 178 +++++++++--------- 1 file changed, 94 insertions(+), 84 deletions(-) diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/BasicValidation.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/BasicValidation.java index 39e208150..091edd838 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/BasicValidation.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/BasicValidation.java @@ -30,7 +30,7 @@ public class BasicValidation { EditConfiguration editConfig; public BasicValidation(EditConfiguration editConfig, EditSubmission editSub){ - this.editConfig = editConfig; + this.editConfig = editConfig; Map> validatorsForFields = new HashMap>(); for(String fieldName: editConfig.getFields().keySet()){ Field field = editConfig.getField(fieldName); @@ -57,10 +57,7 @@ public class BasicValidation { String validateMsg = validate(validationType,value); if( validateMsg != null) { errors.put(name,validateMsg); - if (validateMsg == REQUIRED_FIELD_EMPTY_MSG) { - continue; - } - } + } } } } @@ -72,69 +69,78 @@ public class BasicValidation { HashMap errors = new HashMap(); for( String name : editConfig.getLiteralsOnForm() ){ - Literal literal = varNamesToValues.get(name); - Listvalidations = varsToValidations.get(name); - if( validations != null ){ - for( String validationType : validations){ - String value = null; - try{ - if( literal != null ) - value = literal.getString(); - }catch(Throwable th){ - log.debug("could not convert literal to string" , th); - } - String validateMsg = validate(validationType, value ); + Literal literal = varNamesToValues.get(name); + Listvalidations = varsToValidations.get(name); + if( validations != null ){ + // NB this is case-sensitive + boolean isRequiredField = validations.contains("nonempty"); + for( String validationType : validations){ + String value = null; + try{ + if( literal != null ) + value = literal.getString(); + }catch(Throwable 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); 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) { - break; - } } } - } + } } return errors; } - public MapvalidateFiles(Map> fileItemMap) { - - HashMap errors = new HashMap(); - for(String name: editConfig.getFilesOnForm() ){ - List validators = varsToValidations.get(name); - for( String validationType : validators){ - String validateMsg = validate(validationType, fileItemMap.get(name)); - if( validateMsg != null ) { - errors.put(name, validateMsg); - } - } - } - return errors; - } - + public MapvalidateFiles(Map> fileItemMap) { + + HashMap errors = new HashMap(); + for(String name: editConfig.getFilesOnForm() ){ + List validators = varsToValidations.get(name); + for( String validationType : validators){ + String validateMsg = validate(validationType, fileItemMap.get(name)); + if( validateMsg != null ) { + errors.put(name, validateMsg); + } + } + } + return errors; + } + private String validate(String validationType, List fileItems) { - if( "nonempty".equalsIgnoreCase(validationType)){ - if( fileItems == null || fileItems.size() == 0 ){ - 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 null; - } + if( "nonempty".equalsIgnoreCase(validationType)){ + if( fileItems == null || fileItems.size() == 0 ){ + 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 null; + } - /* null indicates success. A returned string is the validation + /* null indicates success. A returned string is the validation 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,15 +148,15 @@ 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 ); - if ( errorMsg == null ) { - return SUCCESS; - } else { - return errorMsg; - } + String datatypeURI = validationType.substring(9); + String errorMsg = validateAgainstDatatype( value, datatypeURI ); + if ( errorMsg == null ) { + return SUCCESS; + } else { + return errorMsg; + } } return null; // } @@ -162,24 +168,24 @@ public class BasicValidation { private static DatatypeDaoJena ddao = null; public static synchronized String validateAgainstDatatype( String value, String datatypeURI ) { - if ( ( datatypeURI != null ) && ( datatypeURI.length()>0 ) ) { - RDFDatatype datatype = TypeMapper.getInstance().getSafeTypeByName(datatypeURI); - if ( datatype == null ) { - throw new RuntimeException( datatypeURI + " is not a recognized datatype"); - } - if ( datatype.isValid(value) ) { - return null; - } else { - // TODO: better way of getting more friendly names for common datatypes - if (ddao == null) { - ddao = new DatatypeDaoJena(new WebappDaoFactoryJena(ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM))); - } - Datatype dtype = ddao.getDatatypeByURI(datatypeURI); - String dtypeMsg = (dtype != null) ? dtype.getName() : datatypeURI; - return " Please correct this value. (Must be a valid " + dtypeMsg + ")"; - } - } - return null; + if ( ( datatypeURI != null ) && ( datatypeURI.length()>0 ) ) { + RDFDatatype datatype = TypeMapper.getInstance().getSafeTypeByName(datatypeURI); + if ( datatype == null ) { + throw new RuntimeException( datatypeURI + " is not a recognized datatype"); + } + if ( datatype.isValid(value) ) { + return null; + } else { + // TODO: better way of getting more friendly names for common datatypes + if (ddao == null) { + ddao = new DatatypeDaoJena(new WebappDaoFactoryJena(ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM))); + } + Datatype dtype = ddao.getDatatypeByURI(datatypeURI); + String dtypeMsg = (dtype != null) ? dtype.getName() : datatypeURI; + return " Please correct this value: must be a valid " + dtypeMsg + "."; + } + } + return null; } private void checkValidations(){ @@ -187,10 +193,10 @@ public class BasicValidation { for( String key : varsToValidations.keySet()){ for( String validator : varsToValidations.get(key)){ if( ! basicValidations.contains( validator)) { - if ( ! ( ( validator != null) && - ( validator.indexOf( "datatype:" ) == 0 ) ) ) { - unknown.add(validator); - } + if ( ! ( ( validator != null) && + ( validator.indexOf( "datatype:" ) == 0 ) ) ) { + unknown.add(validator); + } } } } @@ -199,11 +205,15 @@ 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}";