Skip to content

fix: invalid date in a DateTime-bound Datepicker no longer crashes the app - #4413

Merged
phlipsterit merged 3 commits into
mainfrom
fix/4180-invalid-date-in-datetime-field
Nov 19, 2026
Merged

phlipsterit merged 3 commits into
mainfrom
fix/4180-invalid-date-in-datetime-field

Conversation

@phlipsterit

@phlipsterit phlipsterit commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Leaving a Datepicker bound to a DateTime field with text it can't parse no longer crashes the app. That includes an impossible date (32.13.2026) and, more commonly, an unfinished one (12.12, saved as 12.12.____). The user now gets the Datepicker's own "invalid date format" message, and the invalid text is never sent to the backend.

 Datepicker blur("32.13.2026")
   setLeafValue
     convertData(value, schema)            # src/features/formData/convertData.ts
-      type: string → accept anything
+      type: string + format: date-time + !strictParseISO(value) → error
-    → currentData → PATCH → backend 500 → "Ukjent feil" error page
+    → invalidCurrentData (kept in the field, never sent)
   useDatepickerValidation                 # src/layout/Datepicker/useDatepickerValidation.ts
-    reads saved value only
+    reads saved value ?? invalid value → "Ugyldig datoformat. Bruk formatet DD.MM.YYYY."
   Datepicker.getValidationFilters         # src/layout/Datepicker/index.tsx
+    drop InvalidData 'validation_errors.pattern' (duplicate of the message above)

This reuses the existing invalid-data mechanism, the same one that already handles e.g. - typed into a number field.

Only format: date-time is checked. Studio generates a C# DateTime for date-time fields, so the backend can't store text that isn't a date. For format: date it generates a string, which stores any text, so those fields can't cause this crash and are left unchanged.

Evidence

A v8 app (Altinn.App 8.12.15) in localtest, with a Datepicker bound to DateTime? dato ("type": "string", "format": "date-time"). Type 32.13.2026, then leave the field.

  • Before (released frontend 4.37.0): PATCH replace /dato "32.13.2026" → 500 → error page.
image

After (this branch): the invalid text is not sent, and the form stays usable with one validation message.

image
  • Unfinished date (12.12), same app:

    • Before (4.37.0): PATCH replace /dato "12.12.____" → 500 → the same error page.
    • After: the invalid text is not sent, and the form shows "Ugyldig datoformat. Bruk formatet DD.MM.YYYY.".
  • Tests (both fail on main and pass on this branch):

    convertData('32.13.2026', { type: 'string', format: 'date-time' })
      main:   { newValue: '32.13.2026', error: false }
      branch: { newValue: undefined,    error: true }
    
    Datepicker bound to a date-time field: type 32.13.2026, tab
      shows 'Ugyldig datoformat. Bruk formatet DD.MM.YYYY.'
      does not show 'Feil format eller verdi'
      doPatchFormData not called          # main: called once with the invalid text
    

    yarn tsc and yarn lint pass. The Jest suites for formData, validation, Datepicker, Input and datamodel all pass.

Merge Danger

Door: two-way

Frontend only, with no data migration. Reverting restores the old behaviour.

Blast Radius: date-time fields

  • Applies to every component bound to a string field with format: date-time, not just the Datepicker. Text that isn't an ISO date is now kept as invalid data instead of being sent.
  • If the user types an invalid date over a saved one, the saved date is removed on the server, so after a reload the field is empty. Invalid numbers already work this way. Before this change, the save failed and the app crashed.
  • strictParseISO (date-fns) is a bit more lenient than .NET's System.Text.Json. Rare ISO forms the Datepicker can't produce (week dates like 2026-W01-1, a space instead of T) still reach the backend and still crash the app.

Follow-up (not in this PR): FormDataWriteProvider throws every save error to the error boundary (if (error) throw error;). Handling a 4xx from PATCH without crashing would also cover the rare forms above.

Related Issue(s)

Verification/QA

  • Manual functionality testing
    • I have tested these changes manually
  • Automated tests
    • Unit test(s) have been added/updated
  • UU/WCAG
    • I have tested with a screen reader/keyboard navigation/automated wcag validator
  • User documentation @ altinn-studio-docs
    • No functionality has been changed/added, so no documentation is needed
  • Support in Altinn Studio
    • This change/feature does not require any changes to Altinn Studio
  • Labels
    • I have added a kind/* and backport* label to this PR for proper release notes grouping

Summary by CodeRabbit

  • Bug Fixes
    • Date-time fields now flag malformed date and timestamp values as invalid while preserving date-only text for validation.
    • Invalid date-time input displays the datepicker’s format message without a duplicate generic error or an unintended form update.
    • Datepicker validation now reflects the latest typed value, including invalid input.

Typing an impossible date like 32.13.2026 into a Datepicker bound to a
DateTime field sent the raw text in the PATCH. The backend could not
deserialize it and the frontend crashed with "Ukjent feil".

convertData now rejects values that are not ISO dates (strictParseISO)
for string fields with format date or date-time, so they are kept as
invalid data and never sent. The Datepicker reads the invalid value to
show its own "invalid date format" message, and filters out the generic
invalid-data message to avoid a duplicate.

Refs #4180
@phlipsterit phlipsterit added kind/bug Something isn't working backport This PR should be cherry-picked onto older release branches labels Nov 17, 2026
@coderabbitai

coderabbitai Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Date-time string conversion now rejects values that strictParseISO cannot parse. Datepicker validation reads invalid field data when no valid value is available and filters a generic pattern validation. Tests cover conversion results and invalid date-time input in the datepicker.

Changes: Date-time validation

Layer / File(s) Summary
Date-time conversion
src/features/formData/convertData.ts, src/features/formData/convertData.test.ts
Conversion now uses the schema format and rejects unparseable date-time strings with an error. Tests cover valid and malformed values, anyOf, and the behavior for format: "date".
Datepicker invalid input handling
src/layout/Datepicker/useDatepickerValidation.ts, src/layout/Datepicker/index.tsx, src/layout/Datepicker/DatepickerComponent.test.tsx
The validation hook falls back to invalid debounced field data. The datepicker filters InvalidData pattern validations. A test checks the datepicker message, absence of the generic error, and no form-data patch.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Possibly related issues

  • Issue 4180 describes a crash when entering an impossible date in a Datepicker bound to a DateTime field. This change handles malformed date-time input in conversion and datepicker validation.

Suggested reviewers: olemartinorg, bjosttveit

Merge Risk: Minimal · up to 80d13

Typing an unparseable date into a date-time Datepicker now shows the field's own invalid-format message and no longer sends the text to the backend, which avoids the crash. The change is narrowly scoped to date-time string fields and has tests for the changed behavior. The known gap, that invalid text replacing a saved date removes the saved value, is documented follow-up work. No blocking risk remains for merge.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: invalid text in a DateTime-bound Datepicker no longer crashes the app. The PR stops unparseable date-time text from being sent to the backend and shows the Datepicker'…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description follows the template well. It has a clear summary with a diff, the related issue (Fixes #4180), manual and automated test checkboxes with evidence, and a merge danger section. Remainin…
Full details: Title check

Explanation

The title names the main change: invalid text in a DateTime-bound Datepicker no longer crashes the app. The PR stops unparseable date-time text from being sent to the backend and shows the Datepicker's own invalid-format message. The title is concise, specific, and matches the changeset, so it passes.

Full details: Description check

Explanation

The description follows the template well. It has a clear summary with a diff, the related issue (Fixes #4180), manual and automated test checkboxes with evidence, and a merge danger section. Remaining gaps: the UU/WCAG screen reader testing box is unchecked and no UU/WCAG testing is stated, and the Cypress E2E checkbox is not addressed. The author also left the kind/* label checkbox ticked, but the label itself cannot be verified from the description. These are non-critical, so the description is mostly complete.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 74f53264-9239-4f88-aa77-63169234b5d5
Commits

Reviewing files that changed from the base of the PR and between fcd6b4c and 80d1311.

Files selected for processing: 5

  • src/features/formData/convertData.test.ts
  • src/features/formData/convertData.ts
  • src/layout/Datepicker/DatepickerComponent.test.tsx
  • src/layout/Datepicker/index.tsx
  • src/layout/Datepicker/useDatepickerValidation.ts

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

Studio generates a C# DateTime only for format date-time. For format
date it generates a string that accepts any text, so validating it
would stop saving the text without preventing any crash.

@JamalAlabdullah JamalAlabdullah left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

@phlipsterit
phlipsterit merged commit 4c1c746 into main Nov 19, 2026
17 checks passed
@phlipsterit
phlipsterit deleted the fix/4180-invalid-date-in-datetime-field branch November 19, 2026 13:53
@github-actions

Copy link
Copy Markdown
Contributor

✅ Automatic backport successful!

A backport PR has been automatically created for the release/v4.37 release branch.

The release branch release/v4.37 already existed and was updated.

The cherry-pick was clean with no conflicts. Please review the backport PR when it appears.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport This PR should be cherry-picked onto older release branches kind/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DateTime in the data model cases crash on wrong input

2 participants