@@ -20,7 +20,9 @@ import {
2020 UntypedFormBuilder ,
2121 UntypedFormControl ,
2222 Validators ,
23- ReactiveFormsModule
23+ ReactiveFormsModule ,
24+ AbstractControl ,
25+ ValidationErrors
2426} from '@angular/forms' ;
2527import { ActivatedRoute , RouterLink } from '@angular/router' ;
2628
@@ -112,14 +114,36 @@ export class FundMappingComponent implements OnInit {
112114 this . buildDependencies ( ) ;
113115 }
114116
117+ /**
118+ * Custom validator to ensure array fields are not empty.
119+ * @param {AbstractControl } control - the form control to validate.
120+ * @returns {ValidationErrors | null } - validation errors or null if valid.
121+ */
122+ private nonEmptyArrayValidator ( control : AbstractControl ) : ValidationErrors | null {
123+ const value = control . value ;
124+ if ( ! value || ! Array . isArray ( value ) || value . length === 0 ) {
125+ return { required : true } ;
126+ }
127+ if ( value . every ( ( item : any ) => item === '' || item === null || item === undefined ) ) {
128+ return { required : true } ;
129+ }
130+ return null ;
131+ }
132+
115133 /**
116134 * Creates the Fund Mapping Form
117135 */
118136 createFundMappingForm ( ) {
119137 this . fundMappingForm = this . formBuilder . group ( {
120- loanStatus : [ '' ] ,
121- loanProducts : [ '' ] ,
122- offices : [ '' ] ,
138+ loanStatus : [
139+ [ ] ,
140+ this . nonEmptyArrayValidator . bind ( this ) ] ,
141+ loanProducts : [
142+ [ ] ,
143+ this . nonEmptyArrayValidator . bind ( this ) ] ,
144+ offices : [
145+ [ ] ,
146+ this . nonEmptyArrayValidator . bind ( this ) ] ,
123147 loanDateOption : [
124148 '' ,
125149 Validators . required
@@ -224,6 +248,10 @@ export class FundMappingComponent implements OnInit {
224248 if ( fundMappingFormData . loanFromDate instanceof Date ) {
225249 fundMappingFormData . loanFromDate = this . dateUtils . formatDate ( prevLoanFromDate , dateFormat ) ;
226250 }
251+ if ( this . fundMappingForm . invalid ) {
252+ this . fundMappingForm . markAllAsTouched ( ) ;
253+ return ;
254+ }
227255 if ( fundMappingFormData . loanToDate instanceof Date ) {
228256 fundMappingFormData . loanToDate = this . dateUtils . formatDate ( prevLoanToDate , dateFormat ) ;
229257 }
0 commit comments