Instatic icon indicating copy to clipboard operation
Instatic copied to clipboard

[Bug]: CMS-native form rejects every select value ("invalid_option") because validation compares against option.id, while form controls send option.value

Open Mineorey opened this issue 1 month ago • 0 comments

Summary

CMS-native form (base.form, mode cms) whose target table has a Select field can never be submitted. The server answers 400 Invalid form values with {"fieldId":"…","code":"invalid_option","message":"Choose one of the allowed options."} for every option the visitor picks.

The form side and the validation side use different option identifiers:

  • The form controls send the option's value, for example general_request. The editor's own "insert missing field" helper builds base.option nodes with value: option.value (src/admin/pages/site/panels/PropertiesPanel/formSettingsAnalysis.ts, around line 242). option.value is also the slug authors see and edit in the Data tab ("Field behavior → Options", right-hand column).
  • src/core/forms/validation.ts only accepts the option's id, the random nanoid generated in newFieldDialogModel.ts ({ id: nanoid(), label, value: slugifyOptionValue(label) }). Authors never see that id in the UI:
// src/core/forms/validation.ts (origin/main 112d7442, lines 222–227)
if (field.type === 'select' && typeof value === 'string') {
  const allowed = new Set(field.options.map((option) => option.id))
  if (!allowed.has(value)) {
    return { fieldId: field.id, code: 'invalid_option', message: 'Choose one of the allowed options.' }
  }
}

multiSelect has the same check (lines 229–233).

Proposed fix

Accept both id and value in the form validation and normalise to the stored representation, which is the option id. The Data grid's CellDisplayRenderer already resolves both: options.find((o) => o.id === value || o.value === value). Issue #488 also shows that cells and loops work with the id.

function resolveOptionId(field: { options: { id: string; value: string }[] }, raw: string): string | null {
  const match = field.options.find((option) => option.id === raw || option.value === raw)
  return match ? match.id : null
}

// select
if (field.type === 'select' && typeof value === 'string') {
  const id = resolveOptionId(field, value)
  if (id === null) return { fieldId: field.id, code: 'invalid_option', message: 'Choose one of the allowed options.' }
  // store `id` instead of the raw value (adjust the caller so the coerced value is replaced)
}

// multiSelect: map every item through resolveOptionId, fail if any is null

Because validateCoercedValue currently only returns an error, the normalisation can sit in coerceFieldValue for select / multiSelect. That function already has field, so the stored cell becomes the option id while the error path stays the same.

Suggested tests (src/core/forms/validation.test.ts):

  • select accepts option.value and stores option.id
  • select still accepts option.id (backwards compatible)
  • select rejects an unknown string with invalid_option
  • multiSelect with mixed id/value items stores ids; one unknown item → invalid_option

Steps to reproduce

  1. In the Data workspace, create a data table with a Select field topic and options, e.g. label "General request" → value general_request.
  2. In the Site editor, add a base.form (mode CMS-native) with that table as target. Add the field with the setup block ("insert missing field") or bind an existing base.select with options whose values are the option values.
  3. Publish, open the page, choose "General request", submit.

Expected behavior

The submission is accepted and the row stores the chosen option, just like when the option is picked in the Data grid.

Actual behavior

POST /_instatic/form/submit → 400

{"error":"Invalid form values","errors":[{"fieldId":"topic","code":"invalid_option","message":"Choose one of the allowed options."}]}

No select field can be submitted through a form, not even one created by Instatic's own form setup helper. The only workaround is to change the field type to Text.

Version or commit

0.0.20

Deployment mode

Docker Compose with SQLite

Logs or screenshots


Mineorey avatar Oct 12 '26 12:10 Mineorey