Skip to content
This repository was archived by the owner on Apr 30, 2018. It is now read-only.

Commit 43a6f39

Browse files
author
Kent C. Dodds
committed
Merge pull request #29 from ckniffen/bugfix/multiCheckbox-required
Required type validation only worked for the last checkbox.
2 parents 3f3b42c + 416b485 commit 43a6f39

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/types/multiCheckbox.js

+35
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ export default ngModule => {
1717
valueProp: c.string.optional
1818
})
1919
},
20+
defaultOptions: {
21+
ngModelAttrs:{
22+
required: {
23+
attribute: '',
24+
bound: ''
25+
}
26+
}
27+
},
2028
apiCheckInstance: c,
2129
controller: /* @ngInject */ function($scope) {
2230
const to = $scope.to;
@@ -35,15 +43,42 @@ export default ngModule => {
3543
});
3644
}
3745

46+
function checkValidity(expressionValue){
47+
var valid = angular.isArray($scope.model[opts.key]) &&
48+
$scope.model[opts.key].length > 0 &&
49+
expressionValue;
50+
51+
$scope.fc.$setValidity('required', valid);
52+
}
53+
3854
function setModel() {
3955
$scope.model[opts.key] = [];
4056
angular.forEach($scope.multiCheckbox.checked, (checkbox, index) => {
4157
if (checkbox) {
4258
$scope.model[opts.key].push(to.options[index][to.valueProp || 'value']);
4359
}
4460
});
61+
62+
// Must make sure we mark as touched because only the last checkbox due to a bug in angular.
63+
$scope.fc.$setTouched();
64+
checkValidity(true);
65+
}
66+
67+
if(opts.expressionProperties && opts.expressionProperties.required){
68+
$scope.$watch($scope.options.expressionProperties.required, function(newValue){
69+
checkValidity(newValue);
70+
});
71+
}
72+
73+
if($scope.to.required){
74+
var unwatchFormControl = $scope.$watch('fc', function(newValue){
75+
if(!newValue){ return; }
76+
checkValidity(true);
77+
unwatchFormControl;
78+
});
4579
}
4680
}
4781
});
4882
}
83+
4984
};

0 commit comments

Comments
 (0)