Angular: Checkboxes becoming all checked after form submission

My form for a food item is span across 2 tabs. Both tabs contain 2 different forms built using json configs which are merged later on. The problem is after I click save and the backend validations run returning an error message if any, all the checkboxes on both tabs are checked even if I have not checked them.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@Input() data?: any;
foodMainCourseForm = foodMainCourseForm as IForm;
foodAddOnsForm = foodAddOnsForm as IForm;
dynamicMainCourseForm!: FormGroup;
dynamicAddOnForm!: FormGroup;
ngOnInit(): void {
this.filteredRecord = this.data;
this.addService.data$.subscribe(data => {
this.data = data;
this.dynamicMainCourseForm = this.setupFormGroup(this.foodMainCourseForm);
this.dynamicAddOnForm = this.setupFormGroup(this.foodAddOnsForm);
});
this.dynamicMainCourseForm = this.setupFormGroup(this.foodMainCourseForm);
this.dynamicAddOnForm = this.setupFormGroup(this.foodAddOnsForm);
</code>
<code>@Input() data?: any; foodMainCourseForm = foodMainCourseForm as IForm; foodAddOnsForm = foodAddOnsForm as IForm; dynamicMainCourseForm!: FormGroup; dynamicAddOnForm!: FormGroup; ngOnInit(): void { this.filteredRecord = this.data; this.addService.data$.subscribe(data => { this.data = data; this.dynamicMainCourseForm = this.setupFormGroup(this.foodMainCourseForm); this.dynamicAddOnForm = this.setupFormGroup(this.foodAddOnsForm); }); this.dynamicMainCourseForm = this.setupFormGroup(this.foodMainCourseForm); this.dynamicAddOnForm = this.setupFormGroup(this.foodAddOnsForm); </code>
@Input() data?: any;
foodMainCourseForm = foodMainCourseForm as IForm;
foodAddOnsForm = foodAddOnsForm as IForm;
dynamicMainCourseForm!: FormGroup;
dynamicAddOnForm!: FormGroup;

ngOnInit(): void {
this.filteredRecord = this.data;
this.addService.data$.subscribe(data => {
    this.data = data;
    this.dynamicMainCourseForm = this.setupFormGroup(this.foodMainCourseForm);
    this.dynamicAddOnForm = this.setupFormGroup(this.foodAddOnsForm);
});

this.dynamicMainCourseForm = this.setupFormGroup(this.foodMainCourseForm);
this.dynamicAddOnForm = this.setupFormGroup(this.foodAddOnsForm);

}

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>private setupFormGroup(formConfig: IForm): FormGroup {
console.log('formConfig', formConfig);
const formGroup: { [key: string]: any } = {};
if (formConfig?.formControls) {
formConfig.formControls.forEach((control: IFormControl) => {
const controlValidators: any = [];
if (control.validators) {
control.validators.forEach((val: IValidator) => {
if (val.validatorName === 'required') controlValidators.push(Validators.required);
if (val.validatorName === 'email') controlValidators.push(Validators.email);
if (val.validatorName === 'minlength') controlValidators.push(Validators.minLength(val.minLength as number));
if (val.validatorName === 'maxlength') controlValidators.push(Validators.maxLength(val.maxLength as number));
if (val.validatorName === 'pattern') controlValidators.push(Validators.pattern(val.pattern as string));
if (val.validatorName === 'greaterThan') {
controlValidators.push(this.greaterThanValidator(val.greaterThan as string));
}
});
}
let controlValue: any = this.data ? this.data[control.name] || '' : null;
if (control.type === 'checkbox') {
controlValue = controlValue === 'T';
}
// when doing Add
if (!this.data) {
if (control.type === 'checkbox' || control.type === 'text' || control.type === 'dropdown') {
controlValue = control.value;
console.log('control.value', control.value);
}
}
formGroup[control.name] = [controlValue, controlValidators];
});
}
return this.fb.group(formGroup);
</code>
<code>private setupFormGroup(formConfig: IForm): FormGroup { console.log('formConfig', formConfig); const formGroup: { [key: string]: any } = {}; if (formConfig?.formControls) { formConfig.formControls.forEach((control: IFormControl) => { const controlValidators: any = []; if (control.validators) { control.validators.forEach((val: IValidator) => { if (val.validatorName === 'required') controlValidators.push(Validators.required); if (val.validatorName === 'email') controlValidators.push(Validators.email); if (val.validatorName === 'minlength') controlValidators.push(Validators.minLength(val.minLength as number)); if (val.validatorName === 'maxlength') controlValidators.push(Validators.maxLength(val.maxLength as number)); if (val.validatorName === 'pattern') controlValidators.push(Validators.pattern(val.pattern as string)); if (val.validatorName === 'greaterThan') { controlValidators.push(this.greaterThanValidator(val.greaterThan as string)); } }); } let controlValue: any = this.data ? this.data[control.name] || '' : null; if (control.type === 'checkbox') { controlValue = controlValue === 'T'; } // when doing Add if (!this.data) { if (control.type === 'checkbox' || control.type === 'text' || control.type === 'dropdown') { controlValue = control.value; console.log('control.value', control.value); } } formGroup[control.name] = [controlValue, controlValidators]; }); } return this.fb.group(formGroup); </code>
private setupFormGroup(formConfig: IForm): FormGroup {
console.log('formConfig', formConfig);
const formGroup: { [key: string]: any } = {};

if (formConfig?.formControls) {
    formConfig.formControls.forEach((control: IFormControl) => {
        const controlValidators: any = [];

        if (control.validators) {
            control.validators.forEach((val: IValidator) => {
                if (val.validatorName === 'required') controlValidators.push(Validators.required);
                if (val.validatorName === 'email') controlValidators.push(Validators.email);
                if (val.validatorName === 'minlength') controlValidators.push(Validators.minLength(val.minLength as number));
                if (val.validatorName === 'maxlength') controlValidators.push(Validators.maxLength(val.maxLength as number));
                if (val.validatorName === 'pattern') controlValidators.push(Validators.pattern(val.pattern as string));
                if (val.validatorName === 'greaterThan') {
                    controlValidators.push(this.greaterThanValidator(val.greaterThan as string));
                }
            });
        }

        let controlValue: any = this.data ? this.data[control.name] || '' : null;

        if (control.type === 'checkbox') {
            controlValue = controlValue === 'T';
        }

        // when doing Add
        if (!this.data) {
            if (control.type === 'checkbox' || control.type === 'text' || control.type === 'dropdown') {
                controlValue = control.value;
                console.log('control.value', control.value);
            }
        }

        formGroup[control.name] = [controlValue, controlValidators];
    });
}

return this.fb.group(formGroup);

}

onSubmit(event: Event) {
event.preventDefault();

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>// Merge form groups
this.dynamicFoodMainForm = this.mergeFormGroups();
if (this.dynamicFoodMainForm.valid) {
for (const controlName of Object.keys(this.dynamicFoodMainForm.controls)) {
const control = this.dynamicFoodMainForm.get(controlName);
if (control instanceof FormControl && typeof control.value === 'boolean') {
control.setValue(control.value ? 'T' : 'F');
}
}
// Emit service events
if (this.data) {
const updatedRowData = { ...this.data, ...this.dynamicFoodMainForm.value };
if (this.form?.editFormTitle) {
this.addService.emitEdit(updatedRowData);
} else {
this.addService.emitEditData(updatedRowData);
}
} else {
this.addService.emitAdd(this.dynamicFoodMainForm.value);
console.log(this.activeModal);
}
</code>
<code>// Merge form groups this.dynamicFoodMainForm = this.mergeFormGroups(); if (this.dynamicFoodMainForm.valid) { for (const controlName of Object.keys(this.dynamicFoodMainForm.controls)) { const control = this.dynamicFoodMainForm.get(controlName); if (control instanceof FormControl && typeof control.value === 'boolean') { control.setValue(control.value ? 'T' : 'F'); } } // Emit service events if (this.data) { const updatedRowData = { ...this.data, ...this.dynamicFoodMainForm.value }; if (this.form?.editFormTitle) { this.addService.emitEdit(updatedRowData); } else { this.addService.emitEditData(updatedRowData); } } else { this.addService.emitAdd(this.dynamicFoodMainForm.value); console.log(this.activeModal); } </code>
// Merge form groups
this.dynamicFoodMainForm = this.mergeFormGroups();

if (this.dynamicFoodMainForm.valid) {
    for (const controlName of Object.keys(this.dynamicFoodMainForm.controls)) {
        const control = this.dynamicFoodMainForm.get(controlName);
        if (control instanceof FormControl && typeof control.value === 'boolean') {
            control.setValue(control.value ? 'T' : 'F');
        }
    }

    // Emit service events
    if (this.data) {
        const updatedRowData = { ...this.data, ...this.dynamicFoodMainForm.value };
        if (this.form?.editFormTitle) {
            this.addService.emitEdit(updatedRowData);
        } else {
            this.addService.emitEditData(updatedRowData);
        }
    } else {
        this.addService.emitAdd(this.dynamicFoodMainForm.value);
        console.log(this.activeModal);
    }

html part for checkboxes

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> @else if (control.type == "checkbox"){
<label class="form-label" ngbTooltip="{{control.tooltip}}" tooltipClass="tooltip-sm">{{ control.label }}</label>
@if(control.disabledControls){
<input formControlName="{{ control.name }}" class="{{ control.class }}" type="checkbox" (change)="onCheckboxChange($event, control)">
}
@else {
<input formControlName="{{ control.name }}" class="{{ control.class }}" type="checkbox" [checked]="control.value === 'T'" [disabled]="true">
}
</code>
<code> @else if (control.type == "checkbox"){ <label class="form-label" ngbTooltip="{{control.tooltip}}" tooltipClass="tooltip-sm">{{ control.label }}</label> @if(control.disabledControls){ <input formControlName="{{ control.name }}" class="{{ control.class }}" type="checkbox" (change)="onCheckboxChange($event, control)"> } @else { <input formControlName="{{ control.name }}" class="{{ control.class }}" type="checkbox" [checked]="control.value === 'T'" [disabled]="true"> } </code>
 @else if (control.type == "checkbox"){
                      <label class="form-label"  ngbTooltip="{{control.tooltip}}" tooltipClass="tooltip-sm">{{ control.label }}</label>
                      @if(control.disabledControls){
                        <input formControlName="{{ control.name }}" class="{{ control.class }}" type="checkbox" (change)="onCheckboxChange($event, control)">
                      }
                      @else {
                        <input formControlName="{{ control.name }}" class="{{ control.class }}" type="checkbox" [checked]="control.value === 'T'" [disabled]="true">
                      }

How do I tackle this issue please? Im using angular 17

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật