Angular How to add Validator to FormControl after control is created?












33














We have a component that has a dynamically built form. The code to add a control with validators might look like this



var c = new FormControl('', Validators.required);


But let's say that I want to add 2nd Validator later. How can we accomplish this? We cannot find any documentation on this online. I did find though in the form controls there is setValidators



this.form.controls["firstName"].setValidators 


but it is not clear how to add a new or custom validator.



Thanks a bunch.










share|improve this question





























    33














    We have a component that has a dynamically built form. The code to add a control with validators might look like this



    var c = new FormControl('', Validators.required);


    But let's say that I want to add 2nd Validator later. How can we accomplish this? We cannot find any documentation on this online. I did find though in the form controls there is setValidators



    this.form.controls["firstName"].setValidators 


    but it is not clear how to add a new or custom validator.



    Thanks a bunch.










    share|improve this question



























      33












      33








      33


      9





      We have a component that has a dynamically built form. The code to add a control with validators might look like this



      var c = new FormControl('', Validators.required);


      But let's say that I want to add 2nd Validator later. How can we accomplish this? We cannot find any documentation on this online. I did find though in the form controls there is setValidators



      this.form.controls["firstName"].setValidators 


      but it is not clear how to add a new or custom validator.



      Thanks a bunch.










      share|improve this question















      We have a component that has a dynamically built form. The code to add a control with validators might look like this



      var c = new FormControl('', Validators.required);


      But let's say that I want to add 2nd Validator later. How can we accomplish this? We cannot find any documentation on this online. I did find though in the form controls there is setValidators



      this.form.controls["firstName"].setValidators 


      but it is not clear how to add a new or custom validator.



      Thanks a bunch.







      forms angular






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 14 '18 at 11:40









      shammelburg

      2,42241829




      2,42241829










      asked Aug 5 '16 at 20:34









      melegantmelegant

      1991211




      1991211
























          3 Answers
          3






          active

          oldest

          votes


















          49














          You simply pass the FormControl an array of validators.



          Here's an example showing how you can add validators to an existing FormControl:



          this.form.controls["firstName"].setValidators([Validators.minLength(1), Validators.maxLength(30)]);


          Note, this will reset any existing validators you added when you created the FormControl.






          share|improve this answer



















          • 4




            ha...sometimes you look at something so long it is best to just step away. THANK YOU!!
            – melegant
            Aug 6 '16 at 14:43










          • glad to help :)
            – Delosdos
            Aug 6 '16 at 14:52










          • This really solved the problem that I was trying to fix for a long time. Thanks a ton for sharing!
            – Devner
            May 12 '17 at 5:28






          • 1




            Is there a way to remove validation
            – Abhijith s.s
            Aug 18 '17 at 11:57






          • 4




            this.form.controls["firstName"].clearValidators()
            – DDD Soft
            Sep 6 '17 at 14:56



















          36














          To add onto what @Delosdos has posted.



          Set a validator for a control in the FormGroup:
          this.myForm.controls['controlName'].setValidators([Validators.required])



          Remove the validator from the control in the FormGroup:
          this.myForm.controls['controlName'].clearValidators()



          Update the FormGroup once you have run either of the above lines.
          this.myForm.controls['controlName'].updateValueAndValidity()



          This is an amazing way to programmatically set your form validation.






          share|improve this answer





















          • For me it worked without the last line, I'm pretty sure new versions of Angular update the form validity by themselves now. But thanks for telling us about the updateValueAndValidity method, might come handy one day!
            – Nino Filiu
            Jul 24 '18 at 9:06










          • @NinoFiliu updateValueAndValidity is still used to perform the validation, and isn't handled any differently in newer versions of Angular. What happens is setValidators updates the validators, but doesn't run a validation check, and then you use updateValueAndValidity to perform the validation. You must be setting the validators at a point where change detection handles it for you, but you'll find using updateValueAndValidity on the group or control depending what validator you just set crucial - github.com/angular/angular/issues/19622#issuecomment-341547884.
            – mtpultz
            Aug 9 '18 at 19:44










          • I'm on Angular 6 and couldn't make it work without the updateValueAndValidity. Thanks @shammelburg !
            – Oli Crt
            Dec 20 '18 at 16:12



















          6














          If you are using reactiveFormModule and have formGroup defined like this:



          public exampleForm = new FormGroup({
          name: new FormControl('Test name', [Validators.required, Validators.minLength(3)]),
          email: new FormControl('test@example.com', [Validators.required, Validators.maxLength(50)]),
          age: new FormControl(45, [Validators.min(18), Validators.max(65)])
          });


          than you are able to add a new validator (and keep old ones) to FormControl with this approach:



          this.exampleForm.get('age').setValidators([
          Validators.pattern('^[0-9]*$'),
          this.exampleForm.get('age').validator
          ]);
          this.exampleForm.get('email').setValidators([
          Validators.email,
          this.exampleForm.get('email').validator
          ]);


          FormControl.validator returns a compose validator containing all previously defined validators.






          share|improve this answer

















          • 2




            Imo this should be the accepted answer. It demonstrates how to add validators like OP requested, but also shows you how to retain previously set validators; which was the first thing I googled how to do after I read the accepted answer, because I didn't want to overwrite some validators I already had, but still needed to programatically add additional ones. Thank you for this answer @Eduard Void
            – Chris
            Nov 16 '18 at 20:45










          • I agree with my predecessor. The question was how to add new validator to control form, not how to replace it.
            – Plusce
            Dec 5 '18 at 14:57











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f38797414%2fangular-how-to-add-validator-to-formcontrol-after-control-is-created%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          49














          You simply pass the FormControl an array of validators.



          Here's an example showing how you can add validators to an existing FormControl:



          this.form.controls["firstName"].setValidators([Validators.minLength(1), Validators.maxLength(30)]);


          Note, this will reset any existing validators you added when you created the FormControl.






          share|improve this answer



















          • 4




            ha...sometimes you look at something so long it is best to just step away. THANK YOU!!
            – melegant
            Aug 6 '16 at 14:43










          • glad to help :)
            – Delosdos
            Aug 6 '16 at 14:52










          • This really solved the problem that I was trying to fix for a long time. Thanks a ton for sharing!
            – Devner
            May 12 '17 at 5:28






          • 1




            Is there a way to remove validation
            – Abhijith s.s
            Aug 18 '17 at 11:57






          • 4




            this.form.controls["firstName"].clearValidators()
            – DDD Soft
            Sep 6 '17 at 14:56
















          49














          You simply pass the FormControl an array of validators.



          Here's an example showing how you can add validators to an existing FormControl:



          this.form.controls["firstName"].setValidators([Validators.minLength(1), Validators.maxLength(30)]);


          Note, this will reset any existing validators you added when you created the FormControl.






          share|improve this answer



















          • 4




            ha...sometimes you look at something so long it is best to just step away. THANK YOU!!
            – melegant
            Aug 6 '16 at 14:43










          • glad to help :)
            – Delosdos
            Aug 6 '16 at 14:52










          • This really solved the problem that I was trying to fix for a long time. Thanks a ton for sharing!
            – Devner
            May 12 '17 at 5:28






          • 1




            Is there a way to remove validation
            – Abhijith s.s
            Aug 18 '17 at 11:57






          • 4




            this.form.controls["firstName"].clearValidators()
            – DDD Soft
            Sep 6 '17 at 14:56














          49












          49








          49






          You simply pass the FormControl an array of validators.



          Here's an example showing how you can add validators to an existing FormControl:



          this.form.controls["firstName"].setValidators([Validators.minLength(1), Validators.maxLength(30)]);


          Note, this will reset any existing validators you added when you created the FormControl.






          share|improve this answer














          You simply pass the FormControl an array of validators.



          Here's an example showing how you can add validators to an existing FormControl:



          this.form.controls["firstName"].setValidators([Validators.minLength(1), Validators.maxLength(30)]);


          Note, this will reset any existing validators you added when you created the FormControl.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Aug 5 '16 at 21:00

























          answered Aug 5 '16 at 20:47









          DelosdosDelosdos

          1,872820




          1,872820








          • 4




            ha...sometimes you look at something so long it is best to just step away. THANK YOU!!
            – melegant
            Aug 6 '16 at 14:43










          • glad to help :)
            – Delosdos
            Aug 6 '16 at 14:52










          • This really solved the problem that I was trying to fix for a long time. Thanks a ton for sharing!
            – Devner
            May 12 '17 at 5:28






          • 1




            Is there a way to remove validation
            – Abhijith s.s
            Aug 18 '17 at 11:57






          • 4




            this.form.controls["firstName"].clearValidators()
            – DDD Soft
            Sep 6 '17 at 14:56














          • 4




            ha...sometimes you look at something so long it is best to just step away. THANK YOU!!
            – melegant
            Aug 6 '16 at 14:43










          • glad to help :)
            – Delosdos
            Aug 6 '16 at 14:52










          • This really solved the problem that I was trying to fix for a long time. Thanks a ton for sharing!
            – Devner
            May 12 '17 at 5:28






          • 1




            Is there a way to remove validation
            – Abhijith s.s
            Aug 18 '17 at 11:57






          • 4




            this.form.controls["firstName"].clearValidators()
            – DDD Soft
            Sep 6 '17 at 14:56








          4




          4




          ha...sometimes you look at something so long it is best to just step away. THANK YOU!!
          – melegant
          Aug 6 '16 at 14:43




          ha...sometimes you look at something so long it is best to just step away. THANK YOU!!
          – melegant
          Aug 6 '16 at 14:43












          glad to help :)
          – Delosdos
          Aug 6 '16 at 14:52




          glad to help :)
          – Delosdos
          Aug 6 '16 at 14:52












          This really solved the problem that I was trying to fix for a long time. Thanks a ton for sharing!
          – Devner
          May 12 '17 at 5:28




          This really solved the problem that I was trying to fix for a long time. Thanks a ton for sharing!
          – Devner
          May 12 '17 at 5:28




          1




          1




          Is there a way to remove validation
          – Abhijith s.s
          Aug 18 '17 at 11:57




          Is there a way to remove validation
          – Abhijith s.s
          Aug 18 '17 at 11:57




          4




          4




          this.form.controls["firstName"].clearValidators()
          – DDD Soft
          Sep 6 '17 at 14:56




          this.form.controls["firstName"].clearValidators()
          – DDD Soft
          Sep 6 '17 at 14:56













          36














          To add onto what @Delosdos has posted.



          Set a validator for a control in the FormGroup:
          this.myForm.controls['controlName'].setValidators([Validators.required])



          Remove the validator from the control in the FormGroup:
          this.myForm.controls['controlName'].clearValidators()



          Update the FormGroup once you have run either of the above lines.
          this.myForm.controls['controlName'].updateValueAndValidity()



          This is an amazing way to programmatically set your form validation.






          share|improve this answer





















          • For me it worked without the last line, I'm pretty sure new versions of Angular update the form validity by themselves now. But thanks for telling us about the updateValueAndValidity method, might come handy one day!
            – Nino Filiu
            Jul 24 '18 at 9:06










          • @NinoFiliu updateValueAndValidity is still used to perform the validation, and isn't handled any differently in newer versions of Angular. What happens is setValidators updates the validators, but doesn't run a validation check, and then you use updateValueAndValidity to perform the validation. You must be setting the validators at a point where change detection handles it for you, but you'll find using updateValueAndValidity on the group or control depending what validator you just set crucial - github.com/angular/angular/issues/19622#issuecomment-341547884.
            – mtpultz
            Aug 9 '18 at 19:44










          • I'm on Angular 6 and couldn't make it work without the updateValueAndValidity. Thanks @shammelburg !
            – Oli Crt
            Dec 20 '18 at 16:12
















          36














          To add onto what @Delosdos has posted.



          Set a validator for a control in the FormGroup:
          this.myForm.controls['controlName'].setValidators([Validators.required])



          Remove the validator from the control in the FormGroup:
          this.myForm.controls['controlName'].clearValidators()



          Update the FormGroup once you have run either of the above lines.
          this.myForm.controls['controlName'].updateValueAndValidity()



          This is an amazing way to programmatically set your form validation.






          share|improve this answer





















          • For me it worked without the last line, I'm pretty sure new versions of Angular update the form validity by themselves now. But thanks for telling us about the updateValueAndValidity method, might come handy one day!
            – Nino Filiu
            Jul 24 '18 at 9:06










          • @NinoFiliu updateValueAndValidity is still used to perform the validation, and isn't handled any differently in newer versions of Angular. What happens is setValidators updates the validators, but doesn't run a validation check, and then you use updateValueAndValidity to perform the validation. You must be setting the validators at a point where change detection handles it for you, but you'll find using updateValueAndValidity on the group or control depending what validator you just set crucial - github.com/angular/angular/issues/19622#issuecomment-341547884.
            – mtpultz
            Aug 9 '18 at 19:44










          • I'm on Angular 6 and couldn't make it work without the updateValueAndValidity. Thanks @shammelburg !
            – Oli Crt
            Dec 20 '18 at 16:12














          36












          36








          36






          To add onto what @Delosdos has posted.



          Set a validator for a control in the FormGroup:
          this.myForm.controls['controlName'].setValidators([Validators.required])



          Remove the validator from the control in the FormGroup:
          this.myForm.controls['controlName'].clearValidators()



          Update the FormGroup once you have run either of the above lines.
          this.myForm.controls['controlName'].updateValueAndValidity()



          This is an amazing way to programmatically set your form validation.






          share|improve this answer












          To add onto what @Delosdos has posted.



          Set a validator for a control in the FormGroup:
          this.myForm.controls['controlName'].setValidators([Validators.required])



          Remove the validator from the control in the FormGroup:
          this.myForm.controls['controlName'].clearValidators()



          Update the FormGroup once you have run either of the above lines.
          this.myForm.controls['controlName'].updateValueAndValidity()



          This is an amazing way to programmatically set your form validation.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 11 '17 at 11:21









          shammelburgshammelburg

          2,42241829




          2,42241829












          • For me it worked without the last line, I'm pretty sure new versions of Angular update the form validity by themselves now. But thanks for telling us about the updateValueAndValidity method, might come handy one day!
            – Nino Filiu
            Jul 24 '18 at 9:06










          • @NinoFiliu updateValueAndValidity is still used to perform the validation, and isn't handled any differently in newer versions of Angular. What happens is setValidators updates the validators, but doesn't run a validation check, and then you use updateValueAndValidity to perform the validation. You must be setting the validators at a point where change detection handles it for you, but you'll find using updateValueAndValidity on the group or control depending what validator you just set crucial - github.com/angular/angular/issues/19622#issuecomment-341547884.
            – mtpultz
            Aug 9 '18 at 19:44










          • I'm on Angular 6 and couldn't make it work without the updateValueAndValidity. Thanks @shammelburg !
            – Oli Crt
            Dec 20 '18 at 16:12


















          • For me it worked without the last line, I'm pretty sure new versions of Angular update the form validity by themselves now. But thanks for telling us about the updateValueAndValidity method, might come handy one day!
            – Nino Filiu
            Jul 24 '18 at 9:06










          • @NinoFiliu updateValueAndValidity is still used to perform the validation, and isn't handled any differently in newer versions of Angular. What happens is setValidators updates the validators, but doesn't run a validation check, and then you use updateValueAndValidity to perform the validation. You must be setting the validators at a point where change detection handles it for you, but you'll find using updateValueAndValidity on the group or control depending what validator you just set crucial - github.com/angular/angular/issues/19622#issuecomment-341547884.
            – mtpultz
            Aug 9 '18 at 19:44










          • I'm on Angular 6 and couldn't make it work without the updateValueAndValidity. Thanks @shammelburg !
            – Oli Crt
            Dec 20 '18 at 16:12
















          For me it worked without the last line, I'm pretty sure new versions of Angular update the form validity by themselves now. But thanks for telling us about the updateValueAndValidity method, might come handy one day!
          – Nino Filiu
          Jul 24 '18 at 9:06




          For me it worked without the last line, I'm pretty sure new versions of Angular update the form validity by themselves now. But thanks for telling us about the updateValueAndValidity method, might come handy one day!
          – Nino Filiu
          Jul 24 '18 at 9:06












          @NinoFiliu updateValueAndValidity is still used to perform the validation, and isn't handled any differently in newer versions of Angular. What happens is setValidators updates the validators, but doesn't run a validation check, and then you use updateValueAndValidity to perform the validation. You must be setting the validators at a point where change detection handles it for you, but you'll find using updateValueAndValidity on the group or control depending what validator you just set crucial - github.com/angular/angular/issues/19622#issuecomment-341547884.
          – mtpultz
          Aug 9 '18 at 19:44




          @NinoFiliu updateValueAndValidity is still used to perform the validation, and isn't handled any differently in newer versions of Angular. What happens is setValidators updates the validators, but doesn't run a validation check, and then you use updateValueAndValidity to perform the validation. You must be setting the validators at a point where change detection handles it for you, but you'll find using updateValueAndValidity on the group or control depending what validator you just set crucial - github.com/angular/angular/issues/19622#issuecomment-341547884.
          – mtpultz
          Aug 9 '18 at 19:44












          I'm on Angular 6 and couldn't make it work without the updateValueAndValidity. Thanks @shammelburg !
          – Oli Crt
          Dec 20 '18 at 16:12




          I'm on Angular 6 and couldn't make it work without the updateValueAndValidity. Thanks @shammelburg !
          – Oli Crt
          Dec 20 '18 at 16:12











          6














          If you are using reactiveFormModule and have formGroup defined like this:



          public exampleForm = new FormGroup({
          name: new FormControl('Test name', [Validators.required, Validators.minLength(3)]),
          email: new FormControl('test@example.com', [Validators.required, Validators.maxLength(50)]),
          age: new FormControl(45, [Validators.min(18), Validators.max(65)])
          });


          than you are able to add a new validator (and keep old ones) to FormControl with this approach:



          this.exampleForm.get('age').setValidators([
          Validators.pattern('^[0-9]*$'),
          this.exampleForm.get('age').validator
          ]);
          this.exampleForm.get('email').setValidators([
          Validators.email,
          this.exampleForm.get('email').validator
          ]);


          FormControl.validator returns a compose validator containing all previously defined validators.






          share|improve this answer

















          • 2




            Imo this should be the accepted answer. It demonstrates how to add validators like OP requested, but also shows you how to retain previously set validators; which was the first thing I googled how to do after I read the accepted answer, because I didn't want to overwrite some validators I already had, but still needed to programatically add additional ones. Thank you for this answer @Eduard Void
            – Chris
            Nov 16 '18 at 20:45










          • I agree with my predecessor. The question was how to add new validator to control form, not how to replace it.
            – Plusce
            Dec 5 '18 at 14:57
















          6














          If you are using reactiveFormModule and have formGroup defined like this:



          public exampleForm = new FormGroup({
          name: new FormControl('Test name', [Validators.required, Validators.minLength(3)]),
          email: new FormControl('test@example.com', [Validators.required, Validators.maxLength(50)]),
          age: new FormControl(45, [Validators.min(18), Validators.max(65)])
          });


          than you are able to add a new validator (and keep old ones) to FormControl with this approach:



          this.exampleForm.get('age').setValidators([
          Validators.pattern('^[0-9]*$'),
          this.exampleForm.get('age').validator
          ]);
          this.exampleForm.get('email').setValidators([
          Validators.email,
          this.exampleForm.get('email').validator
          ]);


          FormControl.validator returns a compose validator containing all previously defined validators.






          share|improve this answer

















          • 2




            Imo this should be the accepted answer. It demonstrates how to add validators like OP requested, but also shows you how to retain previously set validators; which was the first thing I googled how to do after I read the accepted answer, because I didn't want to overwrite some validators I already had, but still needed to programatically add additional ones. Thank you for this answer @Eduard Void
            – Chris
            Nov 16 '18 at 20:45










          • I agree with my predecessor. The question was how to add new validator to control form, not how to replace it.
            – Plusce
            Dec 5 '18 at 14:57














          6












          6








          6






          If you are using reactiveFormModule and have formGroup defined like this:



          public exampleForm = new FormGroup({
          name: new FormControl('Test name', [Validators.required, Validators.minLength(3)]),
          email: new FormControl('test@example.com', [Validators.required, Validators.maxLength(50)]),
          age: new FormControl(45, [Validators.min(18), Validators.max(65)])
          });


          than you are able to add a new validator (and keep old ones) to FormControl with this approach:



          this.exampleForm.get('age').setValidators([
          Validators.pattern('^[0-9]*$'),
          this.exampleForm.get('age').validator
          ]);
          this.exampleForm.get('email').setValidators([
          Validators.email,
          this.exampleForm.get('email').validator
          ]);


          FormControl.validator returns a compose validator containing all previously defined validators.






          share|improve this answer












          If you are using reactiveFormModule and have formGroup defined like this:



          public exampleForm = new FormGroup({
          name: new FormControl('Test name', [Validators.required, Validators.minLength(3)]),
          email: new FormControl('test@example.com', [Validators.required, Validators.maxLength(50)]),
          age: new FormControl(45, [Validators.min(18), Validators.max(65)])
          });


          than you are able to add a new validator (and keep old ones) to FormControl with this approach:



          this.exampleForm.get('age').setValidators([
          Validators.pattern('^[0-9]*$'),
          this.exampleForm.get('age').validator
          ]);
          this.exampleForm.get('email').setValidators([
          Validators.email,
          this.exampleForm.get('email').validator
          ]);


          FormControl.validator returns a compose validator containing all previously defined validators.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 8:32









          Eduard VoidEduard Void

          1,30288




          1,30288








          • 2




            Imo this should be the accepted answer. It demonstrates how to add validators like OP requested, but also shows you how to retain previously set validators; which was the first thing I googled how to do after I read the accepted answer, because I didn't want to overwrite some validators I already had, but still needed to programatically add additional ones. Thank you for this answer @Eduard Void
            – Chris
            Nov 16 '18 at 20:45










          • I agree with my predecessor. The question was how to add new validator to control form, not how to replace it.
            – Plusce
            Dec 5 '18 at 14:57














          • 2




            Imo this should be the accepted answer. It demonstrates how to add validators like OP requested, but also shows you how to retain previously set validators; which was the first thing I googled how to do after I read the accepted answer, because I didn't want to overwrite some validators I already had, but still needed to programatically add additional ones. Thank you for this answer @Eduard Void
            – Chris
            Nov 16 '18 at 20:45










          • I agree with my predecessor. The question was how to add new validator to control form, not how to replace it.
            – Plusce
            Dec 5 '18 at 14:57








          2




          2




          Imo this should be the accepted answer. It demonstrates how to add validators like OP requested, but also shows you how to retain previously set validators; which was the first thing I googled how to do after I read the accepted answer, because I didn't want to overwrite some validators I already had, but still needed to programatically add additional ones. Thank you for this answer @Eduard Void
          – Chris
          Nov 16 '18 at 20:45




          Imo this should be the accepted answer. It demonstrates how to add validators like OP requested, but also shows you how to retain previously set validators; which was the first thing I googled how to do after I read the accepted answer, because I didn't want to overwrite some validators I already had, but still needed to programatically add additional ones. Thank you for this answer @Eduard Void
          – Chris
          Nov 16 '18 at 20:45












          I agree with my predecessor. The question was how to add new validator to control form, not how to replace it.
          – Plusce
          Dec 5 '18 at 14:57




          I agree with my predecessor. The question was how to add new validator to control form, not how to replace it.
          – Plusce
          Dec 5 '18 at 14:57


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f38797414%2fangular-how-to-add-validator-to-formcontrol-after-control-is-created%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          List item for chat from Array inside array React Native

          Thiostrepton

          Caerphilly