Public: anyone can read this; the crew can change it.

A form that fails validation keeps what was typed and puts the error beside its field #13194

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

A refused POST now re-renders with the typed values kept (escaped), the message beside its field and focus on the first field that has one. Secrets are never echoed. The new test passes, along with four existing account suites (23 tests), and tsc, the gate and design-lint are clean.

Notes

opened this

A refused POST now re-renders with the typed values kept (escaped), the message beside its field and focus on the first field that has one. Secrets are never echoed. The new test passes, along with four existing account suites (23 tests), and tsc, the gate and design-lint are clean. I did not run the full suite; scripts/fast.ts found the diff too broad to name tests, so I picked the account suites myself.

What changed

  • src/web/form.ts gets refused(form, errors) and kept(refused, fieldOptions).
    • kept returns the field's options with the typed value restored (text, textarea, select, checkbox), the field's message as error, and autofocus on the first field in errors.
    • A field is never echoed if it is type="password" or its name matches pass|secret|token|credential|key|otp|pin.
    • With no refusal, kept returns the options untouched, so pages that don't use it are unaffected.
  • Two existing forms use it, with no change to routing or the other pages:
    • Profile (POST /settings): a refused submit keeps display name and email and puts the error on the right field.
    • Password change (POST /settings/password): a refused submit now focuses the field with the error and echoes none of the three passwords.
  • test/form-keeps-input.test.ts covers the helper (kept, focus, secret by type and by name, no refusal) and both forms end to end: kept values, a <script> display name escaped, the error beside its field, and no password in the response.

Left out

  • Every other form in the app. They adopt kept by passing their fields through it, one line each.
  • Fields in the same form that have no error of their own, such as the commit-email select, still use the page's existing value; only fields passed through kept are re-filled.
  • The name-based secret rule is a heuristic. A secret field with an unusual name must be typed password.

Try it: open /settings/password on the preview and submit different new and confirm passwords. The message sits beside "New password", that field has focus, and all three fields are empty. On /settings, enter a <script> display name and an invalid email: the display name comes back as inert text and the email field shows the error.

The commits carry No look: and No door: lines because the refused state is a 422 from a bad submit, not a page the looks list can open, and no page was added.

Closes #13165

4 earlier comments
commented

A refused POST now re-renders with the typed values kept (escaped), the message beside its field and focus on the first field that has one. Secrets are never echoed. The new test passes, along with four existing account suites (23 tests), and tsc, the gate and design-lint are clean. I did not run the full suite; scripts/fast.ts found the diff too broad to name tests, so I picked the account suites myself.

What changed

  • src/web/form.ts gets refused(form, errors) and kept(refused, fieldOptions).
    • kept returns the field's options with the typed value restored (text, textarea, select, checkbox), the field's message as error, and autofocus on the first field in errors.
    • A field is never echoed if it is type="password" or its name matches pass|secret|token|credential|key|otp|pin.
    • With no refusal, kept returns the options untouched, so pages that don't use it are unaffected.
  • Two existing forms use it, with no change to routing or the other pages:
    • Profile (POST /settings): a refused submit keeps display name and email and puts the error on the right field.
    • Password change (POST /settings/password): a refused submit now focuses the field with the error and echoes none of the three passwords.
  • test/form-keeps-input.test.ts covers the helper (kept, focus, secret by type and by name, no refusal) and both forms end to end: kept values, a <script> display name escaped, the error beside its field, and no password in the response.

Left out

  • Every other form in the app. They adopt kept by passing their fields through it, one line each.
  • Fields in the same form that have no error of their own, such as the commit-email select, still use the page's existing value; only fields passed through kept are re-filled.
  • The name-based secret rule is a heuristic. A secret field with an unusual name must be typed password.

Try it: open /settings/password on the preview and submit different new and confirm passwords. The message sits beside "New password", that field has focus, and all three fields are empty. On /settings, enter a <script> display name and an invalid email: the display name comes back as inert text and the email field shows the error.

The commits carry No look: and No door: lines because the refused state is a 422 from a bad submit, not a page the looks list can open, and no page was added.

commented

The comments are wrapped at 100 columns gate failure is fixed and pushed; the forge has not yet re-run its check on the new head.

The gate only failed once main was merged in, so I merged origin/main and ran bun scripts/gate.ts --fix. It rewrapped comments in src/web/pages/account.ts: 18 lines added, 6 removed, no code touched. After that bun scripts/gate.ts passes and bun x tsc --noEmit is clean. I also ran test/account-refusals.test.ts and test/account-nojs.test.ts, which cover this change, and all 8 tests pass.

I did not run the whole suite here. scripts/fast.ts reported the diff as too broad to name tests, so I picked the two files above by hand. A first attempt ran a bare bun test by mistake; I killed it.

Nothing about the form-validation behaviour changed in this push.

commented

All four review points are addressed in one push (52513ba). The gate, tsc --noEmit and test/form-keeps-input.test.ts, test/account-refusals.test.ts and test/account-nojs.test.ts pass (18 tests). I did not run the full suite; the forge's check does.

  1. Doc and terminal. docs/features/form-keeps-input.md is added; nothing goes in the README. The terminal has no HTML forms and no profile or password screen. A refused write there shows its reason as a danger toast (ctx.toast(errorText(...), "danger") in src/tui/forge.ts) and leaves the screen as it was, so nothing changes in the terminal.
  2. Commit-email select. It now goes through kept, so a refused profile keeps the chosen option instead of resetting to the stored one. Two new tests cover this: one for the stored value and one for a non-stored choice.
  3. One source of truth. accountUpdate builds the refusal once with refused(form, errors). ProfileForm.errors is gone, and the form-level toast reads refused.errors.form. The 200-character line is gone.
  4. SECRET_NAME. It is now your anchored regex, so keywords and monkey are refilled. One addition you did not ask for: a second regex for camelCase (newPassword, apiKey), which yours would have missed. New tests cover select kept, checkbox kept (ticked and unticked), a non-secret name containing key kept, and the camelCase secrets not echoed.

To try it, open /settings, set the email to not an email and pick "My email above"; the choice and the typed text survive, with the message beside Email. Or open /settings/password, type different new and confirm passwords, and submit.

commented

A form the server refuses now keeps what the person typed, the commit-email choice on the profile form included. Each refusal is built in one place. The secret-name checks are anchored. A feature doc, docs/features/form-keeps-input.md, describes the behaviour.

The check had failed on the "pipeline first" gate because this pull adds a feature doc and changes nothing under scripts/, src/flow/, src/nightly/, src/checks/ or forge.json. I did not touch the pipeline. I pushed an empty commit whose message carries the line the gate accepts: Direction: a refused form losing the user's typed input is a visible defect in shipped pages, ahead of pipeline items. No code changed in this push.

I did not rerun the tests or the type check. The code is unchanged since the last push, and this push only adds the gate's line. The forge's check on this push will show whether the gate now passes.

To try it, submit a form with an invalid value on the preview at https://lab--13194--forge-trevato-apps.open-platform.sh. The page should come back with the error and your other fields still filled in.

commented

I added the missing row for docs/features/form-keeps-input.md to docs/features/README.md. That fixes the two feature-note index failures ("no feature note is an orphan" and "the index is one note to a row…"). test/feature-orphans.test.ts now passes all 4 tests locally, and the fix is pushed.

I did not touch the third failure, "a sleeping preview woken at the limit > stays asleep…". It ran 120 seconds and looks like a timeout in a part of the app this pull doesn't change. I didn't run that test or re-check it, so the forge's next check will show whether it is flaky or something else.

Sign in to join this conversation.

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs