← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 17965: event capture - better validation on event grid

 

------------------------------------------------------------
revno: 17965
committer: Abyot Asalefew Gizaw <abyota@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2015-01-13 23:47:45 +0100
message:
  event capture - better validation on event grid
modified:
  dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/scripts/controllers.js
  dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/scripts/services.js
  dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/views/eventList.html
  dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/angular/plugins/dhis2/directives.js
  dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/angular/plugins/dhis2/services.js


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/scripts/controllers.js'
--- dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/scripts/controllers.js	2015-01-13 17:31:37 +0000
+++ dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/scripts/controllers.js	2015-01-13 22:47:45 +0000
@@ -366,7 +366,7 @@
         $scope.editingEventInGrid = false;
         $scope.currentElement.updated = false;
         
-        $scope.outerForm.$valid = true;
+        //$scope.outerForm.$valid = true;
         
         $scope.currentEvent = {};
     };
@@ -593,14 +593,8 @@
 
         DHIS2EventFactory.update(updatedEvent).then(function(data){            
             
-            //update original value
-            var continueLoop = true;
-            for(var i=0; i< $scope.dhis2Events.length && continueLoop; i++){
-                if($scope.dhis2Events[i].event === $scope.currentEvent.event ){
-                    $scope.dhis2Events[i] = $scope.currentEvent;
-                    continueLoop = false;
-                }
-            }
+            //reflect the change in the gird            
+            $scope.resetEventValue($scope.currentEvent);
                 
             $scope.currentEventOrginialValue = angular.copy($scope.currentEvent); 
             $scope.outerForm.submitted = false;            
@@ -617,20 +611,23 @@
         $scope.currentElement = {id: dataElement};
         
         //get new and old values
-        var newValue = currentEvent[dataElement];        
+        var newValue = $scope.currentEvent[dataElement];        
         var oldValue = $scope.currentEventOrginialValue[dataElement];
         
         //check for form validity
-        $scope.outerForm.submitted = true;
-        if( $scope.outerForm.$invalid ){
-            $scope.currentElement.updated = false;
-            currentEvent[dataElement] = oldValue;
-            return;
-        }   
+        if( $scope.isFormInvalid() ){
+            $scope.currentElement.updated = false;            
+            //reset value back to original
+            $scope.currentEvent[dataElement] = oldValue;            
+            $scope.resetEventValue($scope.currentEvent);
+            return;            
+        }
         
-        if( $scope.prStDes[dataElement].compulsory && !newValue ) {            
-            currentEvent[dataElement] = oldValue;
-            $scope.currentElement.updated = false;
+        if( $scope.prStDes[dataElement].compulsory && !newValue ) {
+            $scope.currentElement.updated = false;                        
+            //reset value back to original
+            $scope.currentEvent[dataElement] = oldValue;            
+            $scope.resetEventValue($scope.currentEvent);
             return;
         }        
                 
@@ -645,21 +642,16 @@
                 newValue = DateUtils.formatFromUserToApi(newValue);
             }
             
-            var updatedSingleValueEvent = {event: currentEvent.event, dataValues: [{value: newValue, dataElement: dataElement}]};
-            var updatedFullValueEvent = DHIS2EventService.reconstructEvent(currentEvent, $scope.selectedProgramStage.programStageDataElements);
+            var updatedSingleValueEvent = {event: $scope.currentEvent.event, dataValues: [{value: newValue, dataElement: dataElement}]};
+            var updatedFullValueEvent = DHIS2EventService.reconstructEvent($scope.currentEvent, $scope.selectedProgramStage.programStageDataElements);
 
             DHIS2EventFactory.updateForSingleValue(updatedSingleValueEvent, updatedFullValueEvent).then(function(data){
                 
-                var continueLoop = true;
-                for(var i=0; i< $scope.dhis2Events.length && continueLoop; i++){
-                    if($scope.dhis2Events[i].event === currentEvent.event ){
-                        $scope.dhis2Events[i] = currentEvent;
-                        continueLoop = false;
-                    }
-                }
+                //reflect the new value in the grid
+                $scope.resetEventValue($scope.currentEvent);
                 
                 //update original value
-                $scope.currentEventOrginialValue = angular.copy(currentEvent);      
+                $scope.currentEventOrginialValue = angular.copy($scope.currentEvent);      
                 
                 $scope.currentElement.updated = true;
                 $scope.updateSuccess = true;
@@ -667,6 +659,16 @@
         }
     };
     
+    $scope.resetEventValue = function(currentEvent){
+        var continueLoop = true;
+        for(var i=0; i< $scope.dhis2Events.length && continueLoop; i++){
+            if($scope.dhis2Events[i].event === currentEvent.event ){
+                $scope.dhis2Events[i] = currentEvent;
+                continueLoop = false;
+            }
+        }
+    };
+    
     $scope.removeEvent = function(){
         
         var dhis2Event = ContextMenuSelectedItem.getSelectedItem();

=== modified file 'dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/scripts/services.js'
--- dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/scripts/services.js	2015-01-13 17:31:37 +0000
+++ dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/scripts/services.js	2015-01-13 22:47:45 +0000
@@ -313,21 +313,4 @@
             return e;
         }        
     };
-})
-
-/* Error messages*/
-.service('ErrorMessageService', function(){
-    this.errorMessages = {};
-    
-    this.setErrorMessages = function(errorMessages){  
-        this.errorMessages = errorMessages;        
-    };
-    
-    this.getErrorMessages = function(){
-        return this.errorMessages;
-    };
-    
-    this.get = function(id){
-        return this.errorMessages[id];
-    };
 });
\ No newline at end of file

=== modified file 'dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/views/eventList.html'
--- dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/views/eventList.html	2014-12-09 23:24:16 +0000
+++ dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-event-capture/views/eventList.html	2015-01-13 22:47:45 +0000
@@ -114,8 +114,7 @@
                             </td>
 
                             <!-- Visible when event is under editing - in grid -->
-                            <td style="cursor:default;"      
-                                ng-dblclick="showEventList()"
+                            <td style="cursor:default;"
                                 ng-if="(currentEvent.event == dhis2Event.event) && eventGridColumn.show && eventGridColumn.id !== 'comment'" 
                                 ng-repeat="eventGridColumn in eventGridColumns">
                                 <ng-form name="innerFormGrid">
@@ -126,22 +125,23 @@
                                                    d2-validation 
                                                    d2-number-validation
                                                    number-type={{prStDes[eventGridColumn.id].dataElement.numberType}}
-                                                   ng-model="dhis2Event[eventGridColumn.id]" 
+                                                   ng-model="currentEvent[eventGridColumn.id]" 
                                                    ng-blur="updateEventDataValue(dhis2Event, eventGridColumn.id)" 
                                                    ng-required={{eventGridColumn.compulsory}}
+                                                   input-field-id={{eventGridColumn.id}} 
                                                    name="foo" 
                                                    style="width:98%;"
                                                    ng-class="{true: 'update-success'} [currentElement.updated == true && currentElement.id == eventGridColumn.id]"
                                                    ng-class="{true: 'update-error'} [!currentElement.updated == true && currentElement.id == eventGridColumn.id]"/>                                            
-                                            <span ng-show="outerForm.submitted && innerFormGrid.foo.$invalid" class="red"><em title="{{'required'| translate}}" class="required">*</em></span>
-                                            <span ng-if="innerFormGrid.foo.$dirty && innerFormGrid.foo.$invalid" class="error">{{'value_must_be'| translate}} - {{prStDes[eventGridColumn.id].dataElement.numberType | translate}}</span>
+                                            <!--<span ng-show="outerForm.submitted && innerFormGrid.foo.$invalid" class="red"><em title="{{'required'| translate}}" class="required">*</em></span>
+                                            <span ng-if="innerFormGrid.foo.$dirty && innerFormGrid.foo.$invalid" class="error">{{'value_must_be'| translate}} - {{prStDes[eventGridColumn.id].dataElement.numberType | translate}}</span>-->
                                         </div>
                                         <div ng-switch-when="string">                                        
                                             <div ng-if="eventGridColumn.id == 'uid'">
                                                 <input type="text"
                                                        d2-validation 
                                                        ng-model="currentEvent[eventGridColumn.id]"                                                                    
-                                                       ng-disabled=true
+                                                       ng-disabled=true 
                                                        name="foo" 
                                                        style="width:99%;"/>                                                    
                                             </div>
@@ -149,68 +149,75 @@
                                                 <div class="container-fluid" ng-if="optionSets[prStDes[eventGridColumn.id].dataElement.optionSet.id]">                                                
                                                     <span ng-if="!selectedProgram.dataEntryMethod || optionSets[prStDes[eventGridColumn.id].dataElement.optionSet.id].options.length > 7">
                                                         <input type="text" 
-                                                           d2-validation 
-                                                           ng-model="dhis2Event[eventGridColumn.id]" 
+                                                           class="typeahead"
+                                                           placeholder="&#xf0d7;&nbsp;&nbsp;"                                                 
+                                                           ng-model="currentEvent[eventGridColumn.id]" 
                                                            ng-blur="updateEventDataValue(dhis2Event, eventGridColumn.id)" 
                                                            typeahead="option.name as option.name for option in optionSets[prStDes[eventGridColumn.id].dataElement.optionSet.id].options | filter:$viewValue | limitTo:20" 
                                                            typeahead-open-on-focus
-                                                           typeahead-editable="false"
+                                                           typeahead-editable="false" 
+                                                           d2-typeahead-validation 
                                                            ng-required={{eventGridColumn.compulsory}}
                                                            ng-disabled="eventGridColumn.id == 'uid'"
+                                                           input-field-id={{eventGridColumn.id}} 
                                                            name="foo" 
                                                            style="width:98%;"
                                                            ng-class="{true: 'update-success'} [currentElement.updated == true && currentElement.id == eventGridColumn.id]"
                                                            ng-class="{true: 'update-error'} [!currentElement.updated == true && currentElement.id == eventGridColumn.id]"
                                                            />
-                                                        <span ng-show="outerForm.submitted && innerFormGrid.foo.$invalid" class="red"><em title="{{'required'| translate}}" class="required">*</em></span>
-                                                        <span ng-if="innerFormGrid.foo.$dirty && innerForm.foo.$invalid" class="required">{{'option_required'| translate}}</span>
+                                                        <!--<span ng-show="outerForm.submitted && innerFormGrid.foo.$invalid" class="red"><em title="{{'required'| translate}}" class="required">*</em></span>
+                                                        <span ng-if="innerFormGrid.foo.$dirty && innerForm.foo.$invalid" class="required">{{'option_required'| translate}}</span>-->
                                                     </span>
                                                     <span ng-if="selectedProgram.dataEntryMethod && optionSets[prStDes[eventGridColumn.id].dataElement.optionSet.id].options.length < 7">                                    
                                                         <label>
                                                             <input type="radio" 
+                                                                   input-field-id={{eventGridColumn.id}} 
                                                                    name="foo"
                                                                    d2-validation 
                                                                    ng-required={{eventGridColumn.compulsory}}
-                                                                   ng-model="dhis2Event[eventGridColumn.id]" 
+                                                                   ng-model="currentEvent[eventGridColumn.id]" 
                                                                    ng-change="updateEventDataValue(dhis2Event, eventGridColumn.id)" 
                                                                    value=""> {{'no_value' | translate}}<br>                                     
                                                         </label>                                    
                                                         <label ng-repeat="option in  optionSets[prStDes[eventGridColumn.id].dataElement.optionSet.id].options">
                                                             <input type="radio" 
+                                                                   input-field-id={{eventGridColumn.id}} 
                                                                    name={{eventGridColumn.id}}   
                                                                    d2-validation 
                                                                    ng-required={{eventGridColumn.compulsory}}
-                                                                   ng-model="dhis2Event[eventGridColumn.id]" 
+                                                                   ng-model="currentEvent[eventGridColumn.id]" 
                                                                    ng-change="updateEventDataValue(dhis2Event, eventGridColumn.id)"
                                                                    ng-class="{true: 'update-success'} [currentElement.updated == true && currentElement.id == eventGridColumn.id]"
                                                                    ng-class="{true: 'update-error'} [!currentElement.updated == true && currentElement.id == eventGridColumn.id]"
                                                                    value={{option.name}}> {{option.name}}<br>                                    
                                                         </label>                                    
-                                                        <span ng-show="outerForm.submitted && innerForm.foo.$invalid" class="required">{{'required'| translate}}</span>                                    
+                                                        <!--<span ng-show="outerForm.submitted && innerForm.foo.$invalid" class="required">{{'required'| translate}}</span>-->
                                                     </span>                                                
                                                 </div>
                                                 <div ng-if="!optionSets[prStDes[eventGridColumn.id].dataElement.optionSet.id]">
                                                     <input type="text" 
                                                         d2-validation    
-                                                        ng-model="dhis2Event[eventGridColumn.id]" 
+                                                        ng-model="currentEvent[eventGridColumn.id]" 
                                                         ng-blur="updateEventDataValue(dhis2Event, eventGridColumn.id)" 
                                                         ng-required={{eventGridColumn.compulsory}}
                                                         ng-disabled="eventGridColumn.id == 'uid'"
+                                                        input-field-id={{eventGridColumn.id}} 
                                                         name="foo" 
                                                         style="width:98%;"
                                                         ng-class="{true: 'update-success'} [currentElement.updated == true && currentElement.id == eventGridColumn.id]"
                                                         ng-class="{true: 'update-error'} [!currentElement.updated == true && currentElement.id == eventGridColumn.id]"
                                                         />
-                                                    <span ng-show="outerForm.submitted && innerFormGrid.foo.$invalid" class="red"><em title="{{'required'| translate}}" class="required">*</em></span>
-                                                    <span ng-if="innerFormGrid.foo.$dirty && innerForm.foo.$invalid" class="required">{{'string_required'| translate}}</span>
+                                                    <!--<span ng-show="outerForm.submitted && innerFormGrid.foo.$invalid" class="red"><em title="{{'required'| translate}}" class="required">*</em></span>
+                                                    <span ng-if="innerFormGrid.foo.$dirty && innerForm.foo.$invalid" class="required">{{'string_required'| translate}}</span>-->
                                                 </div>
                                             </div>
                                         </div>                                        
                                         <div ng-switch-when="bool">
                                             <select d2-validation 
-                                                    ng-model="dhis2Event[eventGridColumn.id]" 
+                                                    ng-model="currentEvent[eventGridColumn.id]" 
                                                     ng-change="updateEventDataValue(dhis2Event, eventGridColumn.id)" 
                                                     ng-required={{eventGridColumn.compulsory}}
+                                                    input-field-id={{eventGridColumn.id}} 
                                                     name="foo" 
                                                     style="width:98%;"
                                                     ng-class="{true: 'update-success'} [currentElement.updated == true && currentElement.id == eventGridColumn.id]"
@@ -219,8 +226,8 @@
                                                 <option value="false">{{'no'| translate}}</option>
                                                 <option value="true">{{'yes'| translate}}</option>
                                             </select>
-                                            <span ng-show="outerForm.submitted && innerFormGrid.foo.$invalid" class="red"><em title="{{'required'| translate}}" class="required">*</em></span>
-                                            <span ng-if="innerFormGrid.foo.$dirty && innerForm.foo.$invalid" class="required">{{'bool_required'| translate}}</span>
+                                            <!--<span ng-show="outerForm.submitted && innerFormGrid.foo.$invalid" class="red"><em title="{{'required'| translate}}" class="required">*</em></span>
+                                            <span ng-if="innerFormGrid.foo.$dirty && innerForm.foo.$invalid" class="required">{{'bool_required'| translate}}</span>-->
                                         </div>
                                         <div ng-switch-when="date">
                                             <input type="text" 
@@ -228,31 +235,34 @@
                                                    d2-date
                                                    max-date="prStDes[eventGridColumn.id].allowFutureDate ? '' : 0" 
                                                    d2-validation 
-                                                   ng-model="dhis2Event[eventGridColumn.id]"
+                                                   ng-model="currentEvent[eventGridColumn.id]"
                                                    blur-or-change="updateEventDataValue(dhis2Event, eventGridColumn.id)" 
                                                    ng-required={{eventGridColumn.compulsory}}
                                                    ng-disabled="eventGridColumn.id == 'event_date'"
+                                                   input-field-id={{eventGridColumn.id}} 
                                                    name="foo" 
                                                    style="width:98%;"
                                                    ng-class="{true: 'update-success'} [currentElement.updated == true && currentElement.id == eventGridColumn.id]"
                                                    ng-class="{true: 'update-error'} [!currentElement.updated == true && currentElement.id == eventGridColumn.id]"
                                                    />
-                                            <span ng-show="outerForm.submitted && innerFormGrid.foo.$invalid" class="red"><em title="{{'required'| translate}}" class="required">*</em></span>
-                                            <span ng-show="innerFormGrid.foo.$dirty && innerForm.foo.$invalid" class="required">{{'date_required'| translate}}</span>
+                                            <!--<span ng-show="outerForm.submitted && innerFormGrid.foo.$invalid" class="red"><em title="{{'required'| translate}}" class="required">*</em></span>
+                                            <span ng-show="innerFormGrid.foo.$dirty && innerForm.foo.$invalid" class="required">{{'date_required'| translate}}</span>-->
                                         </div>
                                         <div ng-switch-when="trueOnly">
                                             <input type="checkbox"
                                                    d2-validation 
-                                                   ng-model="dhis2Event[eventGridColumn.id]"
+                                                   ng-model="currentEvent[eventGridColumn.id]"
                                                    ng-change="updateEventDataValue(dhis2Event, eventGridColumn.id)" 
-                                                   ng-required={{eventGridColumn.compulsory}}
+                                                   ng-required={{eventGridColumn.compulsory}} 
+                                                   input-field-id={{eventGridColumn.id}} 
                                                    name="foo" 
                                                    ng-class="{true: 'update-success'} [currentElement.updated == true && currentElement.id == eventGridColumn.id]"
                                                    ng-class="{true: 'update-error'} [!currentElement.updated == true && currentElement.id == eventGridColumn.id]"
                                                    />
-                                            <span ng-show="outerForm.submitted && innerFormGrid.foo.$invalid"><em title="{{'required'| translate}}" class="required">*</em></span>
+                                            <!--<span ng-show="outerForm.submitted && innerFormGrid.foo.$invalid"><em title="{{'required'| translate}}" class="required">*</em></span>-->
                                         </div>
                                     </div>
+                                    <span ng-show="currentEvent[eventGridColumn.id] && innerFormGrid.foo.$invalid || outerForm.submitted && innerFormGrid.foo.$invalid" class="required">{{getErrorMessage(eventGridColumn.id)}}</span>
                                 </ng-form>
                             </td>
                             <td ng-repeat="eventGridColumn in eventGridColumns" ng-if="eventGridColumn.id === 'comment' && eventGridColumn.show">                                

=== modified file 'dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/angular/plugins/dhis2/directives.js'
--- dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/angular/plugins/dhis2/directives.js	2015-01-13 17:31:37 +0000
+++ dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/angular/plugins/dhis2/directives.js	2015-01-13 22:47:45 +0000
@@ -514,21 +514,20 @@
                     if(!isValid){
                         errorMessages[fieldName] = $translate('date_required');
                     }
-                    
-                    if(isValid && maxDate === 0){                    
-                        isValid = !moment(convertedDate, calendarSetting.momentFormat).isAfter(DateUtils.getToday());
-                        if(!isValid){
-                            errorMessages[fieldName] = $translate('future_date_not_allowed');                            
-                        }
-                        else{                            
-                            if(isRequired){
-                                errorMessages[fieldName] = $translate('required');
-                            }
-                            else{
-                                errorMessages[fieldName] = "";
-                            }
-                        }
-                    }                    
+                    else{
+                        if(isRequired){
+                            errorMessages[fieldName] = $translate('required');
+                        }
+                        else{
+                            errorMessages[fieldName] = "";
+                        }
+                        if(maxDate === 0){                    
+                            isValid = !moment(convertedDate, calendarSetting.momentFormat).isAfter(DateUtils.getToday());
+                            if(!isValid){
+                                errorMessages[fieldName] = $translate('future_date_not_allowed');                            
+                            }                           
+                        }
+                    }                                        
                     ctrl.$setViewValue(this.value);
                     ctrl.$setValidity(fieldName, isValid);
                 }

=== modified file 'dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/angular/plugins/dhis2/services.js'
--- dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/angular/plugins/dhis2/services.js	2015-01-13 17:31:37 +0000
+++ dhis-2/dhis-web/dhis-web-commons-resources/src/main/webapp/dhis-web-commons/javascripts/angular/plugins/dhis2/services.js	2015-01-13 22:47:45 +0000
@@ -331,6 +331,23 @@
     };
 })
 
+/* Error messages*/
+.service('ErrorMessageService', function(){
+    this.errorMessages = {};
+    
+    this.setErrorMessages = function(errorMessages){  
+        this.errorMessages = errorMessages;        
+    };
+    
+    this.getErrorMessages = function(){
+        return this.errorMessages;
+    };
+    
+    this.get = function(id){
+        return this.errorMessages[id];
+    };
+})
+
 /* Modal service for user interaction */
 .service('ModalService', ['$modal', function($modal) {