ng-dynamic-forms
ng-dynamic-forms copied to clipboard
CHECKBOX_GROUP validators not working
I'm submitting a
[ ] Bug / Regression
[ ] Feature Request / Proposal
I'm using
NG Dynamic Forms Version: `12.0.0`
[ ] Basic UI
[ ] Bootstrap UI
[ ] Foundation UI
[ ] Ionic UI
[ ] Kendo UI
[ ] Material
[ ] NG Bootstrap
[x ] Prime NG
Description
Hello
Thank you for this great library
I have noticed that when i add validators to a CHECKBOX_GROUP they don't work. I want to require at least one checkbox to be checked in a checkbox group.
{
"id":"COLLECTION_METHOD_UC",
"label":"Specimen Collection Method",
"name":"COLLECTION_METHOD_UC",
"type":"CHECKBOX_GROUP",
"validators":{
"required":null
},
"errorMessages":{
"required":"{{ label }} is required."
},
"group":[
{
"id":"VOIDED_URINE",
"label":"Voided Urine",
"name":"VOIDED_URINE",
"type":"CHECKBOX",
"value":null
},
{
"id":"RANDOM_VOID",
"label":"Random Void",
"name":"RANDOM_VOID",
"type":"CHECKBOX",
"value":null
}
]
}
I have tried using validators and groupValidators, but the formgroup is always valid even after i check and uncheck all checkboxes, and no error message shows if no checkboxes are checked
as far as i know it needs a custom-validator on the group. in this validator can then be iterated over the controls of the group.
export function myCustomValidator(control: AbstractControl): ValidationErrors | null {
let countChecked = 0;
Object.keys(group.controls).forEach((key) => {
const c: AbstractControl = group.get(key);
if (c.value === true) {
countChecked++;
}
});
const hasError = countChecked < 1;
return hasError ? {myCustomValidator: true} : null;
}