CSS problem with Angular Material and nested mat-form-field: inner mat-form-field in error state cause the...





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







2















I'm trying to deal with a big form creating a custom component for each section placing all the logic and custom validation in each custom component. Basically, each section is sub-form and each one contributes to the validation process of the big form.



I'm also using Angular Material and my custom components are custom form field control implementing the MatFormField<T> interface, in this way, I'll be able to use a nice look to show error and hint of a component as a whole.



For this reason, I'm wrapping my custom component inside <mat-form-field> and even the fields of the sub-form inside the custom component template are wrapped in a <mat-form-field>.



But I have a problem with this solution and Angular Material.
When a field in a sub-form is not valid, <mat-error> for the custom component is correctly displayed and even the <mat-error> for the inner form field is displayed.
The problem is also all the other fields is turning red like if they are invalid, but they're not actually!



Here it is an example:
https://stackblitz.com/edit/angular-2h2fql?embed=1&file=src/app/app.component.ts



each field is required, just try to clear one of them or add a new row and even all the other fields will turn in red.



I think It's just CSS stuff because when <mat-form-field> is invalid, the <mat-error> message class is showed up and a mat-form-field-invalid class is applied to host element <mat-form-field> which turn in red the invalid field.



My question is: do someone knows if it's possible to show up the <mat-error> manually or to set the custom form control in error state without applying the class .mat-form-field-invalid to the host element?



From my understanding, that class is bound to the error state as you can see here



https://github.com/angular/material2/blob/master/src/lib/form-field/form-field.ts#L120



but I can't find an elegant solution to overcome this problem.



I've tried to apply the ShadowDOM view encapsulation but it's not working fine.



I could simply not place my custom form control inside a <mat-form-field>, and it will work fine and the <mat-error> would be showed up, but not in a material design way.



Am I getting wrong or it's some Angular Material problem?



hope my explanation it's clear
thanks










share|improve this question

























  • What do you mean here "I could simply not place my custom form control inside a <mat-form-field>, and it will work fine and the <mat-error> would be showed up, but not in a material design way."

    – Maihan Nijat
    Nov 22 '18 at 0:55













  • I added an example in the stackblitz. If I don't wrap the custom component in a <mat-form-field> I don't have problems but the graphic is not well integrated

    – loru88
    Nov 22 '18 at 9:11




















2















I'm trying to deal with a big form creating a custom component for each section placing all the logic and custom validation in each custom component. Basically, each section is sub-form and each one contributes to the validation process of the big form.



I'm also using Angular Material and my custom components are custom form field control implementing the MatFormField<T> interface, in this way, I'll be able to use a nice look to show error and hint of a component as a whole.



For this reason, I'm wrapping my custom component inside <mat-form-field> and even the fields of the sub-form inside the custom component template are wrapped in a <mat-form-field>.



But I have a problem with this solution and Angular Material.
When a field in a sub-form is not valid, <mat-error> for the custom component is correctly displayed and even the <mat-error> for the inner form field is displayed.
The problem is also all the other fields is turning red like if they are invalid, but they're not actually!



Here it is an example:
https://stackblitz.com/edit/angular-2h2fql?embed=1&file=src/app/app.component.ts



each field is required, just try to clear one of them or add a new row and even all the other fields will turn in red.



I think It's just CSS stuff because when <mat-form-field> is invalid, the <mat-error> message class is showed up and a mat-form-field-invalid class is applied to host element <mat-form-field> which turn in red the invalid field.



My question is: do someone knows if it's possible to show up the <mat-error> manually or to set the custom form control in error state without applying the class .mat-form-field-invalid to the host element?



From my understanding, that class is bound to the error state as you can see here



https://github.com/angular/material2/blob/master/src/lib/form-field/form-field.ts#L120



but I can't find an elegant solution to overcome this problem.



I've tried to apply the ShadowDOM view encapsulation but it's not working fine.



I could simply not place my custom form control inside a <mat-form-field>, and it will work fine and the <mat-error> would be showed up, but not in a material design way.



Am I getting wrong or it's some Angular Material problem?



hope my explanation it's clear
thanks










share|improve this question

























  • What do you mean here "I could simply not place my custom form control inside a <mat-form-field>, and it will work fine and the <mat-error> would be showed up, but not in a material design way."

    – Maihan Nijat
    Nov 22 '18 at 0:55













  • I added an example in the stackblitz. If I don't wrap the custom component in a <mat-form-field> I don't have problems but the graphic is not well integrated

    – loru88
    Nov 22 '18 at 9:11
















2












2








2








I'm trying to deal with a big form creating a custom component for each section placing all the logic and custom validation in each custom component. Basically, each section is sub-form and each one contributes to the validation process of the big form.



I'm also using Angular Material and my custom components are custom form field control implementing the MatFormField<T> interface, in this way, I'll be able to use a nice look to show error and hint of a component as a whole.



For this reason, I'm wrapping my custom component inside <mat-form-field> and even the fields of the sub-form inside the custom component template are wrapped in a <mat-form-field>.



But I have a problem with this solution and Angular Material.
When a field in a sub-form is not valid, <mat-error> for the custom component is correctly displayed and even the <mat-error> for the inner form field is displayed.
The problem is also all the other fields is turning red like if they are invalid, but they're not actually!



Here it is an example:
https://stackblitz.com/edit/angular-2h2fql?embed=1&file=src/app/app.component.ts



each field is required, just try to clear one of them or add a new row and even all the other fields will turn in red.



I think It's just CSS stuff because when <mat-form-field> is invalid, the <mat-error> message class is showed up and a mat-form-field-invalid class is applied to host element <mat-form-field> which turn in red the invalid field.



My question is: do someone knows if it's possible to show up the <mat-error> manually or to set the custom form control in error state without applying the class .mat-form-field-invalid to the host element?



From my understanding, that class is bound to the error state as you can see here



https://github.com/angular/material2/blob/master/src/lib/form-field/form-field.ts#L120



but I can't find an elegant solution to overcome this problem.



I've tried to apply the ShadowDOM view encapsulation but it's not working fine.



I could simply not place my custom form control inside a <mat-form-field>, and it will work fine and the <mat-error> would be showed up, but not in a material design way.



Am I getting wrong or it's some Angular Material problem?



hope my explanation it's clear
thanks










share|improve this question
















I'm trying to deal with a big form creating a custom component for each section placing all the logic and custom validation in each custom component. Basically, each section is sub-form and each one contributes to the validation process of the big form.



I'm also using Angular Material and my custom components are custom form field control implementing the MatFormField<T> interface, in this way, I'll be able to use a nice look to show error and hint of a component as a whole.



For this reason, I'm wrapping my custom component inside <mat-form-field> and even the fields of the sub-form inside the custom component template are wrapped in a <mat-form-field>.



But I have a problem with this solution and Angular Material.
When a field in a sub-form is not valid, <mat-error> for the custom component is correctly displayed and even the <mat-error> for the inner form field is displayed.
The problem is also all the other fields is turning red like if they are invalid, but they're not actually!



Here it is an example:
https://stackblitz.com/edit/angular-2h2fql?embed=1&file=src/app/app.component.ts



each field is required, just try to clear one of them or add a new row and even all the other fields will turn in red.



I think It's just CSS stuff because when <mat-form-field> is invalid, the <mat-error> message class is showed up and a mat-form-field-invalid class is applied to host element <mat-form-field> which turn in red the invalid field.



My question is: do someone knows if it's possible to show up the <mat-error> manually or to set the custom form control in error state without applying the class .mat-form-field-invalid to the host element?



From my understanding, that class is bound to the error state as you can see here



https://github.com/angular/material2/blob/master/src/lib/form-field/form-field.ts#L120



but I can't find an elegant solution to overcome this problem.



I've tried to apply the ShadowDOM view encapsulation but it's not working fine.



I could simply not place my custom form control inside a <mat-form-field>, and it will work fine and the <mat-error> would be showed up, but not in a material design way.



Am I getting wrong or it's some Angular Material problem?



hope my explanation it's clear
thanks







angular angular-material






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 0:46







loru88

















asked Nov 22 '18 at 0:34









loru88loru88

327




327













  • What do you mean here "I could simply not place my custom form control inside a <mat-form-field>, and it will work fine and the <mat-error> would be showed up, but not in a material design way."

    – Maihan Nijat
    Nov 22 '18 at 0:55













  • I added an example in the stackblitz. If I don't wrap the custom component in a <mat-form-field> I don't have problems but the graphic is not well integrated

    – loru88
    Nov 22 '18 at 9:11





















  • What do you mean here "I could simply not place my custom form control inside a <mat-form-field>, and it will work fine and the <mat-error> would be showed up, but not in a material design way."

    – Maihan Nijat
    Nov 22 '18 at 0:55













  • I added an example in the stackblitz. If I don't wrap the custom component in a <mat-form-field> I don't have problems but the graphic is not well integrated

    – loru88
    Nov 22 '18 at 9:11



















What do you mean here "I could simply not place my custom form control inside a <mat-form-field>, and it will work fine and the <mat-error> would be showed up, but not in a material design way."

– Maihan Nijat
Nov 22 '18 at 0:55







What do you mean here "I could simply not place my custom form control inside a <mat-form-field>, and it will work fine and the <mat-error> would be showed up, but not in a material design way."

– Maihan Nijat
Nov 22 '18 at 0:55















I added an example in the stackblitz. If I don't wrap the custom component in a <mat-form-field> I don't have problems but the graphic is not well integrated

– loru88
Nov 22 '18 at 9:11







I added an example in the stackblitz. If I don't wrap the custom component in a <mat-form-field> I don't have problems but the graphic is not well integrated

– loru88
Nov 22 '18 at 9:11














1 Answer
1






active

oldest

votes


















0














I think you may be confused on the intended purpose of mat-form-field... it is important to note the singularity, mat-form-field versus mat-form-fields




In this document, "form field" refers to the wrapper component
<mat-form-field> and "form field control" refers to the component that
the <mat-form-field> is wrapping (e.g. the input, textarea, select,
etc.)




https://material.angular.io/components/form-field/overview





Although that description is open for interpretation... the design of the form field wrapper was intended to be a 1:1... meaning 1 wrapper, 1 control.




  • reviewing all of the stackblitz examples for form-field you will see
    this 1:1 standard.


https://material.angular.io/components/form-field/examples



By wrapping several mat-form-fields in a parent mat-form-field you are essentially turning them all into one big mat-form-field that is treating all controls as one control in terms of styling on errorState... if one is invalid, they all are invalid as it is one giant form-field.





Apply the following form-field wrapper in your people-list.component.html and you will see that the problem is replicated in your OUTSIDE MAT FORM FIELD example.



<div [formGroup]="form">
<mat-form-field>
<div formArrayName="rows" *ngFor="let p of rows.controls; let i=index">
<div [formGroupName]="i" fxLayout="row" fxLayoutAlign="space-around center">

<mat-form-field hintLabel="row number {{ i }}" class="full-width">
<input matInput type="text" formControlName="name"/>
<mat-hint align="end">Valid: {{ p.get('name').valid }}</mat-hint>
<mat-error *ngIf="p.get('name').hasError('required')">required</mat-error>
</mat-form-field>

<mat-form-field class="full-width">
<mat-select placeholder="Select state" formControlName="state">
<mat-option *ngFor="let state of stateList" [value]="state.abbreviation">
{{state.name}}
</mat-option>
</mat-select>
<mat-hint align="end">Valid: {{ p.get('state').valid }}</mat-hint>
<mat-error *ngIf="p.get('state').hasError('required')">required</mat-error>
</mat-form-field>

<button class="glyphicon glyphicon-remove pull-right" *ngIf="rows.length > 0"
(click)="removeStep(i)">Remove</button>
<br>
</div>
</div>
<br>
<button [disabled]="!rows.valid"
(click)="addStep()"
type="button"
color="primary"
*ngIf="rows.length < maxlength">
<span translate>ADD</span>
</button>
</mat-form-field>
</div>




You may want to explore replicating the form-field styling on error using something like below.



create mat-form-field-underline class in app.component.scss



.matFormFieldUnderline{
bottom: 1.25em;
background-color: rgba(0,0,0,.42);
height: 1px;
margin-top:5px;
}


Then use it in your HTML and replicate the mat-form-field style



<h2>OUTSIDE MAT FORM FIELD</h2>
<app-state-selector
name="people2"
[ngModel]="peopleList2"
#peopleRef2="ngModel"
required
maxlength=5
></app-state-selector>
<div class="matFormFieldUnderline" [style.background-color]="peopleRef2.errors ? 'red' : null"></div>
<div fxLayout>
<div fxFlex="70">
<mat-hint *ngIf="!peopleRef2.errors?.rows">Valid component: {{ peopleRef2.valid }}</mat-hint>
<mat-error *ngIf="peopleRef2.errors?.rows">Valid component: {{ peopleRef2.valid }}</mat-error>
</div>
<div fxFlex="30" fxLayoutAlign="end">
<mat-hint>{{peopleRef2.value?.length || 0}} rows</mat-hint>
</div>
</div>
<mat-error *ngIf="peopleRef2.errors?.required">required</mat-error>
<mat-error *ngIf="peopleRef2.errors?.maxlength">max 5 rows</mat-error>





share|improve this answer


























  • Thanks for your help. I'm trying to figure out how to fully mimic the mat-error, even with animation, using what provided with Angular Material. I would like to use some SCSS mixin or something else to let the style follow the currently applied theme but I think is not possible as of today state of art...

    – loru88
    Nov 25 '18 at 23:46












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%2f53422355%2fcss-problem-with-angular-material-and-nested-mat-form-field-inner-mat-form-fiel%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









0














I think you may be confused on the intended purpose of mat-form-field... it is important to note the singularity, mat-form-field versus mat-form-fields




In this document, "form field" refers to the wrapper component
<mat-form-field> and "form field control" refers to the component that
the <mat-form-field> is wrapping (e.g. the input, textarea, select,
etc.)




https://material.angular.io/components/form-field/overview





Although that description is open for interpretation... the design of the form field wrapper was intended to be a 1:1... meaning 1 wrapper, 1 control.




  • reviewing all of the stackblitz examples for form-field you will see
    this 1:1 standard.


https://material.angular.io/components/form-field/examples



By wrapping several mat-form-fields in a parent mat-form-field you are essentially turning them all into one big mat-form-field that is treating all controls as one control in terms of styling on errorState... if one is invalid, they all are invalid as it is one giant form-field.





Apply the following form-field wrapper in your people-list.component.html and you will see that the problem is replicated in your OUTSIDE MAT FORM FIELD example.



<div [formGroup]="form">
<mat-form-field>
<div formArrayName="rows" *ngFor="let p of rows.controls; let i=index">
<div [formGroupName]="i" fxLayout="row" fxLayoutAlign="space-around center">

<mat-form-field hintLabel="row number {{ i }}" class="full-width">
<input matInput type="text" formControlName="name"/>
<mat-hint align="end">Valid: {{ p.get('name').valid }}</mat-hint>
<mat-error *ngIf="p.get('name').hasError('required')">required</mat-error>
</mat-form-field>

<mat-form-field class="full-width">
<mat-select placeholder="Select state" formControlName="state">
<mat-option *ngFor="let state of stateList" [value]="state.abbreviation">
{{state.name}}
</mat-option>
</mat-select>
<mat-hint align="end">Valid: {{ p.get('state').valid }}</mat-hint>
<mat-error *ngIf="p.get('state').hasError('required')">required</mat-error>
</mat-form-field>

<button class="glyphicon glyphicon-remove pull-right" *ngIf="rows.length > 0"
(click)="removeStep(i)">Remove</button>
<br>
</div>
</div>
<br>
<button [disabled]="!rows.valid"
(click)="addStep()"
type="button"
color="primary"
*ngIf="rows.length < maxlength">
<span translate>ADD</span>
</button>
</mat-form-field>
</div>




You may want to explore replicating the form-field styling on error using something like below.



create mat-form-field-underline class in app.component.scss



.matFormFieldUnderline{
bottom: 1.25em;
background-color: rgba(0,0,0,.42);
height: 1px;
margin-top:5px;
}


Then use it in your HTML and replicate the mat-form-field style



<h2>OUTSIDE MAT FORM FIELD</h2>
<app-state-selector
name="people2"
[ngModel]="peopleList2"
#peopleRef2="ngModel"
required
maxlength=5
></app-state-selector>
<div class="matFormFieldUnderline" [style.background-color]="peopleRef2.errors ? 'red' : null"></div>
<div fxLayout>
<div fxFlex="70">
<mat-hint *ngIf="!peopleRef2.errors?.rows">Valid component: {{ peopleRef2.valid }}</mat-hint>
<mat-error *ngIf="peopleRef2.errors?.rows">Valid component: {{ peopleRef2.valid }}</mat-error>
</div>
<div fxFlex="30" fxLayoutAlign="end">
<mat-hint>{{peopleRef2.value?.length || 0}} rows</mat-hint>
</div>
</div>
<mat-error *ngIf="peopleRef2.errors?.required">required</mat-error>
<mat-error *ngIf="peopleRef2.errors?.maxlength">max 5 rows</mat-error>





share|improve this answer


























  • Thanks for your help. I'm trying to figure out how to fully mimic the mat-error, even with animation, using what provided with Angular Material. I would like to use some SCSS mixin or something else to let the style follow the currently applied theme but I think is not possible as of today state of art...

    – loru88
    Nov 25 '18 at 23:46
















0














I think you may be confused on the intended purpose of mat-form-field... it is important to note the singularity, mat-form-field versus mat-form-fields




In this document, "form field" refers to the wrapper component
<mat-form-field> and "form field control" refers to the component that
the <mat-form-field> is wrapping (e.g. the input, textarea, select,
etc.)




https://material.angular.io/components/form-field/overview





Although that description is open for interpretation... the design of the form field wrapper was intended to be a 1:1... meaning 1 wrapper, 1 control.




  • reviewing all of the stackblitz examples for form-field you will see
    this 1:1 standard.


https://material.angular.io/components/form-field/examples



By wrapping several mat-form-fields in a parent mat-form-field you are essentially turning them all into one big mat-form-field that is treating all controls as one control in terms of styling on errorState... if one is invalid, they all are invalid as it is one giant form-field.





Apply the following form-field wrapper in your people-list.component.html and you will see that the problem is replicated in your OUTSIDE MAT FORM FIELD example.



<div [formGroup]="form">
<mat-form-field>
<div formArrayName="rows" *ngFor="let p of rows.controls; let i=index">
<div [formGroupName]="i" fxLayout="row" fxLayoutAlign="space-around center">

<mat-form-field hintLabel="row number {{ i }}" class="full-width">
<input matInput type="text" formControlName="name"/>
<mat-hint align="end">Valid: {{ p.get('name').valid }}</mat-hint>
<mat-error *ngIf="p.get('name').hasError('required')">required</mat-error>
</mat-form-field>

<mat-form-field class="full-width">
<mat-select placeholder="Select state" formControlName="state">
<mat-option *ngFor="let state of stateList" [value]="state.abbreviation">
{{state.name}}
</mat-option>
</mat-select>
<mat-hint align="end">Valid: {{ p.get('state').valid }}</mat-hint>
<mat-error *ngIf="p.get('state').hasError('required')">required</mat-error>
</mat-form-field>

<button class="glyphicon glyphicon-remove pull-right" *ngIf="rows.length > 0"
(click)="removeStep(i)">Remove</button>
<br>
</div>
</div>
<br>
<button [disabled]="!rows.valid"
(click)="addStep()"
type="button"
color="primary"
*ngIf="rows.length < maxlength">
<span translate>ADD</span>
</button>
</mat-form-field>
</div>




You may want to explore replicating the form-field styling on error using something like below.



create mat-form-field-underline class in app.component.scss



.matFormFieldUnderline{
bottom: 1.25em;
background-color: rgba(0,0,0,.42);
height: 1px;
margin-top:5px;
}


Then use it in your HTML and replicate the mat-form-field style



<h2>OUTSIDE MAT FORM FIELD</h2>
<app-state-selector
name="people2"
[ngModel]="peopleList2"
#peopleRef2="ngModel"
required
maxlength=5
></app-state-selector>
<div class="matFormFieldUnderline" [style.background-color]="peopleRef2.errors ? 'red' : null"></div>
<div fxLayout>
<div fxFlex="70">
<mat-hint *ngIf="!peopleRef2.errors?.rows">Valid component: {{ peopleRef2.valid }}</mat-hint>
<mat-error *ngIf="peopleRef2.errors?.rows">Valid component: {{ peopleRef2.valid }}</mat-error>
</div>
<div fxFlex="30" fxLayoutAlign="end">
<mat-hint>{{peopleRef2.value?.length || 0}} rows</mat-hint>
</div>
</div>
<mat-error *ngIf="peopleRef2.errors?.required">required</mat-error>
<mat-error *ngIf="peopleRef2.errors?.maxlength">max 5 rows</mat-error>





share|improve this answer


























  • Thanks for your help. I'm trying to figure out how to fully mimic the mat-error, even with animation, using what provided with Angular Material. I would like to use some SCSS mixin or something else to let the style follow the currently applied theme but I think is not possible as of today state of art...

    – loru88
    Nov 25 '18 at 23:46














0












0








0







I think you may be confused on the intended purpose of mat-form-field... it is important to note the singularity, mat-form-field versus mat-form-fields




In this document, "form field" refers to the wrapper component
<mat-form-field> and "form field control" refers to the component that
the <mat-form-field> is wrapping (e.g. the input, textarea, select,
etc.)




https://material.angular.io/components/form-field/overview





Although that description is open for interpretation... the design of the form field wrapper was intended to be a 1:1... meaning 1 wrapper, 1 control.




  • reviewing all of the stackblitz examples for form-field you will see
    this 1:1 standard.


https://material.angular.io/components/form-field/examples



By wrapping several mat-form-fields in a parent mat-form-field you are essentially turning them all into one big mat-form-field that is treating all controls as one control in terms of styling on errorState... if one is invalid, they all are invalid as it is one giant form-field.





Apply the following form-field wrapper in your people-list.component.html and you will see that the problem is replicated in your OUTSIDE MAT FORM FIELD example.



<div [formGroup]="form">
<mat-form-field>
<div formArrayName="rows" *ngFor="let p of rows.controls; let i=index">
<div [formGroupName]="i" fxLayout="row" fxLayoutAlign="space-around center">

<mat-form-field hintLabel="row number {{ i }}" class="full-width">
<input matInput type="text" formControlName="name"/>
<mat-hint align="end">Valid: {{ p.get('name').valid }}</mat-hint>
<mat-error *ngIf="p.get('name').hasError('required')">required</mat-error>
</mat-form-field>

<mat-form-field class="full-width">
<mat-select placeholder="Select state" formControlName="state">
<mat-option *ngFor="let state of stateList" [value]="state.abbreviation">
{{state.name}}
</mat-option>
</mat-select>
<mat-hint align="end">Valid: {{ p.get('state').valid }}</mat-hint>
<mat-error *ngIf="p.get('state').hasError('required')">required</mat-error>
</mat-form-field>

<button class="glyphicon glyphicon-remove pull-right" *ngIf="rows.length > 0"
(click)="removeStep(i)">Remove</button>
<br>
</div>
</div>
<br>
<button [disabled]="!rows.valid"
(click)="addStep()"
type="button"
color="primary"
*ngIf="rows.length < maxlength">
<span translate>ADD</span>
</button>
</mat-form-field>
</div>




You may want to explore replicating the form-field styling on error using something like below.



create mat-form-field-underline class in app.component.scss



.matFormFieldUnderline{
bottom: 1.25em;
background-color: rgba(0,0,0,.42);
height: 1px;
margin-top:5px;
}


Then use it in your HTML and replicate the mat-form-field style



<h2>OUTSIDE MAT FORM FIELD</h2>
<app-state-selector
name="people2"
[ngModel]="peopleList2"
#peopleRef2="ngModel"
required
maxlength=5
></app-state-selector>
<div class="matFormFieldUnderline" [style.background-color]="peopleRef2.errors ? 'red' : null"></div>
<div fxLayout>
<div fxFlex="70">
<mat-hint *ngIf="!peopleRef2.errors?.rows">Valid component: {{ peopleRef2.valid }}</mat-hint>
<mat-error *ngIf="peopleRef2.errors?.rows">Valid component: {{ peopleRef2.valid }}</mat-error>
</div>
<div fxFlex="30" fxLayoutAlign="end">
<mat-hint>{{peopleRef2.value?.length || 0}} rows</mat-hint>
</div>
</div>
<mat-error *ngIf="peopleRef2.errors?.required">required</mat-error>
<mat-error *ngIf="peopleRef2.errors?.maxlength">max 5 rows</mat-error>





share|improve this answer















I think you may be confused on the intended purpose of mat-form-field... it is important to note the singularity, mat-form-field versus mat-form-fields




In this document, "form field" refers to the wrapper component
<mat-form-field> and "form field control" refers to the component that
the <mat-form-field> is wrapping (e.g. the input, textarea, select,
etc.)




https://material.angular.io/components/form-field/overview





Although that description is open for interpretation... the design of the form field wrapper was intended to be a 1:1... meaning 1 wrapper, 1 control.




  • reviewing all of the stackblitz examples for form-field you will see
    this 1:1 standard.


https://material.angular.io/components/form-field/examples



By wrapping several mat-form-fields in a parent mat-form-field you are essentially turning them all into one big mat-form-field that is treating all controls as one control in terms of styling on errorState... if one is invalid, they all are invalid as it is one giant form-field.





Apply the following form-field wrapper in your people-list.component.html and you will see that the problem is replicated in your OUTSIDE MAT FORM FIELD example.



<div [formGroup]="form">
<mat-form-field>
<div formArrayName="rows" *ngFor="let p of rows.controls; let i=index">
<div [formGroupName]="i" fxLayout="row" fxLayoutAlign="space-around center">

<mat-form-field hintLabel="row number {{ i }}" class="full-width">
<input matInput type="text" formControlName="name"/>
<mat-hint align="end">Valid: {{ p.get('name').valid }}</mat-hint>
<mat-error *ngIf="p.get('name').hasError('required')">required</mat-error>
</mat-form-field>

<mat-form-field class="full-width">
<mat-select placeholder="Select state" formControlName="state">
<mat-option *ngFor="let state of stateList" [value]="state.abbreviation">
{{state.name}}
</mat-option>
</mat-select>
<mat-hint align="end">Valid: {{ p.get('state').valid }}</mat-hint>
<mat-error *ngIf="p.get('state').hasError('required')">required</mat-error>
</mat-form-field>

<button class="glyphicon glyphicon-remove pull-right" *ngIf="rows.length > 0"
(click)="removeStep(i)">Remove</button>
<br>
</div>
</div>
<br>
<button [disabled]="!rows.valid"
(click)="addStep()"
type="button"
color="primary"
*ngIf="rows.length < maxlength">
<span translate>ADD</span>
</button>
</mat-form-field>
</div>




You may want to explore replicating the form-field styling on error using something like below.



create mat-form-field-underline class in app.component.scss



.matFormFieldUnderline{
bottom: 1.25em;
background-color: rgba(0,0,0,.42);
height: 1px;
margin-top:5px;
}


Then use it in your HTML and replicate the mat-form-field style



<h2>OUTSIDE MAT FORM FIELD</h2>
<app-state-selector
name="people2"
[ngModel]="peopleList2"
#peopleRef2="ngModel"
required
maxlength=5
></app-state-selector>
<div class="matFormFieldUnderline" [style.background-color]="peopleRef2.errors ? 'red' : null"></div>
<div fxLayout>
<div fxFlex="70">
<mat-hint *ngIf="!peopleRef2.errors?.rows">Valid component: {{ peopleRef2.valid }}</mat-hint>
<mat-error *ngIf="peopleRef2.errors?.rows">Valid component: {{ peopleRef2.valid }}</mat-error>
</div>
<div fxFlex="30" fxLayoutAlign="end">
<mat-hint>{{peopleRef2.value?.length || 0}} rows</mat-hint>
</div>
</div>
<mat-error *ngIf="peopleRef2.errors?.required">required</mat-error>
<mat-error *ngIf="peopleRef2.errors?.maxlength">max 5 rows</mat-error>






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 5:18

























answered Nov 23 '18 at 3:16









MarshalMarshal

3,8272622




3,8272622













  • Thanks for your help. I'm trying to figure out how to fully mimic the mat-error, even with animation, using what provided with Angular Material. I would like to use some SCSS mixin or something else to let the style follow the currently applied theme but I think is not possible as of today state of art...

    – loru88
    Nov 25 '18 at 23:46



















  • Thanks for your help. I'm trying to figure out how to fully mimic the mat-error, even with animation, using what provided with Angular Material. I would like to use some SCSS mixin or something else to let the style follow the currently applied theme but I think is not possible as of today state of art...

    – loru88
    Nov 25 '18 at 23:46

















Thanks for your help. I'm trying to figure out how to fully mimic the mat-error, even with animation, using what provided with Angular Material. I would like to use some SCSS mixin or something else to let the style follow the currently applied theme but I think is not possible as of today state of art...

– loru88
Nov 25 '18 at 23:46





Thanks for your help. I'm trying to figure out how to fully mimic the mat-error, even with animation, using what provided with Angular Material. I would like to use some SCSS mixin or something else to let the style follow the currently applied theme but I think is not possible as of today state of art...

– loru88
Nov 25 '18 at 23:46




















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%2f53422355%2fcss-problem-with-angular-material-and-nested-mat-form-field-inner-mat-form-fiel%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

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)