Meteor-Community-Packages / meteor-autoform

AutoForm is a Meteor package that adds UI components and helpers to easily create basic forms with automatic insert and update events, and automatic reactive validation.
MIT License
1.44k stars 328 forks source link

Array of objects set last object fields to null when remove unordered element #1697

Open fashkl opened 3 years ago

fashkl commented 3 years ago

Hello Guys, I have an array of objects 'schema' which all fields are 'options', client has to select emp_id based on that bus, dpt, are automatically filter their values, the problem is when i remove unordered element "not last element" all the below elements fields 'bus, dpt' are being null, but if i remove last array element it is working fine,

please guys advice me how to fix that,

Thanks

Array's element schema

partSchema = new SimpleSchema({
        emp_id: {
            type: String,
            optional: false,
            autoform: {
                type: "select2",
                options: function () {
                    return Meteor.users.find({}).map(function (obj) {
                        return {label: obj.profile.firstname, value: obj._id};
                    });
                },
            }
        },
        bus: {
            optional: false,
            type: String,
            autoform: {
                options: function () {
                    let emp_id = AutoForm.getFieldValue(this.name.replace('bus', 'emp_id'));
                        return Bus.find({emp: emp_id}).map(function (obj) {
                            return {label: obj.bus, value: obj._id};
                        });
                },
            }
        },
        dpt: {
            optional: false,
            type: String,
            autoform: {
                options: function () {
                    let emp_id = AutoForm.getFieldValue(this.name.replace('dpt', 'emp_id'));
                        return Dpts.find({emp: emp_id}).map(function (obj) {
                            return {label: obj.dpt, value: obj._id};
                        })
                },
            }
        },
});

Main schema which am using the array

mainSchema = new SimpleSchema({
other fields...,
parts: {
        type: [partSchema],
        optional: false,
}
});

result

"parts": [
    {
      "emp_id": "emp_id1",
      "bus": "bsID1",
      "dpt": "dpID1",
    },
    {
      "emp_id": "emp_id2",
      "bus": "bsID2",
      "dpt": "dpID2",
    },
    {
      "emp_id": "emp_id3",
      "bus": "bsID3",
      "dpt": "dpID3"
    },
    {
      "emp_id": "emp_id4",
      "bus": "bsID4",
      "dpt": "dpID4"
    }
  ]
jankapunkt commented 3 years ago

Hi @fashkl

I really can't follow. Is this array used for autoform options?

fashkl commented 3 years ago

Hi @jankapunkt , thanks for your help,

I have edited description & attached schema code to be more clear,

jankapunkt commented 3 years ago

Thank you @fashkl could you also please add the Autoform version that you use and a short code example of your #autoForm or >quickForm Template so I can start reproducing the Error

fashkl commented 3 years ago

Thank you in advance @jankapunkt , Autoform version 5.8.1 and using quickForm, but after deep searching i got the problem that i'm using AutoForm.getFieldValue('emp_id') to set others fields, so when i remove element not last element for EX: index 0, AutoForm.getFieldValue('emp_id ' ==> 2) returns undefined, related to problem #833

jankapunkt commented 3 years ago

@fashkl is there something that prevents you from updating to 7.0.0? The issue should then be fixed.

jankapunkt commented 3 years ago

@fashkl friendly ping.