angular 5 validation form control update with required





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







3















I am using Angular 5. And How do I add required validation to an existing form control??



this.formSearch.get('name').clearValidators();
this.formSearch.get('name').updateValueAndValidity();









share|improve this question

























  • Try this.myform.get('field').setValidators([Validators.required]); and then call this.myform.get('field').updateValueAndValidity();

    – Daniel C.
    Jan 17 '18 at 0:41


















3















I am using Angular 5. And How do I add required validation to an existing form control??



this.formSearch.get('name').clearValidators();
this.formSearch.get('name').updateValueAndValidity();









share|improve this question

























  • Try this.myform.get('field').setValidators([Validators.required]); and then call this.myform.get('field').updateValueAndValidity();

    – Daniel C.
    Jan 17 '18 at 0:41














3












3








3








I am using Angular 5. And How do I add required validation to an existing form control??



this.formSearch.get('name').clearValidators();
this.formSearch.get('name').updateValueAndValidity();









share|improve this question
















I am using Angular 5. And How do I add required validation to an existing form control??



this.formSearch.get('name').clearValidators();
this.formSearch.get('name').updateValueAndValidity();






angular typescript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 4 at 15:39









mpro

2,18421021




2,18421021










asked Jan 17 '18 at 0:37









Charles MorrisonCharles Morrison

183119




183119













  • Try this.myform.get('field').setValidators([Validators.required]); and then call this.myform.get('field').updateValueAndValidity();

    – Daniel C.
    Jan 17 '18 at 0:41



















  • Try this.myform.get('field').setValidators([Validators.required]); and then call this.myform.get('field').updateValueAndValidity();

    – Daniel C.
    Jan 17 '18 at 0:41

















Try this.myform.get('field').setValidators([Validators.required]); and then call this.myform.get('field').updateValueAndValidity();

– Daniel C.
Jan 17 '18 at 0:41





Try this.myform.get('field').setValidators([Validators.required]); and then call this.myform.get('field').updateValueAndValidity();

– Daniel C.
Jan 17 '18 at 0:41












1 Answer
1






active

oldest

votes


















4














Angular comes with a small set of pre-built validators to match the ones we can define via standard HTML5 attributes, namely required, minlegth, maxlength and pattern which we can access from the Validators module.



The first parameter of a FormControl constructor is the initial value of the control, we’ll leave that as empty string. The second parameter contains either a single validator if we only want to apply one, or a list of validators if we want to apply multiple validators to a single control.



import { FormGroup, FormControl, Validators } from '@angular/forms';

class ModelFormComponent implements OnInit {
myform: FormGroup;

ngOnInit() {
myform = new FormGroup({
name: new FormGroup({
firstName: new FormControl('', Validators.required),
lastName: new FormControl('', Validators.required),
}),
email: new FormControl('', [
Validators.required,
Validators.pattern("[^ @]*@[^ @]*")
]),
password: new FormControl('', [
Validators.minLength(8),
Validators.required
]),
language: new FormControl()
});
}
}



  1. We add a single required validator to mark this control as required.

  2. We can also provide an array of validators.

  3. We specify a pattern validator which checks whether the email contains a @ character.

  4. The minlength validator checks to see if the password is a minimum of 8 characters long.

  5. We don’t add any validators to the language select box.


IN YOUR SCENARIO
You can use something like this



this.formSearch.controls["name"].setValidators([Validators.required]‌​,[Validators.minLength(1), Validators.maxLength(30)]);


Here you simply pass the FormControl an array of validators. And this will reset any existing validators you added when you created the FormControl.



Additionally,




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


  2. Remove the validator from the control in the FormGroup:
    this.formSearch.controls['name'].clearValidators()


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







share|improve this answer





















  • 1





    Invoke updateValueAndValidity is very important and really often it's omitted

    – kris_IV
    Feb 19 '18 at 20:17












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%2f48292000%2fangular-5-validation-form-control-update-with-required%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









4














Angular comes with a small set of pre-built validators to match the ones we can define via standard HTML5 attributes, namely required, minlegth, maxlength and pattern which we can access from the Validators module.



The first parameter of a FormControl constructor is the initial value of the control, we’ll leave that as empty string. The second parameter contains either a single validator if we only want to apply one, or a list of validators if we want to apply multiple validators to a single control.



import { FormGroup, FormControl, Validators } from '@angular/forms';

class ModelFormComponent implements OnInit {
myform: FormGroup;

ngOnInit() {
myform = new FormGroup({
name: new FormGroup({
firstName: new FormControl('', Validators.required),
lastName: new FormControl('', Validators.required),
}),
email: new FormControl('', [
Validators.required,
Validators.pattern("[^ @]*@[^ @]*")
]),
password: new FormControl('', [
Validators.minLength(8),
Validators.required
]),
language: new FormControl()
});
}
}



  1. We add a single required validator to mark this control as required.

  2. We can also provide an array of validators.

  3. We specify a pattern validator which checks whether the email contains a @ character.

  4. The minlength validator checks to see if the password is a minimum of 8 characters long.

  5. We don’t add any validators to the language select box.


IN YOUR SCENARIO
You can use something like this



this.formSearch.controls["name"].setValidators([Validators.required]‌​,[Validators.minLength(1), Validators.maxLength(30)]);


Here you simply pass the FormControl an array of validators. And this will reset any existing validators you added when you created the FormControl.



Additionally,




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


  2. Remove the validator from the control in the FormGroup:
    this.formSearch.controls['name'].clearValidators()


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







share|improve this answer





















  • 1





    Invoke updateValueAndValidity is very important and really often it's omitted

    – kris_IV
    Feb 19 '18 at 20:17
















4














Angular comes with a small set of pre-built validators to match the ones we can define via standard HTML5 attributes, namely required, minlegth, maxlength and pattern which we can access from the Validators module.



The first parameter of a FormControl constructor is the initial value of the control, we’ll leave that as empty string. The second parameter contains either a single validator if we only want to apply one, or a list of validators if we want to apply multiple validators to a single control.



import { FormGroup, FormControl, Validators } from '@angular/forms';

class ModelFormComponent implements OnInit {
myform: FormGroup;

ngOnInit() {
myform = new FormGroup({
name: new FormGroup({
firstName: new FormControl('', Validators.required),
lastName: new FormControl('', Validators.required),
}),
email: new FormControl('', [
Validators.required,
Validators.pattern("[^ @]*@[^ @]*")
]),
password: new FormControl('', [
Validators.minLength(8),
Validators.required
]),
language: new FormControl()
});
}
}



  1. We add a single required validator to mark this control as required.

  2. We can also provide an array of validators.

  3. We specify a pattern validator which checks whether the email contains a @ character.

  4. The minlength validator checks to see if the password is a minimum of 8 characters long.

  5. We don’t add any validators to the language select box.


IN YOUR SCENARIO
You can use something like this



this.formSearch.controls["name"].setValidators([Validators.required]‌​,[Validators.minLength(1), Validators.maxLength(30)]);


Here you simply pass the FormControl an array of validators. And this will reset any existing validators you added when you created the FormControl.



Additionally,




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


  2. Remove the validator from the control in the FormGroup:
    this.formSearch.controls['name'].clearValidators()


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







share|improve this answer





















  • 1





    Invoke updateValueAndValidity is very important and really often it's omitted

    – kris_IV
    Feb 19 '18 at 20:17














4












4








4







Angular comes with a small set of pre-built validators to match the ones we can define via standard HTML5 attributes, namely required, minlegth, maxlength and pattern which we can access from the Validators module.



The first parameter of a FormControl constructor is the initial value of the control, we’ll leave that as empty string. The second parameter contains either a single validator if we only want to apply one, or a list of validators if we want to apply multiple validators to a single control.



import { FormGroup, FormControl, Validators } from '@angular/forms';

class ModelFormComponent implements OnInit {
myform: FormGroup;

ngOnInit() {
myform = new FormGroup({
name: new FormGroup({
firstName: new FormControl('', Validators.required),
lastName: new FormControl('', Validators.required),
}),
email: new FormControl('', [
Validators.required,
Validators.pattern("[^ @]*@[^ @]*")
]),
password: new FormControl('', [
Validators.minLength(8),
Validators.required
]),
language: new FormControl()
});
}
}



  1. We add a single required validator to mark this control as required.

  2. We can also provide an array of validators.

  3. We specify a pattern validator which checks whether the email contains a @ character.

  4. The minlength validator checks to see if the password is a minimum of 8 characters long.

  5. We don’t add any validators to the language select box.


IN YOUR SCENARIO
You can use something like this



this.formSearch.controls["name"].setValidators([Validators.required]‌​,[Validators.minLength(1), Validators.maxLength(30)]);


Here you simply pass the FormControl an array of validators. And this will reset any existing validators you added when you created the FormControl.



Additionally,




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


  2. Remove the validator from the control in the FormGroup:
    this.formSearch.controls['name'].clearValidators()


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







share|improve this answer















Angular comes with a small set of pre-built validators to match the ones we can define via standard HTML5 attributes, namely required, minlegth, maxlength and pattern which we can access from the Validators module.



The first parameter of a FormControl constructor is the initial value of the control, we’ll leave that as empty string. The second parameter contains either a single validator if we only want to apply one, or a list of validators if we want to apply multiple validators to a single control.



import { FormGroup, FormControl, Validators } from '@angular/forms';

class ModelFormComponent implements OnInit {
myform: FormGroup;

ngOnInit() {
myform = new FormGroup({
name: new FormGroup({
firstName: new FormControl('', Validators.required),
lastName: new FormControl('', Validators.required),
}),
email: new FormControl('', [
Validators.required,
Validators.pattern("[^ @]*@[^ @]*")
]),
password: new FormControl('', [
Validators.minLength(8),
Validators.required
]),
language: new FormControl()
});
}
}



  1. We add a single required validator to mark this control as required.

  2. We can also provide an array of validators.

  3. We specify a pattern validator which checks whether the email contains a @ character.

  4. The minlength validator checks to see if the password is a minimum of 8 characters long.

  5. We don’t add any validators to the language select box.


IN YOUR SCENARIO
You can use something like this



this.formSearch.controls["name"].setValidators([Validators.required]‌​,[Validators.minLength(1), Validators.maxLength(30)]);


Here you simply pass the FormControl an array of validators. And this will reset any existing validators you added when you created the FormControl.



Additionally,




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


  2. Remove the validator from the control in the FormGroup:
    this.formSearch.controls['name'].clearValidators()


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








share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 17 '18 at 3:07

























answered Jan 17 '18 at 0:53









Lakindu GunasekaraLakindu Gunasekara

2,61341430




2,61341430








  • 1





    Invoke updateValueAndValidity is very important and really often it's omitted

    – kris_IV
    Feb 19 '18 at 20:17














  • 1





    Invoke updateValueAndValidity is very important and really often it's omitted

    – kris_IV
    Feb 19 '18 at 20:17








1




1





Invoke updateValueAndValidity is very important and really often it's omitted

– kris_IV
Feb 19 '18 at 20:17





Invoke updateValueAndValidity is very important and really often it's omitted

– kris_IV
Feb 19 '18 at 20:17




















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f48292000%2fangular-5-validation-form-control-update-with-required%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

Mossoró

Error while reading .h5 file using the rhdf5 package in R

Pushsharp Apns notification error: 'InvalidToken'