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.

Support 'submit' updateOn trigger for forms when using ng-model-options #7017

Closed

Description

@shahata

This seems important since when we have inputs that pend updates using ng-model-options we still want all changes to take place immediately if the form is submitted.

I'll have a PR ready with this probably this week.

This can be also very useful since ppl will no longer need to have shadow copies of objects while they let users edit an object in a form. The changes will be flushed only when the form is submitted if they use something like {updateOn: 'submit', validateOn: 'default'} (see #7016)

Activity

  1. lrlopez commented on Apr 6, 2014

    @lrlopez
    Contributor

    Hi Shahar,

    As we discussed yesterday (and also on the old pull request, you can force all changes to take place when submitting using the existing code.

    I.e.: If you want to debounce the changes but trigger an immediate update on blur or when submitting you could do this:
    { updateOn: 'submit default blur', debounce: {submit: 0, default: 500, blur: 0} }

    Another subject is decoupling the model update from validation as you suggest. This is a delicate issue because, until now, updates and validation take place at the same time. It's a nice idea though!

  2. lrlopez commented on Apr 6, 2014

    @lrlopez
    Contributor

    There is an ongoing discussion on this on #6740. We could continue the brainstorming there if you like.

  3. shahata commented on Apr 6, 2014

    @shahata
    ContributorAuthor

    @lrlopez - I tested {updateOn: 'submit'} in http://plnkr.co/edit/g0QxDS?p=preview and it doesn't work, but I'm not surprised since I don't believe the input fields get the submit event... We will need to either pass the submit button to the inputs on the DOM (which I'm not a fan of) or find some way around it (which I don't believe is difficult if we put our minds to it)

  4. lrlopez commented on Apr 6, 2014

    @lrlopez
    Contributor

    You're right. I've just confirmed that submit won't be emitted to input fields.

    At first sight we could use a simple solution: <form> will get event though so we could add the event to the form control instead of to the input.

    I.e.: Adding this to ngModelOptionsDirective:

            var SUBMIT_REGEXP = /(\b|^)submit(\b|$)/;
    
            // extract "submit" pseudo-event from list of events that can trigger a model update
            this.$options.updateOn = this.$options.updateOn.replace(SUBMIT_REGEXP, function() {
              that.$options.updateOnSubmit = true;
              return ' ';
            });
    

    On ngModelDirective:

          // Pass the form controller to the ng-model controller
          if ( formCtrl ) {
            modelCtrl.$form = ctrls[2].formCtrl;
          }
    

    And then, on the input directive:

      // Allow submit events on the parent form
      if (ctrl.$options && ctrl.$form && ctrl.$options.updateOnSubmit) {
        ctrl.$form.on('submit', listener);
      }
    

    Of course, assigning the form controller to the model controller is rough, but it's a first approximation 😄

    What do you think? It's monkey patching, but it could work.

  5. shahata commented on Apr 6, 2014

    @shahata
    ContributorAuthor

    Yes, this is more or less the direction I was going for. There are some edge cases to be considered here (for example, we want all the updating of the model to be done before ng-submit is executed, or probably even more complicated before ng-click of the submit button is executed), but I'm sure I'll get around those issues. Hopefully I will have something ready for review before the end of this week.

  6. added this to the milestone on Apr 7, 2014
  7. self-assigned this
    on Apr 7, 2014
  8. removed their assignment
    on Apr 7, 2014
  9. modified the milestones: 1.3.0-beta.8, on Apr 28, 2014
  10. dmackerman commented on Feb 3, 2015

    @dmackerman

    So, running into this now - but is there an easy to to just tell inputs to not validate until submit? I'm using ngMessages as well.

  11. thany commented on Mar 6, 2017

    @thany

    Running into this as well, 2 years later. Do we have a way to achieve this?

  12. Narretz commented on Mar 6, 2017

    @Narretz
    Contributor

    The original use-case is definitely possible. If you use ng-model-options="{updateOn: ''}" then the scope will only be updated when the form is submitted: http://plnkr.co/edit/LeZqYV?p=preview

    However, parsing and validation will also only happen then. This is a different issue that is tracked here: #7016 due to low demand, it hasn't been added.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions