Skip to content
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

Array items don't update properly when you delete an item other than the last one #1004

Closed
rclai opened this issue Jun 4, 2015 · 1 comment

Comments

@rclai
Copy link

rclai commented Jun 4, 2015

Schema:

MyCollection.attachSchema(new SimpleSchema({
  qtys: {
    type: [new SimpleSchema({
      qty: {
        type: Number,
        autoform: {
          defaultValue: 1
        }
      }
    })],
    min: 1,
    autoValue: function () {
      if (this.isInsert && ! this.isSet) {
        return [{
          qty: 1
        }];
      }
    },
    custom: function () {
      // Pay attention to the `length` and indexes of `this.value`
      console.log(this.value);
    }
  }
}));

Form:

{{#autoForm id="MyFormId" class="clearfix" collection="MyCollection" type="update" doc=this resetOnSuccess=false}}
  <ul class="list-inline">
    {{#afEachArrayItem name="qtys"}}
      <li>
        {{> afQuickField class="input-sm" name=this.current.qty}}
        <button style="{{#if afArrayFieldIsFirstVisible}}visibility:hidden{{/if}}" class="autoform-remove-item" data-autoform-field="qtys"><span class="glyphicon glyphicon-minus"></span></button>
      </li>
    {{/afEachArrayItem}}
    <li class="qtysItem">
      <button type="submit" class="btn btn-success"><span class="glyphicon glyphicon-ok-circle"></span> Save</button>
      <button class="btn btn-primary autoform-add-item" data-autoform-field="qtys"><span class="glyphicon glyphicon-plus"></span> Add</button>
    </li>
  </ul>
{{/autoForm}}

I noticed this when I was debugging a custom validation rule in the simple schema for the qtys field. Say I had three items, and I minused out the second item and hit save, it would break. I noticed that the this.value value in the custom validator showed an array with the remaining two items, but I noticed that their indexes were 0 and 2, and the length property showed 3, which was weird. So I had to _.compact(this.value), before I could do any custom validation or it would break on me.

And in order to fix this on the MongoDB operation side, I had to do a before.update collection hook where I would do:

MyCollection.before.update(function (userId, doc, fields, modifier, options) {
  // Do some checks for my desired field.. blah blah..  
  modifier.$set.qtys = _.compact(modifier.$set.qtys);
});

Because modifier.$set.qtys would be an array of three items, where the second item was a null value:

[
  {
    qty: 1
  },
  null, // here it is
  {
    qty: 3
  }
]
@stachrom
Copy link

duplicate of #914

TamarAmit added a commit to TamarAmit/meteor-autoform that referenced this issue Oct 25, 2015
When submiting a form after removing exisiting items from array of Strings, the modifier gets array with null values in the place of the deleted strings, This fix correct it so the modifier gets array with only the remaining fields with the correct length.
Might solve the issues Meteor-Community-Packages#1263, Meteor-Community-Packages#1049, Meteor-Community-Packages#1004
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants