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
c41f94eebe
commit
794e5d9f64
1 changed files with 25 additions and 12 deletions
|
@ -55,8 +55,12 @@ public class BasicValidation {
|
||||||
if( validations!= null){
|
if( validations!= null){
|
||||||
for( String validationType : validations){
|
for( String validationType : validations){
|
||||||
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -80,8 +84,14 @@ public class BasicValidation {
|
||||||
log.debug("could not convert literal to string" , th);
|
log.debug("could not convert literal to string" , th);
|
||||||
}
|
}
|
||||||
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -93,18 +103,19 @@ public class BasicValidation {
|
||||||
HashMap<String,String> errors = new HashMap<String,String>();
|
HashMap<String,String> errors = new HashMap<String,String>();
|
||||||
for(String name: editConfig.getFilesOnForm() ){
|
for(String name: editConfig.getFilesOnForm() ){
|
||||||
List<String> validators = varsToValidations.get(name);
|
List<String> validators = varsToValidations.get(name);
|
||||||
for( String valdationType : validators){
|
for( String validationType : validators){
|
||||||
String validateMsg = validate(valdationType, fileItemMap.get(name));
|
String validateMsg = validate(validationType, fileItemMap.get(name));
|
||||||
if( validateMsg != null )
|
if( validateMsg != null ) {
|
||||||
errors.put(name, validateMsg);
|
errors.put(name, validateMsg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return errors;
|
return errors;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private String validate(String valdationType, List<FileItem> fileItems) {
|
private String validate(String validationType, List<FileItem> fileItems) {
|
||||||
if( "nonempty".equalsIgnoreCase(valdationType)){
|
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{
|
||||||
|
@ -121,12 +132,13 @@ public class BasicValidation {
|
||||||
error message.
|
error message.
|
||||||
*/
|
*/
|
||||||
public String validate(String validationType, String value){
|
public String validate(String validationType, String value){
|
||||||
|
// Required field validation
|
||||||
if( "nonempty".equalsIgnoreCase(validationType)){
|
if( "nonempty".equalsIgnoreCase(validationType)){
|
||||||
if( value == null || value.trim().length() == 0 )
|
if( value == null || value.trim().length() == 0 )
|
||||||
return "this field must not be empty";
|
return REQUIRED_FIELD_EMPTY_MSG;
|
||||||
else
|
}
|
||||||
return SUCCESS;
|
// Format validation
|
||||||
}else if("isDate".equalsIgnoreCase(validationType)){
|
if("isDate".equalsIgnoreCase(validationType)){
|
||||||
if( isDate( value))
|
if( isDate( value))
|
||||||
return SUCCESS;
|
return SUCCESS;
|
||||||
else
|
else
|
||||||
|
@ -191,6 +203,7 @@ public class BasicValidation {
|
||||||
|
|
||||||
/** 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";
|
||||||
|
|
||||||
/** 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}";
|
||||||
|
|
Loading…
Add table
Reference in a new issue