material icon indicating copy to clipboard operation
material copied to clipboard

md-input-container doesn't clear md-input-invalid class uppon setPristine

Open elincognito opened this issue 11 years ago • 7 comments

When using an md-input with an md-input-container setting the form or the field as pristine programmatically using $scope.formName.$setPristine() make md-input-container still keeps the md-input-invalid thereby still showing an underline error line

jsfiddle: https://jsfiddle.net/forcewill/cmdv648j/

Test by typing an invalid email and pressing add the input is underline in red

elincognito avatar Apr 23 '15 16:04 elincognito

One workaround could be use $touched in ng-if and use form.$setUntouched(). Updated fiddle

PrashantAshok avatar Jun 01 '15 22:06 PrashantAshok

UX question: wanted to confirm that it makes sense for the pristine state to hide/override the invalid state. It's possible for ng-invalid and ng-pristine to be set on the same element (e.g. when the user first loads a page that has an input[required] on it), so it's kind of a judgement call how we handle the interaction between these two states. I think it makes sense for pristine to clear the md-invalid-input as is requested in this issue, but would like UX guidance to make sure.

mhchen avatar Aug 18 '15 22:08 mhchen

If setting to pristine is meant to clear something for 'reuse' or to return it to its default, then this request is the best UX, even if this isn't specified in spec.

This can be inferred by the typical default case of a form that has yet to be filled out. If an element is required but has no value,and hasn't been touched yet, I wouldn't expect the invalid state to appear.

This seems like the desired default UX, but would it still be possible to set pristine and manually show invalid for cases where you want to both reset, and call attention to a previous error?

joeyperson avatar Sep 11 '15 17:09 joeyperson

Sorry for the delay in response. I looked into this @forcewill and I'm wondering if $setUntouched isn't what you're looking for instead. It will work for your use case and it makes sense conceptually too. Focusing and then blurring a required field doesn't make it dirty (i.e. not pristine), but it does make it touched. Only actually entering some input marks a field as dirty. Since by default I'd expect focusing and then blurring an input to trigger the invalid class, it seems like you're looking to set the inputs to untouched instead. Let me know if this doesn't work for your situation for some reason.

@joeyperson thanks for your input and please feel free to weigh in if you feel I'm mistaken.

mhchen avatar Oct 05 '15 06:10 mhchen

I realize after revisiting that this is essentially what @PrashantAshok said a long time ago only I don't think it's a workaround, I think it's the correct solution

mhchen avatar Oct 06 '15 15:10 mhchen

Not sure if this is relevant since the discussion isn't exactly in phase with the title, but since I got here because of the title, I might as well comment.

As demonstrated by previous posters, $setUntouched does properly remove the invalid state for inputs, when $setPristine doesn't. But it seems that the behavior is the exact opposite for selects - using $setUntouched leaves the field in its invalid state, whereas $setPristine clears it.

I forked the jsFiddle to demonstrate the issue : https://jsfiddle.net/qtpwhhss/1/

When you fill both fields and submit, the first form leaves the input invalid, and the second leaves the select invalid. On a side note, the required attribute on the md-select seems to be ignored until the validation of the second form, but this is a different matter.

pl-j avatar Oct 16 '15 12:10 pl-j

Is this issue resolved ?? @ThomasBurleson

narendrasinghrathore avatar Jul 21 '17 12:07 narendrasinghrathore