DEV Community

Cover image for Form and Function: How I Lost My Submit Button & Got It Back
Nick Taylor Subscriber for OpenSauced

Posted on • Edited on • Originally published at opensauced.pizza

Form and Function: How I Lost My Submit Button & Got It Back

As web developers, we know that most of the create, read update, and delete (CRUD) actions we perform on the web are typically (hopefully?) done using an HTML form.

HTML Forms

HTML Forms are cool because they have plenty of built-in features.

For example, they have native form validation and access to all the inputs in a form, and at some point, because you need to submit the form, there is a mechanism to do that as well. You can use a button, <button>submit</button> or an input of type submit, <input type="submit" />, although the latter isn't used as much these days in new sites, from what I've seen.

Here is a simple form to exhibit this.

If you fill out the form and click submit, the form will submit and add a paragraph with dark green text client-side that says, "Form submitted".

Submitting the simple form

There are other things in the simple form, like form validation via the required attribute on inputs, but that's not what we're here to discuss.

What we want to touch on is that the form was able to handle the submit event because it had a submit button associated with it, which was defined in HTML within the form element.

Note: you can press enter in fields to submit a form, but again, not what we're here to discuss.

How I Broke My Form

This brings us to a new feature that I was working on for OpenSauced for a few months, workspaces. I encourage you to create your own, but for now, let's get back to the business of forms.

Here's our beautiful workspaces settings page that I implemented.

an OpenSauced workspace settings page

Recently, there were styling changes, which is what you see above.

fix: now workspace settings and new page have a fixed footer for CTAs #2982

Description

What type of PR is this? (check all applicable)

  • [ ] ๐Ÿ• Feature
  • [x] ๐Ÿ› Bug Fix
  • [ ] ๐Ÿ“ Documentation Update
  • [ ] ๐ŸŽจ Style
  • [ ] ๐Ÿง‘โ€๐Ÿ’ป Code Refactor
  • [ ] ๐Ÿ”ฅ Performance Improvements
  • [ ] โœ… Test
  • [ ] ๐Ÿค– Build
  • [ ] ๐Ÿ” CI
  • [ ] ๐Ÿ“ฆ Chore (Release)
  • [ ] โฉ Revert

Related Tickets & Documents

Fixes #2924

Mobile & Desktop Screenshots/Recordings

Before

CleanShot 2024-03-20 at 10 08 59

image

After

Settings Page

CleanShot 2024-03-20 at 10 07 25

CleanShot 2024-03-20 at 09 55 04

New Workspace Page

CleanShot 2024-03-20 at 09 56 01

Steps to QA

  1. Go to your workspace settings page, e.g. /workspaces/some-worspace-id/settings
  2. Scroll through the settings.
  3. Notice the Update workspace button remains in a fixed footer and content scrolls underneath it.
  4. Do the same thing for the create new workspace page, /workspaces/new, and the same behaviour occurs.

Added to documentation?

  • [ ] ๐Ÿ“œ README.md
  • [ ] ๐Ÿ““ docs.opensauced.pizza
  • [ ] ๐Ÿ• dev.to/opensauced
  • [ ] ๐Ÿ“• storybook
  • [x] ๐Ÿ™… no documentation needed

[optional] Are there any post-deployment tasks we need to perform?

[optional] What gif best describes this PR or how it makes you feel?

Everything looked great, and I had tested it.

Narrator: he thought he had tested it, and we shipped things to production.

Once things went live, I decided to do some smoke tests, which I usually do. I went over to the beautiful workspace settings I had worked on, made some changes in the settings, and then clicked Update Workspace button. Hmm, no toast message saying the settings were updated. I checked the browser dev tools to see if there were any JavaScript errors. Nothing related to the updates. And then it dawned on me. The submit button was outside the form, and I just broke some key functionality in the app.

Michael Scott telling everybody not to panic.

Side note, but luckily thanks to Netlify's deployment rollback feature, I was able to revert to the previous production deployment within about a minute of the workspace settings page being broken ๐Ÿ˜…

How I Fixed My Form

So how did I fix it? We needed this new styling to fix several other issues related to z-indexes and layout.

For some context, the OpenSauced application is a Next.js site, so React, but I decided to put on my old school HTML hat and remembered that form elements can be associated to a form via a form attribute. What you need to do is give the form an id attribute, and the form element that you want to associate the form to needs to have a form attribute whose value is the value of the id attribute for the form.

Here's another simple form demonstrating a simplified version of my fix.

I encourage you to remove the form attribute from the button in the above CodePen to see the issue I caused.

Here's the fix I rolled out to production.

fix: fixed a regression with workspaces not updating/creating #3003

Description

Fixes a regression by the submit button for creating and updating workspaces was no longer in the form. The fix leverages the button form attribute.

What type of PR is this? (check all applicable)

  • [ ] ๐Ÿ• Feature
  • [x] ๐Ÿ› Bug Fix
  • [ ] ๐Ÿ“ Documentation Update
  • [ ] ๐ŸŽจ Style
  • [ ] ๐Ÿง‘โ€๐Ÿ’ป Code Refactor
  • [ ] ๐Ÿ”ฅ Performance Improvements
  • [ ] โœ… Test
  • [ ] ๐Ÿค– Build
  • [ ] ๐Ÿ” CI
  • [ ] ๐Ÿ“ฆ Chore (Release)
  • [ ] โฉ Revert

Related Tickets & Documents

Relatest to #2982

Mobile & Desktop Screenshots/Recordings

Steps to QA

Create a workspace

  1. Go to /workspaces/new
  2. Enter a name, description optional.
  3. Click on the Create Workspace button.
  4. The workspace is created.

Update a workspace

  1. Go to /workspaces/some-workspace-id/settings.
  2. Update something about the workspace.
  3. Click the Update Workspace button.
  4. The workspace updates.

Added to documentation?

  • [ ] ๐Ÿ“œ README.md
  • [ ] ๐Ÿ““ docs.opensauced.pizza
  • [ ] ๐Ÿ• dev.to/opensauced
  • [ ] ๐Ÿ“• storybook
  • [x] ๐Ÿ™… no documentation needed

[optional] Are there any post-deployment tasks we need to perform?

[optional] What gif best describes this PR or how it makes you feel?

Wrapping Up

Learning a framework is great, and I'm a big proponent of just building something, but as you continue on in your career, it's great to start getting some fundamentals into the mix.

Also, this is a perfect example of why using semantic HTML is important! It definitely helped me get out of jam! ๐Ÿ˜…

Stay saucy peeps!

If you would like to know more about my work in open source, follow me on OpenSauced.

Top comments (2)

Collapse
 
michaeltharrington profile image
Michael Tharrington

Great post, Nick!

I've said it before, but I love reading posts where someone talks through their mistakes and then reveals the fix. Good stuff!

Collapse
 
nickytonline profile image
Nick Taylor

Thanks Michael!

Johny Utah giving a thumbs up