Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

eggplantSword's avatar

Array getting rewritten but aren't sure why

Can't figure out why a variable is getting reset in my code, I'll try and put it simply but it's quite long and complicated really. What I'm doing is creating a page that is for creating and updating a survey with charts, now the survey form and the charts form are on separate components and transferring the data across is resulting a nightmare. I'm going to post the full methods just in case I missed something dumb. I'm only going to remove the method code that has to do with a new form. since my problem is only when I edit.

This is a 2 step 'form' where step 1 is the survey's info and step 2 are the charts info. You can go between the steps as much as you want but going backward is giving this issue.

Parent component has the child form and child charts components, the issue I'm having is with a specific array inside a variable.

The parent component has a created where I set up the data accordingly if it's a new form or if I'm updating an existing survey. As far as I understand this created is working correctly and it does set the info correctly on the first load.

Moving between the steps works like this in case its relevant

 goBack(e) {
            this.$set(this.form, 'survey_charts', e);
            this.changeStep(1);
        },
        moveTo(e) {
            if (this.validFirstSection(e[0])) {
				//take first_section and join with rest of sections, and re-number them 
                this.changeStep(e[2]);

                let survey_sections_fixed = [e[0]].concat(e[1].survey_sections);
                survey_sections_fixed.map((x, i) => x.number = i + 1);

                this.form = e[1];
                this.form.survey_sections = survey_sections_fixed;

                this.modelType = e[0].hasOwnProperty('id') ?
                    this.form.survey_sections.find(x => x.number === 1).survey_questions[0].survey_question_options[0].choiceable.label :
                    this.form.survey_sections.find(x => x.number === 1).survey_questions[0].survey_question_options[0].choice.value;
                this.modelName = e[0].hasOwnProperty('id') ?
                    this.form.survey_sections.find(x => x.number === 1).survey_questions[1].survey_question_options[0].choiceable.label :
                    this.form.survey_sections.find(x => x.number === 1).survey_questions[1].survey_question_options[0].choice.value;
            }
        },

The created in the child form component gets started again and idk why it resets my data for this.form.restriction_groups, the super weird part is that I'm not actually doing anything with that array in the child created method, the created method for the parent only gets called once so but I'm not modifying that array in any other place.

I'm not touching this.form.restriction_groups in the child component created at all.

Here is what's actually happening to the data from step 1 -> step 2

 [ { "key": "497b0558-c5f8-4f6f-9e1b-60ed1606d7c3", "id": "9f0faba7-cf0e-4d5b-b712-8ebefad19934", "name": "wewer", "is_repeated": false, "is_mandatory": true, "can_edit": false, "can_clone": false, "restrictions": [ { "restrictable_type": "App\\Models\\Tenant\\User", "id": "9f0faba7-d1da-4543-bf8e-18017b6fed34", "restrictable_id": "99229e6a-ae1b-4830-a68c-404550f0d655", "restrictable": { "id": "99229e6a-ae1b-4830-a68c-404550f0d655", "uuid": "46eb5e2c-14f2-4d60-ba0d-e9cd2c660dc8", "name": "Cristian Solís Montero", "email": "[email protected]", "is_active": 1, "created_at": "2023-05-10T23:27:37.000000Z", "updated_at": "2023-05-10T23:27:37.000000Z", "can_export_db": false }, "restrictable_uuid": null } ] } ]

from step 2 -> step 1

[ { "key": "497b0558-c5f8-4f6f-9e1b-60ed1606d7c3", "id": "9f0faba7-cf0e-4d5b-b712-8ebefad19934", "name": "wewer", "is_repeated": false, "is_mandatory": true, "can_edit": false, "can_clone": false, "restrictions": [ { "restrictable_type": "App\\Models\\Tenant\\User", "restrictable_uuid": null } ] } ]

I'm pretty lost on what is happening here, any ideas would be great

0 likes
0 replies

Please or to participate in this conversation.