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

Add option to write invalid $modelValues to scope #8290

Closed

Description

@Narretz

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

Activity

  1. added this to the milestone on Jul 22, 2014
  2. shahata commented on Jul 22, 2014

    @shahata
    Contributor

    I'll take a stab at this

  3. leefernandes commented on Jul 23, 2014

    @leefernandes
    Contributor

    +1

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

  4. pocesar commented on Jul 25, 2014

    @pocesar
    Contributor

    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
  5. leefernandes commented on Jul 26, 2014

    @leefernandes
    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

  6. petebacondarwin commented on Jul 26, 2014

    @petebacondarwin
    Contributor

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

  7. leefernandes commented on Jul 26, 2014

    @leefernandes
    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

  8. added 2 commits that reference this issue on Sep 9, 2014
    d7bfae2
    3c538c1
  9. added a commit that references this issue on Sep 10, 2014
    525e7ac
  10. added a commit that references this issue on Sep 10, 2014
    a3d7934
  11. added 2 commits that reference this issue on Sep 10, 2014
    e4e63a3
    eeddb1d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions