Skip to main content
← All notes
Building

The contact form auto-formats unknown fields into the notification email

software

Every service on the site has its own intake form with different fields — audio asks for genre and track count, software asks for budget range and project type, accessibility asks for a compliance target. All five forms submit to the same /api/contact route. The route does not have a mapping of field names to display labels. It has a function called formatServiceFields that iterates over every key in the request body, skips the five standard keys — name, email, message, service, and website — and formats whatever remains. The formatting is two operations. First, underscores become spaces. Second, each word gets its first letter capitalised. A field called track_count becomes Track Count. A field called budget_range becomes Budget Range. A field called compliance_target becomes Compliance Target. The function does not know these fields exist. It processes them generically. If I add a new field called preferred_format to the audio service config, the next enquiry email will include Preferred Format followed by whatever the client selected. No API change, no email template update, no deployment beyond the config edit. The message field gets special treatment — it appears last, prefixed with a blank line and the label Message with a colon, separated from the structured fields above it. The rest of the fields appear in object iteration order, which in practice matches the order they appear in the form because JavaScript objects preserve insertion order for string keys. The subject line branches on whether the service is set. A service-specific enquiry gets a bracketed label — [Software Engineering] New enquiry from Sarah. A generic contact form submission without a service gets Contact from Sarah. The confirmation email to the enquirer also branches — Thanks for getting in touch about Software Engineering versus just Thanks for getting in touch. Two emails, one route, five services, and every new form field formats itself.

Comments coming soon

Sign in with TikTok to leave a comment. Coming soon.