-
Notifications
You must be signed in to change notification settings - Fork 25.7k
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
Comments
I think, the problem is that you are mixing templete driven and model driven forms. Please, remove
|
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. |
Async validators don't start if sync validators returned 'invalid' state. Rely on this 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. |
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. |
Possibly a duplicate of #12709? |
…has changed Fixes angular#12709 Fixes angular#9120 Fixes angular#10074 Fixes angular#8923
…has changed Fixes angular#12709 Fixes angular#9120 Fixes angular#10074 Fixes angular#8923
…has changed (angular#13222) Fixes angular#12709 Fixes angular#9120 Fixes angular#10074 Fixes angular#8923 PR Close angular#13222
…has changed (angular#13222) Fixes angular#12709 Fixes angular#9120 Fixes angular#10074 Fixes angular#8923 PR Close angular#13222
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. |
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:
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.
The text was updated successfully, but these errors were encountered: