-
Notifications
You must be signed in to change notification settings - Fork 26.2k
feat(forms): update validity on touched #13832
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
Conversation
To me it doesn't address the original issue
in this case it will validate on both blur and change |
No, you're right. It only adds validation checking on blur. It doesn't replace validation checking on change. What would be an appropriate way to allow people to configure what triggers validation? I don't think it would be hard to add the option to have form validation run (1) on change, (2) on blur, or (3) on change and on blur. |
Perhaps the FormControl constructor could have an options parameter added, where you can specify something like: new FormControl('name', validator, asyncValidator, {validateEvent: 'blur'});
new FormControl('name', validator, asyncValidator, {validateEvent: 'change'});
new FormControl('name', validator, asyncValidator, {validateEvent: ['blur', 'change']}); or maybe just a validateEvent parameter: new FormControl('name', validator, asyncValidator, 'blur');
new FormControl('name', validator, asyncValidator, 'change');
new FormControl('name', validator, asyncValidator, ['blur', 'change']); I'm not exactly sure how that would be implemented, but I'm pretty sure I could figure it out. |
Once I get an update on this, I can make the necessary changes to close #7113 |
@@ -47,7 +47,10 @@ export function setUpControl(control: FormControl, dir: NgControl): void { | |||
}); | |||
|
|||
// touched | |||
dir.valueAccessor.registerOnTouched(() => control.markAsTouched()); | |||
dir.valueAccessor.registerOnTouched(() => { | |||
control.updateValueAndValidity(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the validation is already running on change, which will happen before the blur, will this ever produce a different value or validity than the control already has?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I understand what you mean. It seems you're implying that a change event gets fired on blur no matter what. This is not true.
If I'm understanding what you're getting at, the answer would be: No, the value that gets checked on blur would be the same value that got checked on the last change, but I think you can add if (control.touched)
in your ValidatorFn to have code that only runs if the control has been blurred. That code wouldn't run on blur without this change - only the next time the control value changed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I'm saying is running the validation on blur seems superfluous if it's already running on change, because the control would have the same value on blur that it did on the last change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unless you want your validator to run on blur and you check for something like control.touched
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yeah that's a good point. Probably was too early for me to replying to these things.
Could we have some tests? |
I'm waiting for a consensus to be achieved in #7113 before I correct this PR. Once that happens, I'd be more than happy to add some extra tests. |
@kara can you comment |
As it stands, we'd be adding another full validation run for everyone on both value change and on blur (regardless of if the value changed). Not everyone will want / need this extra run. The proper resolution for #7113 needs a lot more thought. Specifically, a design doc that covers more use cases. I think we should close this until this has gone through a design review. |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
closes #7113
Please check if the PR fulfills these requirements
I'm not sure if adding tests or changing the docs is necessary for this change, let me know.
What kind of change does this PR introduce?
What is the current behavior?
#7113
The current behavior is that validators are only called when form values change.
What is the new behavior?
Validators are called onTouched (onBlur).
Does this PR introduce a breaking change?
Other information:
Running validators onTouched (onBlur), allows a validators that only run once the user has moved on to the next input.
For example: