Skip to content

Conversation

@Magnusrm
Copy link
Contributor

@Magnusrm Magnusrm commented Dec 8, 2025

Description

Added required validation handling that checks if the instance has any attachments that stems from the
ImageUpload component.

Related Issue(s)

Verification/QA

  • Manual functionality testing
    • I have tested these changes manually
    • Creator of the original issue (or service owner) has been contacted for manual testing (or will be contacted when released in alpha)
    • No testing done/necessary
  • Automated tests
    • Unit test(s) have been added/updated
    • Cypress E2E test(s) have been added/updated
    • No automatic tests are needed here (no functional changes/additions)
    • I want someone to help me make some tests
  • UU/WCAG (follow these guidelines until we have our own)
    • I have tested with a screen reader/keyboard navigation/automated wcag validator
    • No testing done/necessary (no DOM/visual changes)
    • I want someone to help me perform accessibility testing
  • User documentation @ altinn-studio-docs
    • Has been added/updated
    • No functionality has been changed/added, so no documentation is needed
    • I will do that later/have created an issue
  • Support in Altinn Studio
    • Issue(s) created for support in Studio
    • This change/feature does not require any changes to Altinn Studio
  • Sprint board
    • The original issue (or this PR itself) has been added to the Team Apps project and to the current sprint board
    • I don't have permissions to do that, please help me out
  • Labels
    • I have added a kind/* and backport* label to this PR for proper release notes grouping
    • I don't have permissions to add labels, please help me out

Summary by CodeRabbit

  • New Features

    • Required image upload fields now trigger client-side validation and show a clear error when left empty.
  • Localization

    • Added "You must upload an image" translations in English, Norsk Bokmål, and Nynorsk.
  • Tests

    • Added end-to-end tests to verify required-image validation appears when empty and clears after a successful upload.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 8, 2025

📝 Walkthrough

Walkthrough

Adds required-field validation for the ImageUpload component: a new validation hook, integration into the component, three i18n entries (en/nb/nn), and an end-to-end test that verifies the required-image behavior on navigation and after upload.

Changes

Cohort / File(s) Change Summary
Translations
src/language/texts/en.ts, src/language/texts/nb.ts, src/language/texts/nn.ts
Added image_upload_component.error_required localization key in English, Bokmål, and Nynorsk.
Validation hook
src/layout/ImageUpload/hooks/useValidateRequiredImageUpload.tsx
New exported hook useValidateRequiredImageUpload(baseComponentId: string) returning a ComponentValidation[] that emits a required-field validation when the ImageUpload is required and has no attachments (message key image_upload_component.error_required, severity error, source FrontendValidationSource.Component, category ValidationMask.Required).
Component integration
src/layout/ImageUpload/index.tsx
Added useEmptyFieldValidation(baseComponentId: string): ComponentValidation[] on ImageUpload, delegating to the new hook.
End-to-end test
test/e2e/integration/component-library/image-upload.ts
New E2E test that asserts required-image validation prevents navigation when empty and clears after uploading an image.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Review focus areas:
    • useValidateRequiredImageUpload.tsx: ensure attachments lookup and required-flag logic are correct and that the returned validation shape/metadata matches validation system expectations.
    • src/layout/ImageUpload/index.tsx: confirm useEmptyFieldValidation is invoked where navigation validation collects component validations.
    • E2E test: check intercept stability and file-upload simulation/selectors.

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title directly addresses the main issue: fixing required validation for the ImageUpload component, which matches the core objective in the changeset.
Description check ✅ Passed The description follows the template with all required sections: description, related issue, and comprehensive verification/QA checklist with appropriate selections.
Linked Issues check ✅ Passed All code changes directly address issue #3892: translation keys support validation messages, the new hook implements required validation logic for ImageUpload, and the E2E test verifies the fix works.
Out of Scope Changes check ✅ Passed All changes are in-scope and directly support the required validation feature for ImageUpload: translation keys, validation hook, component integration, and E2E test.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/required-not-working-for-image-upload-component

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3d91383 and 4cafdc1.

📒 Files selected for processing (1)
  • src/layout/ImageUpload/hooks/useValidateRequiredImageUpload.tsx (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/layout/ImageUpload/hooks/useValidateRequiredImageUpload.tsx
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: Install
  • GitHub Check: Analyze (javascript)
  • GitHub Check: Type-checks, eslint, unit tests and SonarCloud

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

Comment @coderabbitai help to get the list of available commands and usage tips.

@Magnusrm Magnusrm added kind/bug Something isn't working backport-ignore This PR is a new feature and should not be cherry-picked onto release branches labels Dec 8, 2025
@Magnusrm Magnusrm added the squad/utforming Issues that belongs to the named squad. label Dec 9, 2025
@Magnusrm Magnusrm moved this to 👷 In progress in Team Altinn Studio Dec 9, 2025
@Magnusrm Magnusrm added backport This PR should be cherry-picked onto older release branches backport-ignore This PR is a new feature and should not be cherry-picked onto release branches and removed backport-ignore This PR is a new feature and should not be cherry-picked onto release branches backport This PR should be cherry-picked onto older release branches labels Dec 9, 2025
@Magnusrm Magnusrm marked this pull request as ready for review December 9, 2025 09:45
@Magnusrm Magnusrm moved this from 👷 In progress to ⚠️ Blocked in Team Altinn Studio Dec 9, 2025
@Magnusrm Magnusrm moved this from ⚠️ Blocked to 🔎 In review in Team Altinn Studio Dec 9, 2025
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
test/e2e/integration/component-library/image-upload.ts (1)

79-97: End‑to‑end coverage for required image behavior is solid; consider minor enhancements

The test correctly:

  • Marks the ImageUpload as required and enables validateOnNext.
  • Verifies the required error appears when clicking next with no image.
  • Uploads and saves an image, then verifies the error is cleared.

Two optional improvements:

  • Also assert on navigation behavior (e.g., that you remain on the same page before upload, and can move on after upload) to align directly with the original bug description about navigation being allowed.
  • Consider using an existing page object / helper (if available) for the "Next" button instead of the hard‑coded /next/i matcher to avoid coupling to a specific language string.
src/layout/ImageUpload/index.tsx (1)

7-42: Hook delegation from layout definition is functionally OK, but Sonar warning should be handled explicitly

useEmptyFieldValidation delegates to the custom hook useValidateRequiredImageUpload, which is the right place for the actual hook logic, and functionally this should work as long as callers invoke useEmptyFieldValidation from a stable hook context (which matches how other layout defs are typically used).

However, static analysis flags this as "calling a hook from a class component" because the call appears inside a class, even though ImageUpload is a layout definition and not a React.Component subclass. To keep Sonar/ESLint noise under control, consider one of:

  • Add an inline disable with a short justification, e.g.:

    // eslint-disable-next-line react-hooks/rules-of-hooks
    useEmptyFieldValidation(baseComponentId: string): ComponentValidation[] {
      return useValidateRequiredImageUpload(baseComponentId);
    }
  • Or, if there is an established pattern elsewhere in this repo for calling hooks from layout defs, align with that pattern (for example, by centralizing the hook call in a top‑level helper function that Sonar already knows is safe, and having the class method just forward the result).

Functionally this looks correct; the main concern is resolving the static analysis warning in a controlled way.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 63e93e5 and 3baeff3.

📒 Files selected for processing (6)
  • src/language/texts/en.ts (1 hunks)
  • src/language/texts/nb.ts (1 hunks)
  • src/language/texts/nn.ts (1 hunks)
  • src/layout/ImageUpload/hooks/useValidateRequiredImageUpload.tsx (1 hunks)
  • src/layout/ImageUpload/index.tsx (2 hunks)
  • test/e2e/integration/component-library/image-upload.ts (1 hunks)
🧰 Additional context used
📓 Path-based instructions (3)
**/*.{ts,tsx}

📄 CodeRabbit inference engine (CLAUDE.md)

**/*.{ts,tsx}: Avoid using any type or type casting (as type) in TypeScript code; improve typing by avoiding casts and anys when refactoring
Use objects for managing query keys and functions, and queryOptions for sharing TanStack Query patterns across the system for central management

Files:

  • src/language/texts/nb.ts
  • src/layout/ImageUpload/hooks/useValidateRequiredImageUpload.tsx
  • src/layout/ImageUpload/index.tsx
  • test/e2e/integration/component-library/image-upload.ts
  • src/language/texts/nn.ts
  • src/language/texts/en.ts
{**/*.module.css,**/*.{ts,tsx}}

📄 CodeRabbit inference engine (CLAUDE.md)

Use CSS Modules for component styling and leverage Digdir Design System components when possible

Files:

  • src/language/texts/nb.ts
  • src/layout/ImageUpload/hooks/useValidateRequiredImageUpload.tsx
  • src/layout/ImageUpload/index.tsx
  • test/e2e/integration/component-library/image-upload.ts
  • src/language/texts/nn.ts
  • src/language/texts/en.ts
src/layout/*/{config,Component,index,config.generated}.{ts,tsx}

📄 CodeRabbit inference engine (CLAUDE.md)

Layout components must follow standardized structure with config.ts, Component.tsx, index.tsx, and config.generated.ts files

Files:

  • src/layout/ImageUpload/index.tsx
🧠 Learnings (2)
📓 Common learnings
Learnt from: lassopicasso
Repo: Altinn/app-frontend-react PR: 3654
File: src/layout/ImageUpload/ImageUploadSummary2/ImageUploadSummary2.tsx:49-53
Timestamp: 2025-10-14T09:01:40.985Z
Learning: In the Altinn app-frontend-react codebase, when data integrity is guaranteed through business logic and parent components guard rendering (e.g., `storedImage ? <Component /> : undefined`), non-null assertions on guaranteed fields like `storedImage!.data?.filename` are acceptable and preferred over optional chaining with fallbacks.
📚 Learning: 2025-11-25T12:53:54.399Z
Learnt from: CR
Repo: Altinn/app-frontend-react PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-11-25T12:53:54.399Z
Learning: Applies to **/*.test.{ts,tsx} : Use `renderWithProviders` from `src/test/renderWithProviders.tsx` when testing components that require form layout context

Applied to files:

  • src/layout/ImageUpload/index.tsx
  • test/e2e/integration/component-library/image-upload.ts
🧬 Code graph analysis (3)
src/layout/ImageUpload/hooks/useValidateRequiredImageUpload.tsx (3)
src/features/validation/index.ts (1)
  • ComponentValidation (151-153)
src/utils/layout/useNodeItem.ts (1)
  • useItemWhenType (15-33)
src/utils/layout/DataModelLocation.tsx (1)
  • useIndexedId (117-120)
src/layout/ImageUpload/index.tsx (2)
src/features/validation/index.ts (1)
  • ComponentValidation (151-153)
src/layout/ImageUpload/hooks/useValidateRequiredImageUpload.tsx (1)
  • useValidateRequiredImageUpload (6-23)
test/e2e/integration/component-library/image-upload.ts (1)
test/e2e/support/apps/component-library/uploadImageAndVerify.ts (1)
  • uploadImageAndVerify (8-23)
🪛 GitHub Check: SonarCloud Code Analysis
src/layout/ImageUpload/index.tsx

[warning] 41-41: React Hook "useValidateRequiredImageUpload" cannot be called in a class component. React Hooks must be called in a React function component or a custom React Hook function.

See more on https://sonarcloud.io/project/issues?id=Altinn_app-frontend-react&issues=AZr-RCOabOiZaeb3Lhnz&open=AZr-RCOabOiZaeb3Lhnz&pullRequest=3893

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Install
🔇 Additional comments (4)
src/language/texts/en.ts (1)

211-223: New image upload required error key looks consistent

Key naming and message fit the existing image_upload_component.* namespace and match the new validation logic and tests.

src/language/texts/nb.ts (1)

212-224: Bokmål translation aligns with usage

The added image_upload_component.error_required key and text match the e2e assertion and surrounding image upload messages.

src/language/texts/nn.ts (1)

212-224: Nynorsk translation key is consistent

The image_upload_component.error_required entry is correctly added and consistent with the en/nb variants and nearby image upload messages.

src/layout/ImageUpload/hooks/useValidateRequiredImageUpload.tsx (1)

1-23: Required‑image validation hook is correctly wired

The hook cleanly ties the required flag and attachment list together and emits a ComponentValidation with the new image_upload_component.error_required key, severity, source, and category set appropriately. The implementation fits the existing validation infrastructure and matches the intended behavior.

@lassopicasso lassopicasso self-assigned this Dec 9, 2025
Copy link
Contributor

@lassopicasso lassopicasso left a comment

Choose a reason for hiding this comment

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

Good job, I tested it also.

Just a suggestion about adjustment for the new hook.

And should we take into account what sonarcloud is complaining about: "React Hook "useValidateRequiredImageUpload" cannot be called in a class component."?

@lassopicasso lassopicasso assigned Magnusrm and unassigned lassopicasso Dec 9, 2025
Magnusrm and others added 3 commits December 10, 2025 09:11
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 62045b9 and 3d91383.

📒 Files selected for processing (1)
  • src/layout/ImageUpload/hooks/useValidateRequiredImageUpload.tsx (1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
**/*.{ts,tsx}

📄 CodeRabbit inference engine (CLAUDE.md)

**/*.{ts,tsx}: Avoid using any type or type casting (as type) in TypeScript code; improve typing by avoiding casts and anys when refactoring
Use objects for managing query keys and functions, and queryOptions for sharing TanStack Query patterns across the system for central management

Files:

  • src/layout/ImageUpload/hooks/useValidateRequiredImageUpload.tsx
{**/*.module.css,**/*.{ts,tsx}}

📄 CodeRabbit inference engine (CLAUDE.md)

Use CSS Modules for component styling and leverage Digdir Design System components when possible

Files:

  • src/layout/ImageUpload/hooks/useValidateRequiredImageUpload.tsx
🧬 Code graph analysis (1)
src/layout/ImageUpload/hooks/useValidateRequiredImageUpload.tsx (4)
src/features/validation/index.ts (1)
  • ComponentValidation (151-153)
src/utils/layout/useNodeItem.ts (1)
  • useItemWhenType (15-33)
src/utils/layout/NodesContext.tsx (1)
  • NodesInternal (407-509)
src/utils/layout/DataModelLocation.tsx (1)
  • useIndexedId (117-120)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Type-checks, eslint, unit tests and SonarCloud
🔇 Additional comments (3)
src/layout/ImageUpload/hooks/useValidateRequiredImageUpload.tsx (3)

1-4: LGTM: Imports are clean and well-organized.

All imports are properly typed and used in the implementation below.


6-8: LGTM: Hook setup follows React patterns correctly.

The hooks are called unconditionally, and the type checking via useItemWhenType ensures type safety.


14-23: LGTM: Validation object is correctly structured.

The validation object properly implements the ComponentValidation interface with appropriate severity, source, and category for a required field validation.

@sonarqubecloud
Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
C Reliability Rating on New Code (required ≥ A)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

@Magnusrm Magnusrm merged commit 8eed596 into main Dec 10, 2025
15 of 16 checks passed
@Magnusrm Magnusrm deleted the fix/required-not-working-for-image-upload-component branch December 10, 2025 09:23
@github-project-automation github-project-automation bot moved this from 🔎 In review to ✅ Done in Team Altinn Studio Dec 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-ignore This PR is a new feature and should not be cherry-picked onto release branches kind/bug Something isn't working squad/utforming Issues that belongs to the named squad.

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Required validation for image upload not working as intended

2 participants