Angular 5 - Reactive Forms With Dynamic FormArray And Simple Validation

angular 5 FormArray

Table of Contents

  • Introduction
  • Display required field indicators
  • Display the checkboxes in the FormArray dynamically
  • Hide and show a form control based on checkbox selection
  • Collect the selected checkbox and dynamic control value
  • Conclusion
  • History
  • Watch this script in action
  • Download
  • Resources

Introduction

I am sharing an article on how to dynamically display controls in FormArray using Angular 5 Reactive Forms and enable/disable the validators based on the selection.

Recently, I was working with Angular 5 Reactive forms to create a registration form. Part of the form requires the checkbox elements to be generated dynamically. So, it could be 3,4,5 or more checkboxes on the form. If the checkbox is checked, a new FormControl/HTML element will appear next to it. That could be a textbox, dropdown or radio button list types in contingent to the checked item and they are required fields. In this article, I will share how to accomplish the following task. If you have a different opinion, please share it -- I’d really appreciate it.

  • Display the checkboxes in the FormArray dynamically
  • Hide and show a form control based on checkbox selection
  • Add and remove required field validator based on checkbox selection
  • Add required field indicator to radio button list
  • Display required field indicators
  • Collect the selected checkbox and dynamic control value

Display required field indicators

Instead of displaying the required field message next to each form element, we can program the form to display an error indicator by using Cascading Style Sheets (CSS) selector. For instance, we can use the ::after selector to append an asterisk next to the label in red font.

Listing 1

  1. .required::after {     content" *";     colorred; }  

Or make the form control border red color if the control has ng-invalid class. Figure 1 shows the output results by using the CSS in Listing 1 and Listing 2.

Listing 2

  1. .form-control.ng-invalid {     border-left:5px solid red; }  

Figure 1

Angular 5 FormGroup Required Field

The radio button list is a little tricky, based on the sample application, we can utilize the CSS combinators and pseudo-classes in Listing 3 to append an asterisk next to the label. Basically, the selector will select all the labels under a form control with invalid state and append an asterisk next to it with red font. Figure 2 shows the form control output using the CSS in listing 3.

Listing 3

  1. .form-control.ng-invalid ~ label.checkbox-inline::after {     content" *";     colorred; }  

Figure 2

Angular 5 FormGroup Radio button Required Field

Display the checkboxes in the FormArray dynamically

Listing 4 shows how to utilize the FormBuilder.group factory method to creates a FormGroup with firstName, lastName, email and programmingLanguage FormControl in it. The first three FormControls are required and the email must match the regular expression requirement. The programmingLanguage values type is a FormArray, which will host an array of available programming languages using Checkboxes and another input type.

Listing 4

  1. this.sampleForm = this.formBuilder.group({  
  2.             firstName: new FormControl('', Validators.required),  
  3.             lastName: new FormControl('', Validators.required),  
  4.             email: new FormControl('', [Validators.required,Validators.pattern(this.regEmail)]),  
  5.             programmingLanguage: this.formBuilder.array([{}])  
  6.         }); 

Shown in listing 5 is the sample object and data that the application will be using.

Listing 5

  1. class Item {  
  2.    constructor(  
  3.         private text: string,  
  4.         private value: number) { }  
  5. }  
  6. class FormControlMetadata {  
  7.    constructor(  
  8.         private checkboxName: string,  
  9.         private checkboxLabel: string,  
  10.         private associateControlName: string,  
  11.         private associateControlLabel: string,  
  12.         private associateControlType: string,  
  13.         private associateControlData: Array<Item>) { }  
  14. }  
  15. this.programmingLanguageList = [   
  16.         new Item('PHP',1),  
  17.         new Item('JavaScript',2),  
  18.         new Item('C#',3),  
  19.         new Item('Other',4)];       
  20.      this.otherProgrammingLanguageList = [   
  21.       new Item('Python',1),  
  22.       new Item('Ruby',2),  
  23.       new Item('C++',3),  
  24.       new Item('Rust',4)];  
  25.     this.phpVersionList = [   
  26.       new Item('v4',1),  
  27.       new Item('v5',2),  
  28.       new Item('v6',3),  
  29.       new Item('v7',4)]; 

The next step is to populate the programming language FormArray. This FormArray will contain an array of FormGroup and each FormGroup will host multiple FormControl instances. Shown in Listing 6 is the logic used to populate the FormArray and the langControlMetada object that will be utilized by the HTML template later on. The later object is to store the properties of element/control such as Checkbox, Textbox, Radio button, Dropdown list and etc.

Initially, the code will loop through the properties of programmingLanguageList object and populate the langControlMetada object. The Checkbox and associate HTML element name will be the combination of a static text and the Item.value/key from the data source. These properties will be mapped to formControlName in the HTML template. The Checkbox label will come from the Item.text. The associateControlLabel property will serve as a placeholder attribute for the input element. By default, the associateControlType will be a textbox, in this example, the application will display a radio button list if PHP option is checked, and a dropdown list if Other option is checked. The purpose of the associateControlData property is to hold the data source for the radio button and dropdown elements.

The next step is to create two FormControls, one for the Checkbox element and one for the associated element. By default, the associated element is disabled. Then, insert the child controls created previously into a FormGroup. The key will be identical to the Checkbox and associate element name. Finally, insert the FormGroup instance into the programmingLanguage object.

Listing 6

  1. enum ControlType {  
  2.     textbox =1 ,  
  3.     dropdown = 2,  
  4.     radioButtonList = 3  
  5. }  
  6.   
  7. export class Common {  
  8.   public static ControlType = ControlType;  
  9.   public static CheckboxPrefix = 'cbLanguage_';  
  10.   public static OtherPrefix ='otherValue_';  
  11. }  
  12.   
  13. langControlMetada: Array<FormControlMetadata> = [];  
  14.   
  15.   populateProgrammingLanguage() {  
  16.     //get the property  
  17.     this.programmingFormArray = this.sampleForm.get('programmingLanguage') as FormArray;      
  18.     //clear  
  19.     this.programmingFormArray.removeAt(0);  
  20.   
  21.     let p:Item;  
  22.     //loop through the list and create the formarray metadata  
  23.     for (p of this.programmingLanguageList) {  
  24.         
  25.       let control = new FormControlMetadata();  
  26.       let group = this.formBuilder.group({});  
  27.         
  28.       //create the checkbox and other form element metadata  
  29.       control.checkboxName = `${Common.CheckboxPrefix}${p.value}`;  
  30.       control.checkboxLabel = p.text;  
  31.       control.associateControlName = `${Common.OtherPrefix}${p.value}`;  
  32.       control.associateControlLabel = `${p.text} comments`;  
  33.       control.associateControlType = Common.ControlType[Common.ControlType.textbox];  
  34.         
  35.       //assume 1 is radio button list  
  36.        if (p.value == 1) {  
  37.           control.associateControlType = Common.ControlType[Common.ControlType.radioButtonList];  
  38.           control.associateControlData = this.phpVersionList;  
  39.       }  
  40.         
  41.       //just assumed id 4 is dropdown  
  42.       if (p.value == 4) {  
  43.           control.associateControlType = Common.ControlType[Common.ControlType.dropdown];  
  44.           control.associateControlData = this.otherProgrammingLanguageList;  
  45.       }  
  46.         
  47.       //store in array, use by html to loop through  
  48.       this.langControlMetada.push(control);  
  49.         
  50.       //form contol  
  51.       let checkBoxControl = this.formBuilder.control('');  
  52.       let associateControl = this.formBuilder.control({ value: '', disabled: true });  
  53.   
  54.       //add to form group [key, control]  
  55.       group.addControl(`${Common.CheckboxPrefix}${p.value}`, checkBoxControl);  
  56.       group.addControl(`${Common.OtherPrefix}${p.value}`, associateControl);  
  57.   
  58.       //add to form array  
  59.       this.programmingFormArray.push(group);  
  60.     }  
  61.   } 

Listing 7 shows how the programmingLanguage FormArray is being rendered in the HTML template. It uses the NgFor directive to build the "Your favorite programming language" section with the properties from the langControlMetada object. The template is very straightforward, the first element is a Checkbox and it has a change event associated with it. The second element will render as a textbox, radio button or dropdown list, depending on the item.associateControlType value.

Listing 7

  1. <div class="form-group row" formArrayName="programmingLanguage">  
  2.         <div class="col-xs-12"  
  3.              *ngFor="let item of langControlMetada; let i = index;">  
  4.             <div [formGroupName]="i">  
  5.                 <div class="form-group row">  
  6.                     <div class="form-inline" style="margin-left:15px;">  
  7.                         <div class="form-check">  
  8.                             <label [for]="item.checkboxName" class="form-check-label">  
  9.                                 <input type="checkbox" class="form-check-input" [id]="item.checkboxName"   
  10.                                         (change)="languageSelectionChange(i, item.checkboxName, item.associateControlName)"   
  11.                                         [formControlName]="item.checkboxName"> {{ item.checkboxLabel}}  
  12.                             </label>  
  13.                             <input *ngIf="item.associateControlType == 'textbox'"  
  14.                                    class="form-control form-control-sm"  
  15.                                    id="item.associateControlName"  
  16.                                    [placeholder]="item.associateControlLabel" maxlength="255"  
  17.                                    [formControlName]="item.associateControlName" />  
  18.                             <span *ngIf="item.associateControlType == 'dropdown'">  
  19.                                 <select class="form-control form-control-sm"  
  20.                                         [formControlName]="item.associateControlName">  
  21.                                     <option *ngFor="let item of item.associateControlData"  
  22.                                             [value]="item.value">  
  23.                                         {{item.text}}  
  24.                                     </option>  
  25.                                 </select>  
  26.                             </span>  
  27.                             <span *ngIf="item.associateControlType == 'radioButtonList'">  
  28.                                 <span *ngFor="let option of item.associateControlData">  
  29.                                     <input #version type="radio" [formControlName]="item.associateControlName"  
  30.                                            class="form-control form-control-sm"  
  31.                                            [value]="option.value">  
  32.                                     <label class="checkbox-inline" *ngIf="!version.disabled"> {{option.text}}</label>  
  33.                                 </span>  
  34.                             </span>  
  35.                         </div>  
  36.                     </div>  
  37.                  </div>  
  38.              </div>  
  39.          </div>  
  40.      </div> 

Hide and show a form control based on checkbox selection

Shown in listing 8 is the function to enable and disable, add and remove validators of an element through the checkbox checked change event. If the checkbox is checked, the business logic will use the enable() method to enable the associate control/element and set the field to required. The purpose of the updateValueAndValidity() method is to update the value and validation status of the control. On the other hand, unchecking the checkbox will clear the value of the associate element, disabled it and remove the validator.

Listing 8

  1. languageSelectionChange(pos: number, cnkName: string, txtName: string) {  
  2.   let programmingLanguage = this.programmingLanguages();  
  3.   
  4.   let control = programmingLanguage.controls[pos] as FormGroup  
  5.   
  6.   if (control.controls[cnkName].value == true) {  
  7.       //checkbox checked  
  8.       control.controls[txtName].enable();  
  9.       control.controls[txtName].setValidators([Validators.required]);  
  10.       control.controls[txtName].updateValueAndValidity();  
  11.       this.selectedLanguageCount++;  
  12.   }  
  13.   else {  
  14.       //unchecked  
  15.       control.controls[txtName].setValue('');  
  16.       control.controls[txtName].disable();  
  17.       control.controls[txtName].clearValidators();  
  18.       control.controls[txtName].updateValueAndValidity();  
  19.       this.selectedLanguageCount--;  
  20.   }  

Shown in listing 9 is the CSS to hide the control with disabled attribute.

Listing 9

  1. .form-control:disabled {  
  2.     displaynone;  

Collect the selected checkbox and dynamic control value

Once the form is valid, the submit button will become clickable. We will keep it simple by sending the selected checkbox id and the associated control/element value to the API.

Shown in listing 10 is the code for the button click event. The code will iterate the programmingLanguage control which is the FormArray to get the checked checkbox and associated control values in the FormGroup. Previously, the checkbox and associated control name were being generated by using a combination of a static text and the value/key from the data source. In order to get the checkbox id, first, get the first control name from the FormGroup. Then, remove the static text and parse it to the number. After that, use the id to generate the checkbox and associated control name and use it to access the control value in the FormGroup by name.

For instance, if the id is 3 then checkbox and associated control name will be “cbLanguage_3” and “otherValue_3” respectively. Then use the name to query the FormGroup to check if the checkbox value is true, if yes, collect the id and the associated control value. Refer to listing 10 for more details.

Listing 10

  1. programmingLanguages(): FormArray {  
  2.     return this.sampleForm.get('programmingLanguage') as FormArray;  
  3.   };  
  4.   
  5. public submit(e: any): void {  
  6.     e.preventDefault();  
  7.   
  8.     //reset  
  9.     let selectedLanguageList: Array<Item> = [];  
  10.     let programmingLanguage = this.programmingLanguages();  
  11.     let i: number;  
  12.     //checkbox id  
  13.     let languageId: number = 0;  
  14.   
  15.     for(i = 0; i < programmingLanguage.controls.length; i++) {  
  16.   
  17.         let control = programmingLanguage.controls[i] as FormGroup  
  18.         let selectedLanguage: Language = {} as any;  
  19.   
  20.         //get the selected checkbox id  
  21.         for (var k in control.controls) {  
  22.             languageId = Number(k.replace(/[a-zA-Z_]/g, ""));  
  23.             break;  
  24.         }  
  25.   
  26.         //capture the selected checkbox Id and textbox value  
  27.         if (control.controls[`${Common.CheckboxPrefix}${languageId}`].value == true) {  
  28.             selectedLanguage.value = languageId;  
  29.             selectedLanguage.text = control.controls[`${Common.OtherPrefix}${languageId}`].value  
  30.             selectedLanguageList.push(selectedLanguage);  
  31.         }  
  32.     }  
  33.   
  34.     if (selectedLanguageList.length == 0) {  
  35.         this.missingLanguage = true;  
  36.     } else {  
  37.         //submit to API  
  38.         let formObjectToApi = new FormControlMetadata();  
  39.   
  40.         formObjectToApi.lastName = this.sampleForm.controls['lastName'].value;  
  41.         formObjectToApi.firstName = this.sampleForm.controls['firstName'].value;  
  42.         formObjectToApi.email = this.sampleForm.controls['email'].value;  
  43.         formObjectToApi.selectedLanguages = selectedLanguageList;  
  44.   
  45.         this.missingLanguage = false;  
  46.         this.test = formObjectToApi;  
  47.     }  

Conclusion

I hope someone will find this information useful and make your programming job easier. If you find any bugs or disagree with the contents or want to help improve this article, please drop me a line and I'll work with you to correct it. I would suggest visiting the demo site and exploring it in order to grasp the full concept because I might miss some important information in this article. Please contact me if you want to help improve this article.

History

04/15/2018 - Initial version

Watch this script in action

Demo - Plunker

Resources

 


Similar Articles