=== modified file 'dhis-2/dhis-web/dhis-web-light/src/main/java/org/hisp/dhis/light/dataentry/action/SaveSectionFormAction.java' --- dhis-2/dhis-web/dhis-web-light/src/main/java/org/hisp/dhis/light/dataentry/action/SaveSectionFormAction.java 2011-10-26 08:43:56 +0000 +++ dhis-2/dhis-web/dhis-web-light/src/main/java/org/hisp/dhis/light/dataentry/action/SaveSectionFormAction.java 2011-10-26 11:49:43 +0000 @@ -278,36 +278,103 @@ .getDataValue( organisationUnit, dataElement, period, optionCombo ); value = value.trim(); - - if ( value == null || value.length() == 0 || !SectionFormUtils.isInteger( value ) ) + Boolean valueIsEmpty = (value == null || value.length() == 0); + + // validate types + Boolean correctType = true; + String type = dataElement.getType(); + String numberType = dataElement.getNumberType(); + + if ( !valueIsEmpty ) + { + if ( type.equals( DataElement.VALUE_TYPE_STRING ) ) + { + } + else if ( type.equals( DataElement.VALUE_TYPE_BOOL ) ) + { + if ( !valueIsEmpty && !SectionFormUtils.isBoolean( value ) ) + { + correctType = false; + typeViolations.put( key, "Invalid boolean" ); + } + } + else if ( type.equals( DataElement.VALUE_TYPE_DATE ) ) + { + if ( !SectionFormUtils.isDate( value ) ) + { + correctType = false; + typeViolations.put( key, "Invalid date (YYYY-MM-DD)" ); + } + } + else if ( type.equals( DataElement.VALUE_TYPE_INT ) + && numberType.equals( DataElement.VALUE_TYPE_NUMBER ) ) + { + if ( !SectionFormUtils.isNumber( value ) ) + { + correctType = false; + typeViolations.put( key, "Invalid number" ); + } + } + else if ( type.equals( DataElement.VALUE_TYPE_INT ) + && numberType.equals( DataElement.VALUE_TYPE_INT ) ) + { + if ( !SectionFormUtils.isInteger( value ) ) + { + correctType = false; + typeViolations.put( key, "Invalid integer" ); + } + } + else if ( type.equals( DataElement.VALUE_TYPE_INT ) + && numberType.equals( DataElement.VALUE_TYPE_POSITIVE_INT ) ) + { + if ( !SectionFormUtils.isPositiveInteger( value ) ) + { + correctType = false; + typeViolations.put( key, "Invalid positive integer" ); + } + } + else if ( type.equals( DataElement.VALUE_TYPE_INT ) + && numberType.equals( DataElement.VALUE_TYPE_NEGATIVE_INT ) ) + { + if ( !SectionFormUtils.isNegativeInteger( value ) ) + { + correctType = false; + typeViolations.put( key, "Invalid negative integer" ); + } + } + } + + // nothing entered + if ( valueIsEmpty || !correctType ) { if ( dataValue != null ) { dataValueService.deleteDataValue( dataValue ); } - - continue; - } - - if ( dataValue == null ) - { - needsValidation = true; - - dataValue = new DataValue( dataElement, period, organisationUnit, value, storedBy, new Date(), - null, optionCombo ); - dataValueService.addDataValue( dataValue ); - } - else - { - if ( !dataValue.getValue().equals( value ) ) + } + + if ( correctType && !valueIsEmpty ) + { + if ( dataValue == null ) { needsValidation = true; - dataValue.setValue( value ); - dataValue.setTimestamp( new Date() ); - dataValue.setStoredBy( storedBy ); - - dataValueService.updateDataValue( dataValue ); + dataValue = new DataValue( dataElement, period, organisationUnit, value, storedBy, new Date(), + null, optionCombo ); + dataValueService.addDataValue( dataValue ); + } + else + { + if ( !dataValue.getValue().equals( value ) ) + { + needsValidation = true; + + dataValue.setValue( value ); + dataValue.setTimestamp( new Date() ); + dataValue.setStoredBy( storedBy ); + + dataValueService.updateDataValue( dataValue ); + } } } } @@ -337,7 +404,8 @@ validationRuleViolations = sectionFormUtils.getValidationRuleViolations( organisationUnit, dataSet, period ); - if ( needsValidation && (validationViolations.size() > 0 || validationRuleViolations.size() > 0) ) + if ( needsValidation + && (!validationViolations.isEmpty() || !validationRuleViolations.isEmpty() || !typeViolations.isEmpty()) ) { return ERROR; } === modified file 'dhis-2/dhis-web/dhis-web-light/src/main/java/org/hisp/dhis/light/dataentry/utils/SectionFormUtils.java' --- dhis-2/dhis-web/dhis-web-light/src/main/java/org/hisp/dhis/light/dataentry/utils/SectionFormUtils.java 2011-10-24 11:36:21 +0000 +++ dhis-2/dhis-web/dhis-web-light/src/main/java/org/hisp/dhis/light/dataentry/utils/SectionFormUtils.java 2011-10-26 11:49:43 +0000 @@ -55,6 +55,8 @@ import org.hisp.dhis.validation.ValidationResult; import org.hisp.dhis.validation.ValidationRule; import org.hisp.dhis.validation.ValidationRuleService; +import org.joda.time.format.DateTimeFormatter; +import org.joda.time.format.ISODateTimeFormat; /** * @author mortenoh @@ -219,6 +221,20 @@ // Static Utils // ------------------------------------------------------------------------- + public static boolean isNumber( String value ) + { + try + { + Double.parseDouble( value ); + } + catch ( NumberFormatException e ) + { + return false; + } + + return true; + } + public static boolean isInteger( String value ) { try @@ -233,6 +249,16 @@ return true; } + public static boolean isPositiveInteger( String value ) + { + return valueHigher( value, 0 ); + } + + public static boolean isNegativeInteger( String value ) + { + return valueLower( value, 0 ); + } + public static boolean valueHigher( String value, int max ) { int integerValue; @@ -272,4 +298,25 @@ return false; } + + public static boolean isBoolean( String value ) + { + return value.equals( "true" ) || value.equals( "false" ); + } + + public static boolean isDate( String value ) + { + DateTimeFormatter sdf = ISODateTimeFormat.yearMonthDay(); + + try + { + sdf.parseDateTime( value ); + return true; + } + catch ( IllegalArgumentException e ) + { + } + + return false; + } } === modified file 'dhis-2/dhis-web/dhis-web-light/src/main/webapp/dhis-web-light/dataEntry.vm' --- dhis-2/dhis-web/dhis-web-light/src/main/webapp/dhis-web-light/dataEntry.vm 2011-10-26 08:43:56 +0000 +++ dhis-2/dhis-web/dhis-web-light/src/main/webapp/dhis-web-light/dataEntry.vm 2011-10-26 11:49:43 +0000 @@ -42,23 +42,29 @@ #end #end + #if( $typeViolations.get( $key ) ) + #set( $typeViolation = $typeViolations.get( $key ) ) +
$typeViolation + #end + #if( $dataElement.type == "string" ) - + #elseif( $dataElement.type == "bool" ) #elseif( $dataElement.type == "date" ) - + #elseif( $dataElement.type == "int" && $dataElement.numberType == "number" ) - + #elseif( $dataElement.type == "int" && $dataElement.numberType == "int" ) - + #elseif( $dataElement.type == "int" && $dataElement.numberType == "positiveNumber" ) - + #elseif( $dataElement.type == "int" && $dataElement.numberType == "negativeNumber" ) - + #end #end #end