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;
}
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();
add a comment |
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();
Trythis.myform.get('field').setValidators([Validators.required]);and then callthis.myform.get('field').updateValueAndValidity();
– Daniel C.
Jan 17 '18 at 0:41
add a comment |
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();
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();
edited Jan 4 at 15:39
mpro
2,18421021
2,18421021
asked Jan 17 '18 at 0:37
Charles MorrisonCharles Morrison
183119
183119
Trythis.myform.get('field').setValidators([Validators.required]);and then callthis.myform.get('field').updateValueAndValidity();
– Daniel C.
Jan 17 '18 at 0:41
add a comment |
Trythis.myform.get('field').setValidators([Validators.required]);and then callthis.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
add a comment |
1 Answer
1
active
oldest
votes
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()
});
}
}
- We add a single required validator to mark this control as required.
- We can also provide an array of validators.
- We specify a pattern validator which checks whether the email contains a @ character.
- The minlength validator checks to see if the password is a minimum of 8 characters long.
- 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,
Set a validator for a control in the FormGroup:
this.formSearch.controls['name'].setValidators([Validators.required])Remove the validator from the control in the FormGroup:
this.formSearch.controls['name'].clearValidators()Update the FormGroup once you have run either of the above lines.
this.formSearch.controls['name'].updateValueAndValidity()
1
InvokeupdateValueAndValidityis very important and really often it's omitted
– kris_IV
Feb 19 '18 at 20:17
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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()
});
}
}
- We add a single required validator to mark this control as required.
- We can also provide an array of validators.
- We specify a pattern validator which checks whether the email contains a @ character.
- The minlength validator checks to see if the password is a minimum of 8 characters long.
- 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,
Set a validator for a control in the FormGroup:
this.formSearch.controls['name'].setValidators([Validators.required])Remove the validator from the control in the FormGroup:
this.formSearch.controls['name'].clearValidators()Update the FormGroup once you have run either of the above lines.
this.formSearch.controls['name'].updateValueAndValidity()
1
InvokeupdateValueAndValidityis very important and really often it's omitted
– kris_IV
Feb 19 '18 at 20:17
add a comment |
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()
});
}
}
- We add a single required validator to mark this control as required.
- We can also provide an array of validators.
- We specify a pattern validator which checks whether the email contains a @ character.
- The minlength validator checks to see if the password is a minimum of 8 characters long.
- 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,
Set a validator for a control in the FormGroup:
this.formSearch.controls['name'].setValidators([Validators.required])Remove the validator from the control in the FormGroup:
this.formSearch.controls['name'].clearValidators()Update the FormGroup once you have run either of the above lines.
this.formSearch.controls['name'].updateValueAndValidity()
1
InvokeupdateValueAndValidityis very important and really often it's omitted
– kris_IV
Feb 19 '18 at 20:17
add a comment |
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()
});
}
}
- We add a single required validator to mark this control as required.
- We can also provide an array of validators.
- We specify a pattern validator which checks whether the email contains a @ character.
- The minlength validator checks to see if the password is a minimum of 8 characters long.
- 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,
Set a validator for a control in the FormGroup:
this.formSearch.controls['name'].setValidators([Validators.required])Remove the validator from the control in the FormGroup:
this.formSearch.controls['name'].clearValidators()Update the FormGroup once you have run either of the above lines.
this.formSearch.controls['name'].updateValueAndValidity()
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()
});
}
}
- We add a single required validator to mark this control as required.
- We can also provide an array of validators.
- We specify a pattern validator which checks whether the email contains a @ character.
- The minlength validator checks to see if the password is a minimum of 8 characters long.
- 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,
Set a validator for a control in the FormGroup:
this.formSearch.controls['name'].setValidators([Validators.required])Remove the validator from the control in the FormGroup:
this.formSearch.controls['name'].clearValidators()Update the FormGroup once you have run either of the above lines.
this.formSearch.controls['name'].updateValueAndValidity()
edited Jan 17 '18 at 3:07
answered Jan 17 '18 at 0:53
Lakindu GunasekaraLakindu Gunasekara
2,61341430
2,61341430
1
InvokeupdateValueAndValidityis very important and really often it's omitted
– kris_IV
Feb 19 '18 at 20:17
add a comment |
1
InvokeupdateValueAndValidityis 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Try
this.myform.get('field').setValidators([Validators.required]);and then callthis.myform.get('field').updateValueAndValidity();– Daniel C.
Jan 17 '18 at 0:41