Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

docs(ngModel): $setViewValue does not check validity before updating model #3498

Closed
wants to merge 1 commit into from
Closed

docs(ngModel): $setViewValue does not check validity before updating model #3498

wants to merge 1 commit into from

Conversation

niallsmart
Copy link
Contributor

Removes an incorrect assertion in the docs for $setViewValue. It actually updates the model regardless of the control validity.

…model.

Removes an incorrect assertion in the docs for $setViewValue. It actually
updates the model regardless of the control validity.
@mary-poppins
Copy link

Thanks for the PR!

  • Contributor signed CLA now or in the past
    • If you just signed, leave a comment here with your real name
  • PR's commit messages follow the commit message format

If you need to make changes to your pull request, you can update the commit with git commit --amend.
Then, update the pull request with git push -f.

Thanks again for your help!

IgorMinar added a commit that referenced this pull request Aug 9, 2013
@IgorMinar IgorMinar closed this in 43997c1 Aug 9, 2013
@IgorMinar
Copy link
Contributor

thanks but this change wasn't quite correct either. I clarified the docs in a separate commit.

thanks again.

@niallsmart
Copy link
Contributor Author

@IgorMinar Good call on the longer comment. However I think the sentence "if validators determine the value is invalid, the $modelValue and the model path will be set to undefined" is a bit misleading. Unless I'm misunderstanding the code, $setViewValue will use whatever value is returned by this.$parsers, regardless of whether they mark the value as invalid.

So, if you implement a custom validator, then just setting the control validity is not enough, you must also return undefined – $setViewValue will not do that for you.

@petebacondarwin
Copy link
Contributor

@niallsmart - I agree that the model will be set to whatever the value is returned by the last $parser in the pipeline. As it turns out, all the built-in validation directives "do" set this return to undefined if they are invalid. @IgorMinar - We should update the docs, again.

@niallsmart
Copy link
Contributor Author

@petebacondarwin the pull request linked to above clarifies this I think, which Igor merged earlier today.

@rinatio
Copy link

rinatio commented Jul 18, 2014

Hey guys!

I'm a little bit confused, what is an idea behind setting $modelValue to the undefined if validation fails? In case we have a watcher for this value in a controller and ngModel becomes invalid and then back valid with the same value the watcher will be triggered again and again. Here's an example to illustrate my question. Please advise how to handle such cases?

@petebacondarwin
Copy link
Contributor

@rinatio - The idea is that the model should only contain valid values or undefined. If you make the $viewValue invalid then the validation/parsers will pass undefined to the model. This is a convention. It would be possible to not change the model if the view becomes invalid but then you would have the potential for confusion and inconsistency between the view and the model. At least this way you can choose not to react if the model becomes undefined or to store your own copy of the valid model:

$scope.$watch('model', function(newVal, oldVal) {
  if ( newVal !== undefined ) {
    $scope.validModel = newVal;
  }
});

Then if you watch this validModel property it will only change when the model goes from one valid value to another.

@rinatio
Copy link

rinatio commented Jul 19, 2014

@petebacondarwin thanks, it makes sense. Maybe it is possible to ask explicitly ngModelController to use only last valid value? Like having some additional option in ng-model-options?

@petebacondarwin
Copy link
Contributor

I would be interested to see your use-case, as in general people have not had this issue before.

@rinatio
Copy link

rinatio commented Jul 19, 2014

@petebacondarwin it is pretty simple. Every time input's value got changed, we send a new ajax request. In case ngModel becomes invalid and then back valid with same value (as in example) the request is being sent again. Setting ng-model-options="{ updateOn: 'blur'}" will probably help me in my case, although there might be other use-cases. Anyway, just wondering

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants