angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

More states for form validation

Open mhevery opened this issue 15 years ago • 46 comments

After reading this: http://www.alistapart.com/articles/inline-validation-in-web-forms/

In order to implement on blur, there needs to be more states to forms then just pristine and dirty. The gold standard should be to validate all the time, but updating the UI only when user navigates away from the widget.

mhevery avatar Oct 06 '11 23:10 mhevery

@2ix okay

daviesgeek avatar Apr 30 '14 18:04 daviesgeek

touched (blurred) is simple to implement https://gist.github.com/guzart/e4ecea64f509f9d5062d

guzart avatar May 28 '14 14:05 guzart

@guzart sorry I've been very busy refactoring the validators for ngModel. Could you put together a Pull Request for that and then I can review it? Remember to include the docs + tests.

matsko avatar May 28 '14 19:05 matsko

@matsko I'll take a stab at it this weekend

guzart avatar May 29 '14 23:05 guzart

Excellent.

Remember that $touched is true when a value is blurred and when the form has been submitted. Therefore $submitted for the FormController may also need to be created.

matsko avatar May 30 '14 00:05 matsko

@guzart @matsko, I've a pending pull request with an $attempted status. I didn't used the word $submitted cause the submision can be prevented due to validations. @IgorMinar asked for another name instead of $attempted, I believe he doesn't like the idea of $submitted. Check #5574

nicoabie avatar May 30 '14 02:05 nicoabie

@matsko I would've thought that $touched being true would be independent of the form being submitted. What's the thought behind this?

Personally I prefer $touched and $submitted separated, since I prefer to show the error messages after the user has left the field because I think it's unlikely they will come back to fix any error. If I need them to work together using a logical AND is simple enough to do, verbose... but simple.

guzart avatar Jun 02 '14 15:06 guzart

validation on blur would be very nice

coli avatar Aug 06 '14 15:08 coli

@coli that can be done using ngModelOptions.

matsko avatar Aug 06 '14 16:08 matsko

@matsko Thanks, learned something new today.

coli avatar Aug 06 '14 16:08 coli

Should a call to setPristine on NgModelController also set the controller to untouched? In RC1 it does not. So a reset function that calls setPristine and empties the model clears a form but validation error messages (required errors) still appear.

mdsauer avatar Sep 11 '14 14:09 mdsauer

@mdsauer the required error on empty inputs is always set, even if the form hasn't been touched yet. When a user leaves (blurs) the input, the ng-touched class is added to it and $touched is set to true. You can use this in your error display logic - only display when error is set and touched is true. When you reset the form, call $setUntouched on it.

Narretz avatar Sep 11 '14 19:09 Narretz

Thanks @Narretz . My initial thought was to call a $setUntouched method but I do not see that method on the form in 1.3 RC1. Is it being added?

mdsauer avatar Sep 12 '14 10:09 mdsauer

Oh, true. It's only available on the ngModelController. We should probably add it.

Narretz avatar Sep 12 '14 11:09 Narretz

For now, $touched is set to true on blur event. If user click on input then click elsewhere, $touched was set to true while there is no change.

Why not set $touched to true only if the value has been changed ?

var ngModelDirective = function() {
  // ... snip...
      post: function(scope, element, attr, ctrls) {
        // ... snip...

        var lastValue = element.val();

        element.on('blur', function(ev) {
          if (lastValue === element.val()) return;

          lastValue = element.val();

          if (modelCtrl.$touched) return;

          scope.$apply(function() {
            modelCtrl.$setTouched();
          });
        });
      }
};

marmotz avatar Sep 28 '14 18:09 marmotz

@marmotz: Isn't that what $dirty is (sort of) for ?

gkalpak avatar Sep 28 '14 18:09 gkalpak

Yes, you're right. But it seems strange to indicate that a field has been "touched" when it has not been changed, just focus and blur events. No?

At the beggining of this issue, someone said that show errors while user type is a bad thing. If I click by mistake on input (focus) then elsewhere (blur), $touched is set to true.

  • If I use $touched state, I immediately shows an error message. This is bad.
  • If I use $touched and $dirty state, I do not display an error. Good. BUT, the next time I'm updating this input, the error message appears immediately because $touched has already been set to true and $dirty will be on true, too.

You see what I mean?

marmotz avatar Sep 28 '14 19:09 marmotz

$touched was specifically implemented for the case that a user tabs through a form and leaves inputs invalid. It happens far more often that you blur an input and leave it invalid, then accidentially clicking an input, then blurring it. You can further control your error display behavior with the form field's $viewValue. Except for required errors, you could only display errors when the user has input something.

Narretz avatar Sep 28 '14 19:09 Narretz

Ok, so i don't understand the use case.

On the other hand, display an error while you just begin to type an email "Invalid email !!!" is a bad practice. So, ok, it seems that $touched is not the information I need, but I think this information is missing: "This field has been changed and the user has just left it, so now, do what you want as validation or displaying messages or anything else"

marmotz avatar Sep 28 '14 19:09 marmotz

It sounds like the condition you need is $touched && $dirty On Sep 28, 2014 10:23 PM, "Renaud LITTOLFF" [email protected] wrote:

Ok, so i don't understand the use case.

On the other hand, display an error while you just begin to type an email "Invalid email !!!" is a bad practice. So, ok, it seems that $touched is not the information I need, but I think this information is missing: "This field has been changed and the user has just left it, so now, do what you want as validation or displaying messages or anything else"

— Reply to this email directly or view it on GitHub https://github.com/angular/angular.js/issues/583#issuecomment-57096644.

shahata avatar Sep 29 '14 05:09 shahata