Posting Objects, Arrays, and an Array of Objects in HTML Forms

In this lesson, we'll learn how we can use form field names to send an array of objects with our HTML form submission. We'll discuss pitfalls to watch out for and how we use AlpineJS to simplify index syncing within our field names.

Published
Jun 26, 24
Duration
26m 26s

Developer & dog lover. I teach AdonisJS, a full-featured Node.js framework, at Adocasts where I publish weekly lessons. Professionally, I work with JavaScript, .NET, and SQL Server.

Adocasts

Burlington, KY

Get the Code

Download or explore the source code for this lesson on GitHub

Repository

00:00 - Creating the Crew Member Form Fields
03:21 - Adding A Crew Member Button
03:56 - Moving Our Fields Into A Component and Template
06:16 - Array and Object Fields Names (Initial)
07:16 - Add Crew Member Click Handler Script
08:04 - Inspecting Our Form Submission Results
08:50 - Submitting With One Value (Object, Not Array)
09:19 - Submitting With Two or More Values (Object of Arrays)
09:46 - Using Indexes To Get An Array of Objects
12:54 - Validating Our Array of Objects
13:36 - Saving Our Crew Members with Pivot Table Data
15:53 - Testing Our Crew Member Save
16:21 - Querying Our Crew Members Directly from the Pivot Table
17:21 - Our Remaining Issue, Missing Indexes on Removal
18:01 - Adding AlpineJS
18:28 - Defining Our AlpineJS State
19:46 - Refactoring to use AlpineJS
25:32 - Testing Our AlpineJS Injected Indexes

Join The Discussion! (2 Comments)

Please sign in or sign up for free to join in on the dicussion.

  1. Commented 9 days ago

    Great lessons, but I had real trouble on this one. I'm new to AlpineJS but trying to add the same crew member twice completely broke it, with more issues when trying to "sync" on the backend.

    In the end, I changed the alpine key to use index instead of cineasts_id, then in Adonis removed the 'sync' method and simply deleted all crew_movies entries and mass inserted the new entries

    1

    Please sign in or sign up for free to reply

    1. Commented 7 days ago

      Terribly sorry about that drummersi-3! This module wasn't originally a part of this series, I added it on last minute and that really shows in this lesson! I'll have to re-watch this module and think through how to appropriately correct it. Again - apologies you had difficulty here, there's definitely room for improvement on this lesson.

      1

      Please sign in or sign up for free to reply