Skip to content
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

Async Validator causes form control state to be set to valid when other validators are invalid #10074

Closed
buddyackerman opened this issue Jul 14, 2016 · 6 comments

Comments

@buddyackerman
Copy link

buddyackerman commented Jul 14, 2016

I'm not sure if this is a bug or not but the behavior doesn't make sense. I'm using a model driven form on my page and one of the controls has two synchronous validators (required and minlength) and a custom asyncValidator. When the page initially loads the form control is empty so it should be failing the required and minlength validations. The custom validator will return valid when the field is empty. However, the state of the control is being set Valid on form initialization. Previously my custom async validator had been a synchronous validator and it worked as expected, i.e. the custom validator is initially valid but the form control is still invalid due to the other validations being invalid.

The following plnkr illustrates the behavior:

http://plnkr.co/edit/VJlg1Q1EXs9mQ8Bx2YTQ?p=preview

Also, note that the after form initialization the async validator only runs after satisfying the minlength requirement (in this case upon typing the 6th character) while the sync validator runs on every key press and the sync validator is run twice on form initialization while the async validator only runs once. Another thing is that the form control with the async validator will report invalid if you type some characters then delete them all making the field empty.

@nick1247
Copy link

I think, the problem is that you are mixing templete driven and model driven forms. Please, remove required from html templete and put it to form builder:

this.registrationForm = this.formBuilder.group({
              UserId: ['', [Validators.minLength(6), Validators.required], [this.UserIdExistsAsyncValidator.bind(this)]],
              UserId2: ['', [Validators.minLength(6), Validators.required, this.UserIdExistsSyncValidator.bind(this)]],
        });

@buddyackerman
Copy link
Author

It does work when I remove the required attribute from the markup (which of course breaks my styling as it relies on the required attribute, i.e. .ng-valid[required]) but I don't see why that should make a difference between they types of validators being used. All the async validator does is add an item to the errors array on the control. It shouldn't make any difference if it's added immediately or after some delay.

@nick1247
Copy link

Async validators don't start if sync validators returned 'invalid' state. Rely on this
In your case, form has only minlength validator, that is valid if control value is empty, so async validator start working and return 'valid' state.

I don't know is mixing template and model validators a good path, but as you can see it doesn't work as expected.

I think all validation should be at one place, at html template or at form model.

@buddyackerman
Copy link
Author

The problem is that the async validator DID run on form initialization and the async validator's result was apparently the only result considered in the control's state even though required and minlength failed. I've removed the required attribute (and had to add my own dummy attribute to get my css to work) from the form controls but I don't see any reason that this shouldn't have worked in the first place.

@xban1x
Copy link

xban1x commented Nov 15, 2016

Possibly a duplicate of #12709?

DzmitryShylovich pushed a commit to DzmitryShylovich/angular that referenced this issue Jan 28, 2017
DzmitryShylovich pushed a commit to DzmitryShylovich/angular that referenced this issue Jan 29, 2017
mhevery pushed a commit that referenced this issue Feb 2, 2017
mhevery pushed a commit that referenced this issue Feb 3, 2017
asnowwolf pushed a commit to asnowwolf/angular that referenced this issue Aug 11, 2017
juleskremer pushed a commit to juleskremer/angular that referenced this issue Aug 28, 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 10, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants