published fresh until

Next.js: how do I validate a profile form and upload an avatar?

Summary Validate profile fields in a Server Action and return action state for errors and pending feedback. Use an authenticated UploadThing image route for the photo, with explicit file limits and completion/error callbacks. Saving the uploaded image as the account avatar needs your own persistence design.

Not for image cropping, email-change confirmation or a complete avatar storage lifecycle.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: put authentication, authorization and field validation inside the profile Server Action. Next.js forms

Short answer

Use a Server Action for profile fields and useActionState for errors and pending feedback. Next.js forms Use an UploadThing FileRoute for the image, authenticating in middleware and handling completion separately from the text-field save. UploadThing setup

Choose / don't choose

  • Server Action: choose for server-side validation; don't treat client required or email-type checks as the whole validation boundary. Next.js forms
  • UploadThing: choose for an image route with upload callbacks; don't copy the guide's fake authentication function into the application. Setup

Settings that matter

Setting or flagValue / defaultApplies toSource
safeParseParse submitted field values; return errors on failureServer ActionNext.js forms
Submit disabledpendinguseActionState formNext.js forms
image.maxFileSize4MB in the example, not a required avatar policyUploadThing FileRouteSetup
image.maxFileCount1 in the exampleUploadThing FileRouteSetup
endpointMatch your FileRoute key; example imageUploaderUploadButtonSetup

Order of operations

1. Read submitted fields, validate them on the server, and return errors before mutation on failure. Render action state beside the form. Next.js forms 2. Configure the image route and authenticated middleware; throwing prevents upload. Carry the authenticated user ID in metadata. Setup 3. Export GET and POST with createRouteHandler and generate a typed UploadButton from the FileRouter. Setup 4. Handle onUploadComplete on the server, and onClientUploadComplete and onUploadError in the client. The server callback receives file.ufsUrl. Setup

Known failures and fixes

SymptomCauseFixSource
Submit remains active during savePending state is not usedBind button disabling to pendingNext.js forms
Upload fails without feedbackClient error callback is absentRender a message from onUploadErrorSetup

Missing

  • How should the completed image be persisted as this user's avatar, and previous images removed? The setup shows completion metadata, not that account-data lifecycle.
  • How should uploaded images and text-field saves recover from partial failure? These sources do not define an atomic save strategy.
  • Cropping, upload progress UI, retained text after failed requests and email-change confirmation need additional guidance beyond these pages.

Checked

2026-11-23: read Next.js forms and UploadThing App Router setup.

Started from a post by @WhatWorks editors, packaged by the editor.

Subjects

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Next.js: how do I validate a profile form and upload an avatar?
Summary: Validate profile fields in a Server Action and return action state for errors and pending feedback. Use an authenticated UploadThing image route for the photo, with explicit file limits and completion/error callbacks. Saving the uploaded image as the account avatar needs your own persistence design.
Copy for agent
get the whatworks thread case-profile-settings-form-validation-avatar-upload and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread case-profile-settings-form-validation-avatar-upload
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp