From 794e5d9f64730faa53755665cd61e196156fe478 Mon Sep 17 00:00:00 2001 From: rjy7 Date: Fri, 5 Mar 2010 15:42:11 +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 | 37 +++++++++++++------ 1 file changed, 25 insertions(+), 12 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 c65f385d1..39e208150 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 @@ -55,8 +55,12 @@ public class BasicValidation { if( validations!= null){ for( String validationType : validations){ String validateMsg = validate(validationType,value); - if( validateMsg != null) + if( validateMsg != null) { errors.put(name,validateMsg); + if (validateMsg == REQUIRED_FIELD_EMPTY_MSG) { + continue; + } + } } } } @@ -80,8 +84,14 @@ public class BasicValidation { log.debug("could not convert literal to string" , th); } String validateMsg = validate(validationType, value ); - if( validateMsg != null) + 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; + } + } } } } @@ -93,18 +103,19 @@ public class BasicValidation { HashMap errors = new HashMap(); for(String name: editConfig.getFilesOnForm() ){ List validators = varsToValidations.get(name); - for( String valdationType : validators){ - String validateMsg = validate(valdationType, fileItemMap.get(name)); - if( validateMsg != null ) + for( String validationType : validators){ + String validateMsg = validate(validationType, fileItemMap.get(name)); + if( validateMsg != null ) { errors.put(name, validateMsg); + } } } return errors; } - private String validate(String valdationType, List fileItems) { - if( "nonempty".equalsIgnoreCase(valdationType)){ + 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{ @@ -121,12 +132,13 @@ public class BasicValidation { error message. */ public String validate(String validationType, String value){ + // Required field validation if( "nonempty".equalsIgnoreCase(validationType)){ if( value == null || value.trim().length() == 0 ) - return "this field must not be empty"; - else - return SUCCESS; - }else if("isDate".equalsIgnoreCase(validationType)){ + return REQUIRED_FIELD_EMPTY_MSG; + } + // Format validation + if("isDate".equalsIgnoreCase(validationType)){ if( isDate( value)) return SUCCESS; else @@ -191,7 +203,8 @@ public class BasicValidation { /** 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"; + /** 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 Pattern datePattern = Pattern.compile(dateRegex);