Skip to content

Commit

Permalink
Required type validation only worked for the last checkbox.
Browse files Browse the repository at this point in the history
  • Loading branch information
Caleb Kniffen committed Jun 11, 2015
1 parent 3f3b42c commit 416b485
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/types/multiCheckbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ export default ngModule => {
valueProp: c.string.optional
})
},
defaultOptions: {
ngModelAttrs:{
required: {
attribute: '',
bound: ''
}
}
},
apiCheckInstance: c,
controller: /* @ngInject */ function($scope) {
const to = $scope.to;
Expand All @@ -35,15 +43,42 @@ export default ngModule => {
});
}

function checkValidity(expressionValue){
var valid = angular.isArray($scope.model[opts.key]) &&
$scope.model[opts.key].length > 0 &&
expressionValue;

$scope.fc.$setValidity('required', valid);
}

function setModel() {
$scope.model[opts.key] = [];
angular.forEach($scope.multiCheckbox.checked, (checkbox, index) => {
if (checkbox) {
$scope.model[opts.key].push(to.options[index][to.valueProp || 'value']);
}
});

// Must make sure we mark as touched because only the last checkbox due to a bug in angular.
$scope.fc.$setTouched();
checkValidity(true);
}

if(opts.expressionProperties && opts.expressionProperties.required){
$scope.$watch($scope.options.expressionProperties.required, function(newValue){
checkValidity(newValue);
});
}

if($scope.to.required){
var unwatchFormControl = $scope.$watch('fc', function(newValue){
if(!newValue){ return; }
checkValidity(true);
unwatchFormControl;
});
}
}
});
}

};

0 comments on commit 416b485

Please sign in to comment.