I am using vForm to post data to a laravel backend . the form data contain an array of objects that i modify using a dynamic input . i dont know exactly how many object i will send within the form thats why i used an array this is how vform object it looks like after adding some objects to the array
form : new vForm({
field1 : '',
field2 : '',
array : [
{
field1 : '' ,
field2 : '' ,
field3 : ''
},
{
field1 : '' ,
field2 : '' ,
field3 : ''
},
{
field1 : '' ,
field2 : '' ,
field3 : ''
}
]
})
my plan is simple , loop through the objects and create Models for them . the problem here is that laravel receives the data in different way , it handled each item of each object as an object itself ! and i ended up with 9 objects inside of array instead of 3 !
what vuejs sends
{
field1 : '',
field2 : '',
array : [
{
field1 : '' ,
field2 : '' ,
field3 : ''
},
{
field1 : '' ,
field2 : '' ,
field3 : ''
},
{
field1 : '' ,
field2 : '' ,
field3 : ''
}
]
}
what laravel receives (return $request)
{
field1 : '',
field2 : '',
array : [
{ field1 : '' },
{ field2 : '' },
{ field3 : '' },
{ field1 : '' },
{ field2 : '' },
{ field3 : '' },
{ field1 : '' },
{ field2 : '' },
{ field3 : '' }
]
}
my attempt to solve it
i searched for solution and i found out that you can modify the form data before sending it using transfer data inside vform , but it did not work for me so what i ended up doing was passing the array as a json string and decode it in the backend , i used
this.form.array = JSON.stringify(this.form.array) ;
$array = json_decode($request->array);
but i have to parse it after the post request so it wont mess up my dynamic inputs , since i am using vfor to loop through the array and stringify will change it to a string and i ll have a lot of fields caused by the loop ..
am i missing somthing ? is this a good way to solve it ? is there any other way to do it using vform ?
this.someOtherDataVariable = JSON.stringify(this.form.array) ;instead ofthis.form.array = JSON.stringify(this.form.array) ;and sendthis.someOtherDataVariableto formadata and server This will keep your v-for loop working on