Validators.required throws an error, even though there is a value inside of it
my issue is that I have a MatInput as follows:
<input matInput [(ngModel)]="name" i18n-placeholder="@@Placeholder" placeholder="Your name" [formControl]="InputControl" value=" fieldName ">
In the correspoding component I declared fieldName: string;
and I am setting the value in the ngOnInit()
method:
ngOnInit()
this.fieldName = "VALUE"; // obtained from server so it may differ
if(this.fieldName.startsWith("PREFIX")) // if value starts with a given prefix I want to remove it
this.fieldName = this.fieldName.substr(3);
The thing is that it's actually working well (value gets displayed properly), but as soon as I want to submit the MatDialog form it tells me that I can't submit an empty form! Thats because I added a 'required' validator:
InputControl = new FormControl("", [Validators.required]);
So there is a value in the input field, but apparently Angular doesn't notice it? If I add a space to the value and delete it afterwards the value is exactly the same, but then it doesn't throw any errors.
Does anyone know how to fix this? It's really annoying.
javascript angular validation
add a comment |
my issue is that I have a MatInput as follows:
<input matInput [(ngModel)]="name" i18n-placeholder="@@Placeholder" placeholder="Your name" [formControl]="InputControl" value=" fieldName ">
In the correspoding component I declared fieldName: string;
and I am setting the value in the ngOnInit()
method:
ngOnInit()
this.fieldName = "VALUE"; // obtained from server so it may differ
if(this.fieldName.startsWith("PREFIX")) // if value starts with a given prefix I want to remove it
this.fieldName = this.fieldName.substr(3);
The thing is that it's actually working well (value gets displayed properly), but as soon as I want to submit the MatDialog form it tells me that I can't submit an empty form! Thats because I added a 'required' validator:
InputControl = new FormControl("", [Validators.required]);
So there is a value in the input field, but apparently Angular doesn't notice it? If I add a space to the value and delete it afterwards the value is exactly the same, but then it doesn't throw any errors.
Does anyone know how to fix this? It's really annoying.
javascript angular validation
You're usingngModel
but also talking aboutFormControl
. These are two conflicting form implementations:ngModel
two-way data binding can't be used withFormControl
and viceversa. Either you use theformControl
directive to bind the to thatFormControl
object, or you use therequired
attribute to make the input required without using FormControl. See also: What are the practical differences between template-driven and reactive forms?
– Badashi
Nov 12 '18 at 11:54
you are mixingngModel
and value and FormControl, are you sure you want to set value? Perhaps you want to set thename
of the inputname="fieldName"
, or you want to set a default value? Then you should update thethis.name = this.fieldName
..
– PierreDuc
Nov 12 '18 at 11:54
add a comment |
my issue is that I have a MatInput as follows:
<input matInput [(ngModel)]="name" i18n-placeholder="@@Placeholder" placeholder="Your name" [formControl]="InputControl" value=" fieldName ">
In the correspoding component I declared fieldName: string;
and I am setting the value in the ngOnInit()
method:
ngOnInit()
this.fieldName = "VALUE"; // obtained from server so it may differ
if(this.fieldName.startsWith("PREFIX")) // if value starts with a given prefix I want to remove it
this.fieldName = this.fieldName.substr(3);
The thing is that it's actually working well (value gets displayed properly), but as soon as I want to submit the MatDialog form it tells me that I can't submit an empty form! Thats because I added a 'required' validator:
InputControl = new FormControl("", [Validators.required]);
So there is a value in the input field, but apparently Angular doesn't notice it? If I add a space to the value and delete it afterwards the value is exactly the same, but then it doesn't throw any errors.
Does anyone know how to fix this? It's really annoying.
javascript angular validation
my issue is that I have a MatInput as follows:
<input matInput [(ngModel)]="name" i18n-placeholder="@@Placeholder" placeholder="Your name" [formControl]="InputControl" value=" fieldName ">
In the correspoding component I declared fieldName: string;
and I am setting the value in the ngOnInit()
method:
ngOnInit()
this.fieldName = "VALUE"; // obtained from server so it may differ
if(this.fieldName.startsWith("PREFIX")) // if value starts with a given prefix I want to remove it
this.fieldName = this.fieldName.substr(3);
The thing is that it's actually working well (value gets displayed properly), but as soon as I want to submit the MatDialog form it tells me that I can't submit an empty form! Thats because I added a 'required' validator:
InputControl = new FormControl("", [Validators.required]);
So there is a value in the input field, but apparently Angular doesn't notice it? If I add a space to the value and delete it afterwards the value is exactly the same, but then it doesn't throw any errors.
Does anyone know how to fix this? It's really annoying.
javascript angular validation
javascript angular validation
asked Nov 12 '18 at 11:50
gv99gv99
428
428
You're usingngModel
but also talking aboutFormControl
. These are two conflicting form implementations:ngModel
two-way data binding can't be used withFormControl
and viceversa. Either you use theformControl
directive to bind the to thatFormControl
object, or you use therequired
attribute to make the input required without using FormControl. See also: What are the practical differences between template-driven and reactive forms?
– Badashi
Nov 12 '18 at 11:54
you are mixingngModel
and value and FormControl, are you sure you want to set value? Perhaps you want to set thename
of the inputname="fieldName"
, or you want to set a default value? Then you should update thethis.name = this.fieldName
..
– PierreDuc
Nov 12 '18 at 11:54
add a comment |
You're usingngModel
but also talking aboutFormControl
. These are two conflicting form implementations:ngModel
two-way data binding can't be used withFormControl
and viceversa. Either you use theformControl
directive to bind the to thatFormControl
object, or you use therequired
attribute to make the input required without using FormControl. See also: What are the practical differences between template-driven and reactive forms?
– Badashi
Nov 12 '18 at 11:54
you are mixingngModel
and value and FormControl, are you sure you want to set value? Perhaps you want to set thename
of the inputname="fieldName"
, or you want to set a default value? Then you should update thethis.name = this.fieldName
..
– PierreDuc
Nov 12 '18 at 11:54
You're using
ngModel
but also talking about FormControl
. These are two conflicting form implementations: ngModel
two-way data binding can't be used with FormControl
and viceversa. Either you use the formControl
directive to bind the to that FormControl
object, or you use the required
attribute to make the input required without using FormControl. See also: What are the practical differences between template-driven and reactive forms?– Badashi
Nov 12 '18 at 11:54
You're using
ngModel
but also talking about FormControl
. These are two conflicting form implementations: ngModel
two-way data binding can't be used with FormControl
and viceversa. Either you use the formControl
directive to bind the to that FormControl
object, or you use the required
attribute to make the input required without using FormControl. See also: What are the practical differences between template-driven and reactive forms?– Badashi
Nov 12 '18 at 11:54
you are mixing
ngModel
and value and FormControl, are you sure you want to set value? Perhaps you want to set the name
of the input name="fieldName"
, or you want to set a default value? Then you should update the this.name = this.fieldName
..– PierreDuc
Nov 12 '18 at 11:54
you are mixing
ngModel
and value and FormControl, are you sure you want to set value? Perhaps you want to set the name
of the input name="fieldName"
, or you want to set a default value? Then you should update the this.name = this.fieldName
..– PierreDuc
Nov 12 '18 at 11:54
add a comment |
1 Answer
1
active
oldest
votes
value
attributes are worthless in Angular.
If you want to give your control a value, do it through the control itself :
<input matInput [(ngModel)]="name" i18n-placeholder="@@Placeholder" placeholder="Your name" [formControl]="InputControl">
ngOnInit()
this.fieldName = "VALUE"; // obtained from server so it may differ
if(this.fieldName.startsWith("PREFIX"))
this.fieldName = this.fieldName.substr(3);
// w/ form control
this.InputControl.setValue(this.fieldName);
// w/ template driven
this.name = this.fieldName;
(Also, listen to comments on your questions and don't use bothformControl
&ngModel
)
– trichetriche
Nov 12 '18 at 12:23
I already tried that but then I got an ExpressionChangedAfterItHasBeenCheckedError exception.
– gv99
Nov 12 '18 at 13:39
Thanks anyways, I already fixed it, just had to say this.name (model name) instead of my first thought.
– gv99
Nov 12 '18 at 13:40
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%2f53261565%2fvalidators-required-throws-an-error-even-though-there-is-a-value-inside-of-it%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
value
attributes are worthless in Angular.
If you want to give your control a value, do it through the control itself :
<input matInput [(ngModel)]="name" i18n-placeholder="@@Placeholder" placeholder="Your name" [formControl]="InputControl">
ngOnInit()
this.fieldName = "VALUE"; // obtained from server so it may differ
if(this.fieldName.startsWith("PREFIX"))
this.fieldName = this.fieldName.substr(3);
// w/ form control
this.InputControl.setValue(this.fieldName);
// w/ template driven
this.name = this.fieldName;
(Also, listen to comments on your questions and don't use bothformControl
&ngModel
)
– trichetriche
Nov 12 '18 at 12:23
I already tried that but then I got an ExpressionChangedAfterItHasBeenCheckedError exception.
– gv99
Nov 12 '18 at 13:39
Thanks anyways, I already fixed it, just had to say this.name (model name) instead of my first thought.
– gv99
Nov 12 '18 at 13:40
add a comment |
value
attributes are worthless in Angular.
If you want to give your control a value, do it through the control itself :
<input matInput [(ngModel)]="name" i18n-placeholder="@@Placeholder" placeholder="Your name" [formControl]="InputControl">
ngOnInit()
this.fieldName = "VALUE"; // obtained from server so it may differ
if(this.fieldName.startsWith("PREFIX"))
this.fieldName = this.fieldName.substr(3);
// w/ form control
this.InputControl.setValue(this.fieldName);
// w/ template driven
this.name = this.fieldName;
(Also, listen to comments on your questions and don't use bothformControl
&ngModel
)
– trichetriche
Nov 12 '18 at 12:23
I already tried that but then I got an ExpressionChangedAfterItHasBeenCheckedError exception.
– gv99
Nov 12 '18 at 13:39
Thanks anyways, I already fixed it, just had to say this.name (model name) instead of my first thought.
– gv99
Nov 12 '18 at 13:40
add a comment |
value
attributes are worthless in Angular.
If you want to give your control a value, do it through the control itself :
<input matInput [(ngModel)]="name" i18n-placeholder="@@Placeholder" placeholder="Your name" [formControl]="InputControl">
ngOnInit()
this.fieldName = "VALUE"; // obtained from server so it may differ
if(this.fieldName.startsWith("PREFIX"))
this.fieldName = this.fieldName.substr(3);
// w/ form control
this.InputControl.setValue(this.fieldName);
// w/ template driven
this.name = this.fieldName;
value
attributes are worthless in Angular.
If you want to give your control a value, do it through the control itself :
<input matInput [(ngModel)]="name" i18n-placeholder="@@Placeholder" placeholder="Your name" [formControl]="InputControl">
ngOnInit()
this.fieldName = "VALUE"; // obtained from server so it may differ
if(this.fieldName.startsWith("PREFIX"))
this.fieldName = this.fieldName.substr(3);
// w/ form control
this.InputControl.setValue(this.fieldName);
// w/ template driven
this.name = this.fieldName;
answered Nov 12 '18 at 12:22
trichetrichetrichetriche
25.9k42152
25.9k42152
(Also, listen to comments on your questions and don't use bothformControl
&ngModel
)
– trichetriche
Nov 12 '18 at 12:23
I already tried that but then I got an ExpressionChangedAfterItHasBeenCheckedError exception.
– gv99
Nov 12 '18 at 13:39
Thanks anyways, I already fixed it, just had to say this.name (model name) instead of my first thought.
– gv99
Nov 12 '18 at 13:40
add a comment |
(Also, listen to comments on your questions and don't use bothformControl
&ngModel
)
– trichetriche
Nov 12 '18 at 12:23
I already tried that but then I got an ExpressionChangedAfterItHasBeenCheckedError exception.
– gv99
Nov 12 '18 at 13:39
Thanks anyways, I already fixed it, just had to say this.name (model name) instead of my first thought.
– gv99
Nov 12 '18 at 13:40
(Also, listen to comments on your questions and don't use both
formControl
& ngModel
)– trichetriche
Nov 12 '18 at 12:23
(Also, listen to comments on your questions and don't use both
formControl
& ngModel
)– trichetriche
Nov 12 '18 at 12:23
I already tried that but then I got an ExpressionChangedAfterItHasBeenCheckedError exception.
– gv99
Nov 12 '18 at 13:39
I already tried that but then I got an ExpressionChangedAfterItHasBeenCheckedError exception.
– gv99
Nov 12 '18 at 13:39
Thanks anyways, I already fixed it, just had to say this.name (model name) instead of my first thought.
– gv99
Nov 12 '18 at 13:40
Thanks anyways, I already fixed it, just had to say this.name (model name) instead of my first thought.
– gv99
Nov 12 '18 at 13:40
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%2f53261565%2fvalidators-required-throws-an-error-even-though-there-is-a-value-inside-of-it%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
You're using
ngModel
but also talking aboutFormControl
. These are two conflicting form implementations:ngModel
two-way data binding can't be used withFormControl
and viceversa. Either you use theformControl
directive to bind the to thatFormControl
object, or you use therequired
attribute to make the input required without using FormControl. See also: What are the practical differences between template-driven and reactive forms?– Badashi
Nov 12 '18 at 11:54
you are mixing
ngModel
and value and FormControl, are you sure you want to set value? Perhaps you want to set thename
of the inputname="fieldName"
, or you want to set a default value? Then you should update thethis.name = this.fieldName
..– PierreDuc
Nov 12 '18 at 11:54