Topic: Only the final delivery address reaches our submit handler
owlStack77
free
asked 1 week ago
Users can add several delivery address rows to an order form. Every street input has name="street", and each visible row contains a different value, but the JSON request includes only the last street.
Our handler builds FormData and then passes it to Object.fromEntries before JSON.stringify. The inputs are enabled and they all belong to the form. What should change if the endpoint expects an array of addresses?
Igor Przybysz
staff
answered 1 week ago
FormData can contain repeated names, but a plain object's property can hold only one value for a given key. Object.fromEntries therefore replaces earlier street entries. For a single repeated field, FormData.getAll('street') returns all its values.
For complete address objects, collect each address row separately so that its street, city and postal code stay associated. Give the inputs unique IDs for labels while using consistent names within each row. Submit the resulting addresses array according to your endpoint's contract.
const addresses = Array.from(form.querySelectorAll('[data-address-row]'), row => ({
street: row.querySelector('[name="street"]').value.trim(),
city: row.querySelector('[name="city"]').value.trim(),
postalCode: row.querySelector('[name="postalCode"]').value.trim()
}));
const payload = { addresses };
https://mdbootstrap.com/docs/standard/forms/input-fields/
FREE CONSULTATION
Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.
Answered
- ForumUser: Free
- Premium support: No
- Technology: MDB Standard
- MDB Version: MDB5 9.3.0
- Device: Laptop
- Browser: Firefox
- OS: Ubuntu
- Provided sample code: No
- Provided link: No