Skip to content

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

Closed
wants to merge 1 commit into from
Closed

feat(forms): update validity on touched #13832

wants to merge 1 commit into from

Conversation

SystemDisc
Copy link

@SystemDisc SystemDisc commented Jan 7, 2017

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?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

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?

  • Yes
  • No

Other information:
Running validators onTouched (onBlur), allows a validators that only run once the user has moved on to the next input.

For example:

export function trim(control: AbstractControl): {[key: string]: any} {
	if (control.touched && String(control.value).match(/^\s+|\s+$/)) {
		control.setValue(String(control.value).replace(/^\s+|\s+$/, ''));
		control.markAsUntouched();
	}
	return null;
}

@DzmitryShylovich
Copy link
Contributor

To me it doesn't address the original issue

Angular2 Forms Controls validation "OnLostFocus (OnBlur)" rather than "OnChange"

in this case it will validate on both blur and change

@SystemDisc
Copy link
Author

SystemDisc commented Jan 7, 2017

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.

@SystemDisc
Copy link
Author

SystemDisc commented Jan 7, 2017

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.

@SystemDisc
Copy link
Author

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();

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?

Copy link
Author

@SystemDisc SystemDisc Jan 19, 2017

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.

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.

Copy link
Author

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

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.

@mhevery
Copy link
Contributor

mhevery commented Jan 28, 2017

Could we have some tests?

@SystemDisc
Copy link
Author

SystemDisc commented Jan 28, 2017

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.

@mhevery
Copy link
Contributor

mhevery commented Feb 10, 2017

@kara can you comment

@kara
Copy link
Contributor

kara commented Mar 9, 2017

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.

@kara kara closed this Mar 9, 2017
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 11, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Angular2 Forms Controls validation "OnLostFocus (OnBlur)" rather than "OnChange"
7 participants