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

Add option to write invalid $modelValues to scope #8290

Closed
Narretz opened this issue Jul 22, 2014 · 6 comments
Closed

Add option to write invalid $modelValues to scope #8290

Narretz opened this issue Jul 22, 2014 · 6 comments

Comments

@Narretz
Copy link
Contributor

Narretz commented Jul 22, 2014

Since the validation refactoring, theoretically all invalid models appear in the input even if invalid. It's however not possible to write an invalid $modelValue to the scope. The $validators now actively set $modelValue to undefined when it is invalid, meaning that users cannot use the previous workarounds for this behavior. They now only have $$invalidModelValue to get the raw value, but that is undocumented and quasi-private. Additionally, with $validators, it is not possible anymore to use a $parser to set the model even if invalid (another common workaround)

I propose we use ng-model-options to make it possible to write the parsed $modelValue to scope even if invalid.

Why is this needed?

  1. Server-side validation with using $setValidity manually is broken (see $setValidity somehow breaks model binding in 1.3.0 Beta 12 and after #8080 (comment))
  2. Keeping input regardless of validity (see input not showing invalid model values #1412 (comment))
  3. Displaying invalid models (from DB etc.): It currently displays the invalid model in the input, but at the same time resets the model to undefined (see http://plnkr.co/edit/Fapn7ePUhajjGNsjwIlg?p=preview)
  4. All common workarounds for this are broken in 1.3

/cc @matsko @shahata @petebacondarwin @caitp

@Narretz Narretz added this to the 1.3.0 milestone Jul 22, 2014
@shahata
Copy link
Contributor

shahata commented Jul 22, 2014

I'll take a stab at this

@leefernandes
Copy link
Contributor

+1

logic using invalidated $modelValues is broken: http://codepen.io/ItsLeeOwen/pen/BcjDI

@pocesar
Copy link
Contributor

pocesar commented Jul 25, 2014

just happened, I'm validating stuff server side, and when the user resubmit the form, it's gone, then the input becomes marked with required (since no data was sent to the server), even if I reset the validity state

  • I'm using required, minlength and "name" (custom validation state).
  • I set name manually using $setValidity from the server.
  • The field becomes $error = {required: false, minlength:false, name: true}
  • On model update on $watch, I reset name to false
  • $modelValue is now undefined on form submission
  • Server returns required: true, name: true, becomes an "endless client-server" loop

@leefernandes
Copy link
Contributor

Why can't or won't angular 1.3.x return to the days when invalid $modelValues were not undefined?

http://codepen.io/ItsLeeOwen/pen/DuJhg

@petebacondarwin
Copy link
Contributor

@itsleeowen - this was never the case with builtin validators: http://codepen.io/anon/pen/jykGi

@leefernandes
Copy link
Contributor

@petebacondarwin I don't know whether the change in behavior has anything to do with validators, I suspect a change to ngModel.$setValidity because in this comparison between 1.2.x - 1.3.x I'm not using $validators, and only end up with undefined $modelValues in the 1.3.x branch.

1.2.21: http://codepen.io/ItsLeeOwen/pen/EfwKC

1.3.x http://codepen.io/ItsLeeOwen/pen/CwALx

@btford btford removed the gh: issue label Aug 20, 2014
tbosch pushed a commit to tbosch/angular.js that referenced this issue Sep 9, 2014
This option allows to write invalid values to the model instead of having them become undefined.
Use this together with calling `ctrl.$setValidity` directly for displaying errors
from serverside validation.

Closes angular#8290
Closes angular#8313
@tbosch tbosch closed this as completed in 3c538c1 Sep 9, 2014
mgallag pushed a commit to mgallag/angular.js that referenced this issue Sep 10, 2014
This option allows to write invalid values to the model instead of having them become undefined.
Use this together with calling `ctrl.$setValidity` directly for displaying errors
from serverside validation.

Closes angular#8290
Closes angular#8313
tbosch pushed a commit that referenced this issue Sep 10, 2014
This option allows to write invalid values to the model instead of having them become undefined.
Use this together with calling `ctrl.$setValidity` directly for displaying errors
from serverside validation.

Closes #8290
Closes #8313
Closes #9016
mgallag pushed a commit to mgallag/angular.js that referenced this issue Sep 10, 2014
This option allows to write invalid values to the model instead of having them become undefined.
Use this together with calling `ctrl.$setValidity` directly for displaying errors
from serverside validation.

Closes angular#8290
Closes angular#8313
mgallag pushed a commit to mgallag/angular.js that referenced this issue Sep 10, 2014
This option allows to write invalid values to the model instead of having them become undefined.
Use this together with calling `ctrl.$setValidity` directly for displaying errors
from serverside validation.

Closes angular#8290
Closes angular#8313
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants