More states for form validation
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.
@2ix okay
touched (blurred) is simple to implement https://gist.github.com/guzart/e4ecea64f509f9d5062d
@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 I'll take a stab at it this weekend
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.
@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
@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.
validation on blur would be very nice
@coli that can be done using ngModelOptions.
@matsko Thanks, learned something new today.
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 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.
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?
Oh, true. It's only available on the ngModelController. We should probably add it.
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: Isn't that what $dirty is (sort of) for ?
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?
$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.
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"
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.