Fields become red before I start typing
The problem is that fields are validated before I start typing. When I open the page that consists all fields from screenshot, fields are already red.
I posted snippets of html and type script code because all fields look the same.
I will be very grateful for the help.
primaryFormGroup = new FormGroup({});
primaryFirstName = new FormControl('', [Validators.required]);
ngAfterViewInit(): void {
this.primaryFormGroup.addControl('primaryFirstName', this.primaryFirstName);
this.formGroupPersonalInfo.addControl('primaryFormGroup', this.primaryFormGroup);
}
Html
<div fxLayout="column" fxLayout.gt-sm="row" fxLayoutGap="10px">
<mat-form-field fxFlex="25">
<input matInput name="primaryfirstname" fuse-letters-only-format [formControl]="primaryFirstName" [attr.data-name]="sel_primary_firstname_input" [(ngModel)]="applicationService.application.primaryPersonalInfo.firstName"
placeholder="First name" maxlength="12" minlength="1" required (change)="personalInfoService.searchForWordTest($event)">
<mat-hint align="end">{{primaryFirstName.value?.length || 0}} / 12</mat-hint>
<mat-error *ngIf="primaryFirstName.hasError('required')">
<div [attr.data-name]="sel_primary_firstname_req_errmsg">First name field is required.</div>
<div fxLayoutAlign="end start" fxFlex>{{primaryFirstName.value?.length || 0}} / 12</div>
</mat-error>
</mat-form-field>
</div>
angular typescript angular5
add a comment |
The problem is that fields are validated before I start typing. When I open the page that consists all fields from screenshot, fields are already red.
I posted snippets of html and type script code because all fields look the same.
I will be very grateful for the help.
primaryFormGroup = new FormGroup({});
primaryFirstName = new FormControl('', [Validators.required]);
ngAfterViewInit(): void {
this.primaryFormGroup.addControl('primaryFirstName', this.primaryFirstName);
this.formGroupPersonalInfo.addControl('primaryFormGroup', this.primaryFormGroup);
}
Html
<div fxLayout="column" fxLayout.gt-sm="row" fxLayoutGap="10px">
<mat-form-field fxFlex="25">
<input matInput name="primaryfirstname" fuse-letters-only-format [formControl]="primaryFirstName" [attr.data-name]="sel_primary_firstname_input" [(ngModel)]="applicationService.application.primaryPersonalInfo.firstName"
placeholder="First name" maxlength="12" minlength="1" required (change)="personalInfoService.searchForWordTest($event)">
<mat-hint align="end">{{primaryFirstName.value?.length || 0}} / 12</mat-hint>
<mat-error *ngIf="primaryFirstName.hasError('required')">
<div [attr.data-name]="sel_primary_firstname_req_errmsg">First name field is required.</div>
<div fxLayoutAlign="end start" fxFlex>{{primaryFirstName.value?.length || 0}} / 12</div>
</mat-error>
</mat-form-field>
</div>
angular typescript angular5
1
why are you using[(ngModel)]
when you've usedformControl
?
– CruelEngine
Nov 22 '18 at 10:58
To be sure that doesn't happen change your *ngIf to: !primaryFirstName.pristine && primaryFirstName.hasError('required')
– rrd
Nov 22 '18 at 10:59
add a comment |
The problem is that fields are validated before I start typing. When I open the page that consists all fields from screenshot, fields are already red.
I posted snippets of html and type script code because all fields look the same.
I will be very grateful for the help.
primaryFormGroup = new FormGroup({});
primaryFirstName = new FormControl('', [Validators.required]);
ngAfterViewInit(): void {
this.primaryFormGroup.addControl('primaryFirstName', this.primaryFirstName);
this.formGroupPersonalInfo.addControl('primaryFormGroup', this.primaryFormGroup);
}
Html
<div fxLayout="column" fxLayout.gt-sm="row" fxLayoutGap="10px">
<mat-form-field fxFlex="25">
<input matInput name="primaryfirstname" fuse-letters-only-format [formControl]="primaryFirstName" [attr.data-name]="sel_primary_firstname_input" [(ngModel)]="applicationService.application.primaryPersonalInfo.firstName"
placeholder="First name" maxlength="12" minlength="1" required (change)="personalInfoService.searchForWordTest($event)">
<mat-hint align="end">{{primaryFirstName.value?.length || 0}} / 12</mat-hint>
<mat-error *ngIf="primaryFirstName.hasError('required')">
<div [attr.data-name]="sel_primary_firstname_req_errmsg">First name field is required.</div>
<div fxLayoutAlign="end start" fxFlex>{{primaryFirstName.value?.length || 0}} / 12</div>
</mat-error>
</mat-form-field>
</div>
angular typescript angular5
The problem is that fields are validated before I start typing. When I open the page that consists all fields from screenshot, fields are already red.
I posted snippets of html and type script code because all fields look the same.
I will be very grateful for the help.
primaryFormGroup = new FormGroup({});
primaryFirstName = new FormControl('', [Validators.required]);
ngAfterViewInit(): void {
this.primaryFormGroup.addControl('primaryFirstName', this.primaryFirstName);
this.formGroupPersonalInfo.addControl('primaryFormGroup', this.primaryFormGroup);
}
Html
<div fxLayout="column" fxLayout.gt-sm="row" fxLayoutGap="10px">
<mat-form-field fxFlex="25">
<input matInput name="primaryfirstname" fuse-letters-only-format [formControl]="primaryFirstName" [attr.data-name]="sel_primary_firstname_input" [(ngModel)]="applicationService.application.primaryPersonalInfo.firstName"
placeholder="First name" maxlength="12" minlength="1" required (change)="personalInfoService.searchForWordTest($event)">
<mat-hint align="end">{{primaryFirstName.value?.length || 0}} / 12</mat-hint>
<mat-error *ngIf="primaryFirstName.hasError('required')">
<div [attr.data-name]="sel_primary_firstname_req_errmsg">First name field is required.</div>
<div fxLayoutAlign="end start" fxFlex>{{primaryFirstName.value?.length || 0}} / 12</div>
</mat-error>
</mat-form-field>
</div>
primaryFormGroup = new FormGroup({});
primaryFirstName = new FormControl('', [Validators.required]);
ngAfterViewInit(): void {
this.primaryFormGroup.addControl('primaryFirstName', this.primaryFirstName);
this.formGroupPersonalInfo.addControl('primaryFormGroup', this.primaryFormGroup);
}
Html
<div fxLayout="column" fxLayout.gt-sm="row" fxLayoutGap="10px">
<mat-form-field fxFlex="25">
<input matInput name="primaryfirstname" fuse-letters-only-format [formControl]="primaryFirstName" [attr.data-name]="sel_primary_firstname_input" [(ngModel)]="applicationService.application.primaryPersonalInfo.firstName"
placeholder="First name" maxlength="12" minlength="1" required (change)="personalInfoService.searchForWordTest($event)">
<mat-hint align="end">{{primaryFirstName.value?.length || 0}} / 12</mat-hint>
<mat-error *ngIf="primaryFirstName.hasError('required')">
<div [attr.data-name]="sel_primary_firstname_req_errmsg">First name field is required.</div>
<div fxLayoutAlign="end start" fxFlex>{{primaryFirstName.value?.length || 0}} / 12</div>
</mat-error>
</mat-form-field>
</div>
primaryFormGroup = new FormGroup({});
primaryFirstName = new FormControl('', [Validators.required]);
ngAfterViewInit(): void {
this.primaryFormGroup.addControl('primaryFirstName', this.primaryFirstName);
this.formGroupPersonalInfo.addControl('primaryFormGroup', this.primaryFormGroup);
}
Html
<div fxLayout="column" fxLayout.gt-sm="row" fxLayoutGap="10px">
<mat-form-field fxFlex="25">
<input matInput name="primaryfirstname" fuse-letters-only-format [formControl]="primaryFirstName" [attr.data-name]="sel_primary_firstname_input" [(ngModel)]="applicationService.application.primaryPersonalInfo.firstName"
placeholder="First name" maxlength="12" minlength="1" required (change)="personalInfoService.searchForWordTest($event)">
<mat-hint align="end">{{primaryFirstName.value?.length || 0}} / 12</mat-hint>
<mat-error *ngIf="primaryFirstName.hasError('required')">
<div [attr.data-name]="sel_primary_firstname_req_errmsg">First name field is required.</div>
<div fxLayoutAlign="end start" fxFlex>{{primaryFirstName.value?.length || 0}} / 12</div>
</mat-error>
</mat-form-field>
</div>
angular typescript angular5
angular typescript angular5
asked Nov 22 '18 at 10:54
Andrii KolesnykAndrii Kolesnyk
286
286
1
why are you using[(ngModel)]
when you've usedformControl
?
– CruelEngine
Nov 22 '18 at 10:58
To be sure that doesn't happen change your *ngIf to: !primaryFirstName.pristine && primaryFirstName.hasError('required')
– rrd
Nov 22 '18 at 10:59
add a comment |
1
why are you using[(ngModel)]
when you've usedformControl
?
– CruelEngine
Nov 22 '18 at 10:58
To be sure that doesn't happen change your *ngIf to: !primaryFirstName.pristine && primaryFirstName.hasError('required')
– rrd
Nov 22 '18 at 10:59
1
1
why are you using
[(ngModel)]
when you've used formControl
?– CruelEngine
Nov 22 '18 at 10:58
why are you using
[(ngModel)]
when you've used formControl
?– CruelEngine
Nov 22 '18 at 10:58
To be sure that doesn't happen change your *ngIf to: !primaryFirstName.pristine && primaryFirstName.hasError('required')
– rrd
Nov 22 '18 at 10:59
To be sure that doesn't happen change your *ngIf to: !primaryFirstName.pristine && primaryFirstName.hasError('required')
– rrd
Nov 22 '18 at 10:59
add a comment |
1 Answer
1
active
oldest
votes
You should also check if it is touched before showing error.
<mat-error *ngIf="primaryFirstName.hasError('required') && primaryFirstName.touched">
It hides error message but fields are still highlighted in red color. prnt.sc/lljuot
– Andrii Kolesnyk
Nov 22 '18 at 11:22
Ok... u dont hv to useformcontrol
with[(ngModel)]
– bereket gebredingle
Nov 22 '18 at 11:30
i fixed it but it did not help
– Andrii Kolesnyk
Nov 22 '18 at 11:32
Good for u. Thanks.
– bereket gebredingle
Nov 22 '18 at 11:36
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%2f53429369%2ffields-become-red-before-i-start-typing%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
You should also check if it is touched before showing error.
<mat-error *ngIf="primaryFirstName.hasError('required') && primaryFirstName.touched">
It hides error message but fields are still highlighted in red color. prnt.sc/lljuot
– Andrii Kolesnyk
Nov 22 '18 at 11:22
Ok... u dont hv to useformcontrol
with[(ngModel)]
– bereket gebredingle
Nov 22 '18 at 11:30
i fixed it but it did not help
– Andrii Kolesnyk
Nov 22 '18 at 11:32
Good for u. Thanks.
– bereket gebredingle
Nov 22 '18 at 11:36
add a comment |
You should also check if it is touched before showing error.
<mat-error *ngIf="primaryFirstName.hasError('required') && primaryFirstName.touched">
It hides error message but fields are still highlighted in red color. prnt.sc/lljuot
– Andrii Kolesnyk
Nov 22 '18 at 11:22
Ok... u dont hv to useformcontrol
with[(ngModel)]
– bereket gebredingle
Nov 22 '18 at 11:30
i fixed it but it did not help
– Andrii Kolesnyk
Nov 22 '18 at 11:32
Good for u. Thanks.
– bereket gebredingle
Nov 22 '18 at 11:36
add a comment |
You should also check if it is touched before showing error.
<mat-error *ngIf="primaryFirstName.hasError('required') && primaryFirstName.touched">
You should also check if it is touched before showing error.
<mat-error *ngIf="primaryFirstName.hasError('required') && primaryFirstName.touched">
answered Nov 22 '18 at 11:16
bereket gebredinglebereket gebredingle
1,201819
1,201819
It hides error message but fields are still highlighted in red color. prnt.sc/lljuot
– Andrii Kolesnyk
Nov 22 '18 at 11:22
Ok... u dont hv to useformcontrol
with[(ngModel)]
– bereket gebredingle
Nov 22 '18 at 11:30
i fixed it but it did not help
– Andrii Kolesnyk
Nov 22 '18 at 11:32
Good for u. Thanks.
– bereket gebredingle
Nov 22 '18 at 11:36
add a comment |
It hides error message but fields are still highlighted in red color. prnt.sc/lljuot
– Andrii Kolesnyk
Nov 22 '18 at 11:22
Ok... u dont hv to useformcontrol
with[(ngModel)]
– bereket gebredingle
Nov 22 '18 at 11:30
i fixed it but it did not help
– Andrii Kolesnyk
Nov 22 '18 at 11:32
Good for u. Thanks.
– bereket gebredingle
Nov 22 '18 at 11:36
It hides error message but fields are still highlighted in red color. prnt.sc/lljuot
– Andrii Kolesnyk
Nov 22 '18 at 11:22
It hides error message but fields are still highlighted in red color. prnt.sc/lljuot
– Andrii Kolesnyk
Nov 22 '18 at 11:22
Ok... u dont hv to use
formcontrol
with [(ngModel)]
– bereket gebredingle
Nov 22 '18 at 11:30
Ok... u dont hv to use
formcontrol
with [(ngModel)]
– bereket gebredingle
Nov 22 '18 at 11:30
i fixed it but it did not help
– Andrii Kolesnyk
Nov 22 '18 at 11:32
i fixed it but it did not help
– Andrii Kolesnyk
Nov 22 '18 at 11:32
Good for u. Thanks.
– bereket gebredingle
Nov 22 '18 at 11:36
Good for u. Thanks.
– bereket gebredingle
Nov 22 '18 at 11:36
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%2f53429369%2ffields-become-red-before-i-start-typing%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
1
why are you using
[(ngModel)]
when you've usedformControl
?– CruelEngine
Nov 22 '18 at 10:58
To be sure that doesn't happen change your *ngIf to: !primaryFirstName.pristine && primaryFirstName.hasError('required')
– rrd
Nov 22 '18 at 10:59