Skip to content

Add guidance for styling invalid inputs #348

Closed

Description

@Pixcell

Note: for support questions, please use one of these channels:
https://github.com/angular/material2/blob/master/CONTRIBUTING.md#question.
This repository's issues are reserved for feature requests and bug reports.

  • Do you want to request a feature or report a bug?
    Feature
  • What is the current behavior?
    The only class available on md-input is .md-focused
  • If the current behavior is a bug,
    please provide steps to reproduce and if possible a minimal demo of the problem

    via https://plnkr.co or similar.
  • What is the expected behavior?
    It would be great if the classes of ngControl could be transfered to the md-input label and the md-input underline, in order to display live return on the validity of inputs
  • What is the motivation / use case for changing the behavior?

Better User experience

  • Which version of Angular and Material, and which browser and OS does this issue affect?
    Did this work in previous versions of Angular / Material?
    Please also test with the latest stable and snapshot versions.
  • Other information
    (e.g. detailed explanation, stacktraces, related issues, suggestions how to fix)

Activity

  1. changed the title [-]input validation system (ngControl support) (CSS)[/-] [+]Add guidance for styling invalid inputs[/+] on Apr 25, 2016
  2. added
    feature
    on Apr 25, 2016
  3. added this to the alpha.6 milestone on Apr 25, 2016
  4. cschroeter commented on May 18, 2016

    @cschroeter
    import {Directive, HostBinding} from '@angular/core';
    
    @Directive({
        selector: '[input-error]'
    })
    export class InputErrorDirective {
    
        @HostBinding('attr.dividerColor')
        get isValid() {
             // Checks..
             return 'warn';
        }
    }

    The @HostBinding or better the setAttribute() function is not case-sensitive. This results in

    <md-input [...] dividercolor="warn"></md-input> 
    

    and unfortunately this don't work.

    Any ideas?

  5. modified the milestones: alpha.7, alpha.6 on Jun 21, 2016
  6. leocaseiro commented on Oct 7, 2016

    @leocaseiro
    Contributor

    For the time being, I'm using a md-hint to show my error messages:

    <md-input>
        <md-hint *ngIf="error" [ngStyle]="{'color': 'red'}" align="start">{{errorMessage}}</md-hint>
    </md-input>

    It might help someone

  7. csabattilas commented on Oct 13, 2016

    @csabattilas

    Is there a class we can use linked to theming?

  8. leocaseiro commented on Oct 13, 2016

    @leocaseiro
    Contributor

    @krigton You can use the SASS md-color for theming,

    .unicorn-carousel {
        color: md-color($warn);
    }

    see more on the guide

  9. csabattilas commented on Oct 13, 2016

    @csabattilas

    so no class already there for errors? we can put what we want?

  10. leocaseiro commented on Oct 24, 2016

    @leocaseiro
    Contributor

    Hi @krigton, yep. My solution is temporary.

    Hope works for you.

    You can use any color from material or create your own.

    update from md to mat

    Example:

    @import '~@angular/material/core/theming/all-theme';
    @include mat-core();
    
    .red {
        color: mat-color($mat-red, 500, 1);
    }
    
    .warn {
       color: mat-color($warn);
    }
  11. modified the milestone: alpha.7 on Nov 1, 2016
  12. csabattilas commented on Nov 1, 2016

    @csabattilas
  13. 9 remaining items

  14. abqadeer commented on Apr 25, 2017

    @abqadeer

    How to divider color for md-select? I have got validations to work on inputs on submit but for select it works only if you have touched it once atleast and then click submit.

  15. tmburnell commented on May 2, 2017

    @tmburnell

    @abqadeer, can you give an example of how you got the validations on submit to work?
    I could not figure out how to set touched on elements on submit for it to work correctly.

    however i did the following for the error message colors:
    +input did not mark errors in red
    +select does not have a container, so not md-hint. So i created one that mocked it.

    md-input-container.ng-touched.ng-invalid {
    	/deep/ .mat-input-placeholder {
    		color: $error;
    	}
    
    	/deep/ .mat-input-underline {
    		border-top-width: 1px;
    		border-color: $error;
    	}
    
    	/deep/ .mat-input-ripple {
    		background-color: $error;
    	}
    
    	md-hint {
    		color: $error;
    		float: right;
    	}
    }
    
    div.select-container {
    	position: relative;
    
    	.md-hint {
    		color: $error;
    		padding-top: 2px;
    		font-size: 75%;
    		position: absolute;
    
    		right: 0;
    	}
    }
    
  16. tmburnell commented on May 2, 2017

    @tmburnell

    I just realized they they added md-error ... and the need for some of my css is no longer needed. However I still dont know how to make the input trigger in error when you have not touched it.

  17. coder925 commented on May 3, 2017

    @coder925

    @tmburnell , there is a workaround described in #4232 for allowing the md-error to show an error even if the control is not touched.

  18. CDDelta commented on May 3, 2017

    @CDDelta

    @tmburnell running .valid on the input form will also show the md-error on all invalid inputs. (*for reactive forms)

  19. onalbi commented on May 3, 2017

    @onalbi

    If this can help someone, md-error works fine with formControl, for example:

    [HTML]

              <fieldset [disabled]="isSubmitting">
                    <md-input-container class="full-width">
                        <md-error>{{tagField.errors ? tagField.errors.missedTag : null}}</md-error>
                        <input mdInput class="article-tags" type="text" placeholder="Enter tags"
                               [formControl]="tagField" (keyup.enter)="addTag()"/>
                        <md-chip-list>
                            <md-chip *ngFor="let tag of article.tags">
                                <md-icon (click)="removeTag(tag)">delete</md-icon>
                                <span>{{tag}}</span>
                            </md-chip>
                        </md-chip-list>
                        <md-hint>Press [enter] to add the tag</md-hint>
                    </md-input-container>
                </fieldset>
                <fieldset [disabled]="isSubmitting">
                    <button md-raised-button type="button" (click)="submitForm()" [disabled]="checkValidForm()">
                        <md-icon>publish</md-icon>
                        <span>Submit</span>
                    </button>
                </fieldset>

    [TS]

    this.fb.group({tagField: []});
    
    checkValidForm(): boolean {
            if (this.article.tags.length < 1) {
                this.tagField.setErrors({'missedTag': 'Please add at last one tag'});
            }
            return !this.articleForm.valid || this.article.tags.length < 1;
    }
  20. mmalerba commented on May 5, 2017

    @mmalerba
    Contributor

    Closing this since we now have <md-error> and a section in the input documentation on how to use it

  21. davorzdralo commented on Jun 30, 2017

    @davorzdralo

    @mmalerba - we do? The documentation on https://material.angular.io/components/input/overview is broken, I can't see example code and Plunkers don't do anything, and docs don't even try to explain how to have multiple messages.

  22. steve-todorov commented on Jul 3, 2017

    @steve-todorov

    The docs seems to be broken here as well. Was looking forward to experimenting with this feature, but will have to wait a while until everything is back to normal.

  23. atbe commented on Jul 3, 2017

    @atbe

    @davorzdralo Confirmed still broken on my end.

  24. willshowell commented on Jul 3, 2017

    @willshowell
    Contributor

    @davorzdralo @steve-todorov @atbe

    Dunno why that particular example is broken (cc @amcdnl), but the source is here:

    https://github.com/angular/material2/tree/master/src/material-examples/input-errors

    and here's the example in a plunker:

    https://plnkr.co/edit/mS8vyDKHLUhC9YOr84Aw?p=preview

  25. angular-automatic-lock-bot commented on Sep 6, 2019

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  26. locked and limited conversation to collaborators on Sep 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

docsfeature

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions