Closed st-clair-clarke closed 8 years ago
MDL is wonderful and complicated. You just need to know a bit more of how it works.
Here are the pertinent stanzas of material.css.
.mdl-textfield__error {
color: rgb(222, 50, 38);
position: absolute;
font-size: 12px;
margin-top: 3px;
visibility: hidden;
display: block; }
.mdl-textfield.is-invalid .mdl-textfield__error {
visibility: visible; }
Note in the first stanza that class .mdl-textfield__error
has visibility:hidden
by default.
But, in the second stanza, if an element with .mdl-textfield__error
is inside an element with .mdl-textfield.is-invalid
, then visibility:visible
is set.
So, in your template, something like the following might work. You might even get rid of this new div and put the div's attribute assignment into the field's containing div.
<div [class.mdl-textfield.is-invalid]="!firstCtrl.valid">
<span class="mdl-textfield__error"
*ngIf = "!firstCtrl.valid">{{firstCtrlErrorMsg}}</span>
</div>
By the way, I hadn't seen a comprehensive use of Angular2's form capabilities before. Nice!
And I haven't actually tested the solution above; I was just working from first principles. But it should be close.
Hi Jim, I have tried your proposed solution but the error is still not being displayed. I am trying some other methods myself. In the mean time, do take a second look at it for me.
Thanks.
New version coming up. Most of this text is "how I got there".
Oof. I missed a dot in
.mdl-textfield.is-invalid .mdl-textfield__error {
visibility: visible; }
Upon further investigation, it seems that MaterialTextfield has its own way of determining validity, using HTML5 properties of the Input tag, so, with Angular, if you try to set [class.is-invalid]="!firstCtrl.valid"
on the div with .mdl-textfield
, it gets removed at run-time as the widget itself finds no problem with the input by its validation method.
A work-around would appear to be an additional style. The Angular form adds an ng-invalid
class to the input tag while the input is invalid. We can use that.
.mdl-textfield__input.ng-invalid ~ .mdl-textfield__error {visibility: visible; }
seems to work. You can add that to your stylesheet, or include it as
styles: const [
'.mdl-textfield__input.ng-invalid ~ .mdl-textfield__error {visibility: visible; }'
]
in your component metadata.
But I have a better idea. I'll fix MaterialTextfield to look at the input element for .ng-invalid
to set the proper classes so you don't have to do this. New version 0.0.5 shortly.
Thanks for the fix Jim. Everything is working fine now.
st clair
:thumbsup:
Hi Jim, I have the following Dart angular2 files:
name_component.dart
name_view.html
When the application is run the error message is not displayed.
Remove the class="mdl-textfielderror" from <span class="mdl-textfielderror" *ngIf = "!firstCtrl.valid">{{firstCtrlErrorMsg}} now shows the error message
Please let me know what you think is causing this problem. Cheers