Get access to FormControl from the custom form component in Angular

This solution was born from the discussion in the Angular repository. Please, make sure to read it or even better to participate if you are interested in this problem. I’ve studied the code of FormControlName directive and it’s inspired me to write the following solution: @Component({ selector: ‘my-custom-form-component’, templateUrl: ‘./custom-form-component.html’, providers: [{ provide: NG_VALUE_ACCESSOR, useExisting: … Read more

Angular – Dynamically add/remove validators

If you are using Angular 12.2 or higher, you can use the AbstractControl methods addValidators, removeValidators, and hasValidator, as per the docs: if(this.businessFormGroup.get(‘businessType’).value !== ‘Other’){ this.businessFormGroup.get(‘description’).addValidators(Validators.required); } else { this.businessFormGroup.get(‘description’).clearValidators(); } For older versions, Angular forms have a built in function setValidators() that enables programmatic assignment of Validators. However, this will overwrite your validators. For … Read more

Angular 2: Form submission canceled because the form is not connected

There might be other reasons this occurs but in my case I had a button that was interpreted by the browser as a submit button and hence the form was submitted when the button was clicked causing the error. Adding type=”button” fixed the issue. Full element: <button type=”button” (click)=”submitForm()”>

Confirm password validation in Angular 6 [duplicate]

This question could be solved with a combination of these two answers: https://stackoverflow.com/a/43493648/6294072 and https://stackoverflow.com/a/47670892/6294072 So first of all, you would need a custom validator for checking the passwords, that could look like this: checkPasswords: ValidatorFn = (group: AbstractControl): ValidationErrors | null => { let pass = group.get(‘password’).value; let confirmPass = group.get(‘confirmPassword’).value return pass === … Read more

tech