Skip to content

Provide Automatic Binding of FormControl Error Messages and Status to input hints #2810

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
arciisine opened this issue Jan 26, 2017 · 13 comments
Labels
area: material/input feature This issue represents a new feature or feature request rather than a bug or bug fix needs: discussion Further discussion with the team is needed before proceeding P4 A relatively minor issue that is not relevant to core functions

Comments

@arciisine
Copy link

Bug, feature request, or proposal:

This is a feature request.

This is related to #348, but is specifically asking for the material guidance to be a part of the framework, integrating with standard functionality of @angular/forms.

What is the expected behavior?

Looking at Material Error Patterns, there seems to be a standard behavior of inlining form error message into the hint area (with a specific color).

screen shot 2017-01-25 at 6 58 03 pm

It also supports separating the existing hint from the error message via a comma.

screen shot 2017-01-25 at 6 59 02 pm

What is the current behavior?

Error state and messages are not handled.

What are the steps to reproduce?

There is nothing to show as this is feature that is yet to be implemented. Material Error Patterns should provide the expected behavior and visual treatment.

What is the use-case or motivation for changing an existing behavior?

The general gist is that if everyone were to follow the guidelines from material.io, you would end up with a fair amount of duplicate code for every input. The pervasiveness of the desired functionality seems like a prime candidate for handling at the framework level.

Which versions of Angular, Material, OS, browsers are affected?

All

Is there anything else we should know?

@arciisine arciisine changed the title Provide Automatic Binding of ngModel Error Messages to mdInput hints Provide Automatic Binding of ngModel Error Messages to input hints Jan 26, 2017
@arciisine arciisine changed the title Provide Automatic Binding of ngModel Error Messages to input hints Provide Automatic Binding of FormControl Error Messages and Status to input hints Jan 26, 2017
@arciisine
Copy link
Author

I would be interested in opening a PR to support this, if the material team thinks this is a valid feature.

@jelbourn jelbourn added the feature This issue represents a new feature or feature request rather than a bug or bug fix label Jan 27, 2017
@arciisine
Copy link
Author

For anyone else looking at this, we have a very simple (not robust) implementation that should help along these lines. Currently its only setup for <md-input-container> but should be pretty easy to extrapolate.

import {
  Directive, ViewContainerRef, OnInit,
  HostListener, ContentChild, AfterViewInit, ContentChildren
} from '@angular/core';
import { NgControl, FormControl } from '@angular/forms';
import { MdInputDirective, MdInputContainer } from '@angular/material';

@Directive({
  selector: 'md-input-container'
})
export class ValidationDirective implements AfterViewInit {

  @ContentChild(MdInputDirective) mdInput;

  constructor(private container: MdInputContainer) { }

  ngAfterViewInit() {
    let ctrl = this.mdInput._ngControl as NgControl;
    if (ctrl === null) {
      // handles when field is not attached to form
      return;
    }
    let ogHint = this.container.hintLabel;
    let formCtrl = ctrl.control;
    // Not an easy way to see what validators are actually on a control, and so we assume if an
    //   empty input produced an error, the field is required
    if (formCtrl && formCtrl.validator) { 
      let res = formCtrl.validator(new FormControl());
      if (Object.keys(res).length > 0) {
        this.mdInput.required = true;
      }
    }
    ctrl.statusChanges.subscribe(() => {
      let messages = [ogHint];

      if (ctrl.invalid) {
        messages.push(...Object.keys(ctrl.errors)
          .map(x => ctrl.errors[x])
          .map(m => m.includes('required') ?
            'This field is required' :
            m));
      }

      let message = messages.filter(x => !!x).join(', ');
      this.container.hintLabel = message;
    });
  }
}

@arciisine
Copy link
Author

@kara @mmalerba I see there is a PR #3114 that address part of this issue. Is that to indicate that this ticket will also make it in at some future point, or is that still in the discussion phase?

@mmalerba
Copy link
Contributor

We still need to discuss the error messages part of it.

@iliketomatoes
Copy link

If we could have something like this: https://material.angularjs.org/latest/demo/input, it would be awesome.
Thanks!

@natsid
Copy link

natsid commented Mar 2, 2017

As the error message feature gets expanded to include other form controls (checkboxes, radio buttons, ...), would it be possible to implement a "group-level" error message, i.e., a message that applies to a group of associated form controls?

Select 'green' in this plunkr to see an example: http://plnkr.co/edit/HhN7ykzp0oM07IGyy4a3?p=preview

@jefersonestevo
Copy link
Contributor

I've developed a component to get and show the error messages from another component. It's pretty straighforward to use, you only need to provide the control (NgModel or FormControl) from the component to it and it will handle the messages. You can even customize the message you want to show for each erro by providing templates with a custom directive.

There are some pros and cons to this approach.

Pros:

  • You can easily customize the messages (By using a template for the specific error message)
  • The developer will not handle the input controls. The component will do that for you
  • It shows the message only when the user has touched the input
  • You can use it with any component that implements the form ControlValueAcessor (you may use it outside of the md-input-container) and both in template and reactive forms

Cons:

  • As it's another component, you'll have to find a place in your app to put it (probably below the input component itself, but it may change your layout)
  • The formControl does not have a "input touched" callback, so I had to "wrap" it so I could be notified when the user touched the element (suppose you have a required field, when the user open the page, it's already invalid but not touched. When the user click the component and leave, the error-messages component must be notified to show the error message)

You can see the demo here:
https://rawgit.com/jefersonestevo/angular-smd/master/dist/index.html#/angular-smd/demo-error-messages

Hopefully this component may help you with some ideas. I'm using it for now.

@mmalerba
Copy link
Contributor

fyi error messages part is in progress: #3560

@mmalerba
Copy link
Contributor

mmalerba commented May 5, 2017

Closing since this has been implemented via <md-error>

@mmalerba mmalerba closed this as completed May 5, 2017
@arciisine
Copy link
Author

@mmalerba I don't believe <md-error> provides any form of automatic binding of FormControl error messages. As I understand it, it is only used for specifying error messages one by one, but nothing automatic, which is the goal of this issue.

@mmalerba
Copy link
Contributor

mmalerba commented May 5, 2017

Ah sorry, will re-open this for discussion

@mmalerba mmalerba reopened this May 5, 2017
@ZachRoberts25
Copy link

it would be great to be able to dynamically add error messages as they come from HTTP requests, which is what I am thinking @arciisine is referring to.

To be able to set errors directly on the FormControl dynamically would allow for a lot less duplicated code.

@mmalerba mmalerba added the P4 A relatively minor issue that is not relevant to core functions label Nov 22, 2017
@jelbourn jelbourn added P5 The team acknowledges the request but does not plan to address it, it remains open for discussion and removed P4 A relatively minor issue that is not relevant to core functions labels Oct 5, 2018
@BenRacicot
Copy link

I submitted a feature request which Kara linked to 21823 that I think may be very similar but related to form success messages. Basically the idea comes down to being able to CRUD statusChanges.

I hope it's ok to reference that here.

@mmalerba mmalerba added needs: discussion Further discussion with the team is needed before proceeding and removed discussion labels Mar 3, 2020
@jelbourn jelbourn added P4 A relatively minor issue that is not relevant to core functions and removed P5 The team acknowledges the request but does not plan to address it, it remains open for discussion labels Jan 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: material/input feature This issue represents a new feature or feature request rather than a bug or bug fix needs: discussion Further discussion with the team is needed before proceeding P4 A relatively minor issue that is not relevant to core functions
Projects
None yet
Development

No branches or pull requests

10 participants