Skip to content

fix: add inline validation message for empty subject field#560

Merged
knoxiboy merged 3 commits into
knoxiboy:mainfrom
muditd27:muditd27/form-validation
Jun 9, 2026
Merged

fix: add inline validation message for empty subject field#560
knoxiboy merged 3 commits into
knoxiboy:mainfrom
muditd27:muditd27/form-validation

Conversation

@muditd27

@muditd27 muditd27 commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

User description

Closes #527

What was done :
Audited both AskDoubt.tsx and AskAIView.tsx against the issue checklist. Most of the validation was already implemented in the codebase. The one missing piece was an inline error message for the required Subject field in the doubt form, which I added.

Changes made:
Added a single inline validation message below the Subject input in AskDoubt.tsx that shows "Subject is required." when the user has interacted with the field and left it empty. Uses the existing subjectWasEdited state so it doesn't show on first load.

Checklist verification:

  • Empty doubt submissions blocked, already implemented via disabled prop on submit button
  • Empty AI prompt submissions blocked, already implemented in AskAIView.tsx
  • Submit buttons show disabled/loading states, already implemented with Loader2 spinner
  • Inline validation message for subject field, added in this PR
  • Server-side validation preserved, no backend changes made

Description

Added inline validation message below the Subject field in AskDoubt.tsx. Shows "Subject is required." when the user has interacted with the field and left it empty.

Related Issue

Closes #527

Type of Change

  • Bug fix (non-breaking change that fixes an issue)

Screenshots (if UI change)

| Before | After |
|Screenshot 2026-06-03 185430 |Screenshot 2026-06-03 185652 |

How Has This Been Tested?

  • Tested locally with npm run dev
  • Verified on desktop viewport (1440px)

Checklist

  • I have tested my changes locally (npm run dev)
  • My code follows the existing code style (TypeScript, Tailwind, no any types)
  • I have not introduced unrelated changes (each PR should address one issue)
  • I have added comments where necessary
  • My branch is up to date with main
  • I have linked the related issue above
  • Screenshots are included (if this is a UI change)

Summary by CodeRabbit

  • New Features

    • Ask Doubt form now shows a red inline validation message under Subject when the user edited it and left it empty, giving immediate feedback.
  • Bug Fixes

    • Improved access checks for teacher doubts to prevent viewing when required classroom information is missing, reducing unauthorized access and ensuring correct permission handling.

CodeAnt-AI Description

Show a required-subject message in the doubt form and prevent invalid classroom reply access

What Changed

  • The doubt form now shows an inline “Subject is required.” message after the subject field has been touched and left empty
  • The message stays hidden on first load, so users only see it when they interact with the field
  • Reply access now fails safely when a classroom doubt is missing classroom details instead of attempting to continue

Impact

✅ Clearer form errors
✅ Fewer failed doubt submissions
✅ Safer reply access for classroom doubts

💡 Usage Guide

Checking Your Pull Request

Every time you make a pull request, our system automatically looks through it. We check for security issues, mistakes in how you're setting up your infrastructure, and common code problems. We do this to make sure your changes are solid and won't cause any trouble later.

Talking to CodeAnt AI

Got a question or need a hand with something in your pull request? You can easily get in touch with CodeAnt AI right here. Just type the following in a comment on your pull request, and replace "Your question here" with whatever you want to ask:

@codeant-ai ask: Your question here

This lets you have a chat with CodeAnt AI about your pull request, making it easier to understand and improve your code.

Example

@codeant-ai ask: Can you suggest a safer alternative to storing this secret?

Preserve Org Learnings with CodeAnt

You can record team preferences so CodeAnt AI applies them in future reviews. Reply directly to the specific CodeAnt AI suggestion (in the same thread) and replace "Your feedback here" with your input:

@codeant-ai: Your feedback here

This helps CodeAnt AI learn and adapt to your team's coding style and standards.

Example

@codeant-ai: Do not flag unused imports.

Retrigger review

Ask CodeAnt AI to review the PR again, by typing:

@codeant-ai: review

Check Your Repository Health

To analyze the health of your code repository, visit our dashboard at https://app.codeant.ai. This tool helps you identify potential issues and areas for improvement in your codebase, ensuring your repository maintains high standards of code health.

Closes knoxiboy#527

What was done :
Audited both AskDoubt.tsx and AskAIView.tsx against the issue checklist. Most of the validation was already implemented in the codebase. The one missing piece was an inline error message for the required Subject field in the doubt form, which I added.

Changes made:
Added a single inline validation message below the Subject input in AskDoubt.tsx that shows "Subject is required." when the user has interacted with the field and left it empty. Uses the existing subjectWasEdited state so it doesn't show on first load.

Checklist verification:

* [x] Empty doubt submissions blocked, already implemented via disabled prop on submit button
* [x] Empty AI prompt submissions blocked, already implemented in AskAIView.tsx
* [x] Submit buttons show disabled/loading states, already implemented with Loader2 spinner
* [x] Inline validation message for subject field, added in this PR
* [x] Server-side validation preserved, no backend changes made
@codeant-ai

codeant-ai Bot commented Jun 3, 2026

Copy link
Copy Markdown

CodeAnt AI is reviewing your PR.

@vercel

vercel Bot commented Jun 3, 2026

Copy link
Copy Markdown

@muditd27 is attempting to deploy a commit to the Karan Mani Tripathi 's projects Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions github-actions Bot added gssoc'26 GSSoC program issue level:advanced Advanced level task type:bug Bug fix labels Jun 3, 2026
@github-actions
github-actions Bot requested review from knoxiboy June 3, 2026 13:39

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Technical Review

Hi @muditd27! Thank you for your contribution to DoubtDesk.

The code changes look good. Before we can complete the technical review, approve, and merge this pull request, we have one final requirement for all contributors: Please star the DoubtDesk repository.

Once you have starred the repository, please drop a comment here saying "done" (or we will automatically detect it) and we will proceed with approving and merging your PR. Thank you.

@coderabbitai

coderabbitai Bot commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

Adds an inline "Subject is required." validation message in AskDoubt when the edited Subject is empty, and adds an early 403 guard plus adjusted classroom-membership lookup in replies GET handling for teacher-type doubts.

Changes

Subject field validation error display

Layer / File(s) Summary
Subject required validation message
src/components/AskDoubt.tsx
Inline validation error displays below the Subject input when subjectWasEdited is true and !subject.trim().

Replies GET: teacher-classroom authorization

Layer / File(s) Summary
Teacher doubt classroom membership guard
src/app/api/replies/route.ts
For doubt.type === 'teacher', return 403 when doubt.classroomId is missing; membership lookup now filters by user email (non-null) and doubt.classroomId to determine canTeach.

🎯 4 (Complex) | ⏱️ ~45 minutes

Suggested labels: quality:clean, mentor:knoxiboy

Suggested reviewers:

  • knoxiboy
🚥 Pre-merge checks | ✅ 2 | ❌ 3

❌ Failed checks (2 warnings, 1 inconclusive)

Check name Status Explanation Resolution
Out of Scope Changes check ⚠️ Warning Changes in src/app/api/replies/route.ts appear unrelated to issue #527 which focuses on client-side form validation UI. Remove API route changes from this PR or create a separate PR for backend access-control fixes; keep this PR focused on the subject validation message.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Linked Issues check ❓ Inconclusive The PR partially addresses issue #527 by implementing inline validation for the subject field, but leaves other requirements (AI form validation, loading states) incomplete. Clarify scope: either complete all #527 requirements (both AskDoubt and AskAIView validation, loading states) or document in the PR that this addresses only the subject validation subset.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the primary change: adding inline validation for the empty subject field in AskDoubt.tsx.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@github-actions

github-actions Bot commented Jun 3, 2026

Copy link
Copy Markdown

@coderabbitai review

1 similar comment
@github-actions

github-actions Bot commented Jun 3, 2026

Copy link
Copy Markdown

@coderabbitai review

@github-actions github-actions Bot added the invalid This doesn't seem right label Jun 3, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Technical Review

Hi @muditd27! Thank you for your contribution to DoubtDesk.

The code changes look good. Before we can complete the technical review, approve, and merge this pull request, we have one final requirement for all contributors: Please star the DoubtDesk repository.

Once you have starred the repository, please drop a comment here saying "done" (or we will automatically detect it) and we will proceed with approving and merging your PR. Thank you.

@github-actions github-actions Bot closed this Jun 3, 2026
@codeant-ai codeant-ai Bot added the size:XS label Jun 3, 2026
@github-actions github-actions Bot removed the size:XS label Jun 3, 2026
@codeant-ai

codeant-ai Bot commented Jun 3, 2026

Copy link
Copy Markdown

CodeAnt AI finished reviewing your PR.

@muditd27

muditd27 commented Jun 3, 2026

Copy link
Copy Markdown
Contributor Author

done

@knoxiboy knoxiboy removed gssoc'26 GSSoC program issue level:advanced Advanced level task type:bug Bug fix size/xs labels Jun 3, 2026
@knoxiboy knoxiboy reopened this Jun 6, 2026
@knoxiboy knoxiboy removed the invalid This doesn't seem right label Jun 6, 2026
@github-actions github-actions Bot added gssoc'26 GSSoC program issue level:critical Critical level task labels Jun 6, 2026
@knoxiboy

knoxiboy commented Jun 7, 2026

Copy link
Copy Markdown
Owner

@muditd27 look into the ci/cd failure and fix it

@muditd27

muditd27 commented Jun 7, 2026

Copy link
Copy Markdown
Contributor Author

Hi @knoxiboy, I looked into the CI failure. The unit test error is caused by a missing NEXT_PUBLIC_NEON_DB_CONNECTION_STRING environment variable in the CI environment, it's not related to my change (which only adds an inline validation message in AskDoubt.tsx). The same tests were likely failing before this PR too.

image

@knoxiboy

knoxiboy commented Jun 8, 2026

Copy link
Copy Markdown
Owner

Automated Detailed Review

Please address the above items and request a review again.

@codeant-ai

codeant-ai Bot commented Jun 8, 2026

Copy link
Copy Markdown

CodeAnt AI is running Incremental review

@codeant-ai codeant-ai Bot added the size:XS label Jun 8, 2026
@github-actions github-actions Bot removed the size:XS label Jun 8, 2026
@codeant-ai

codeant-ai Bot commented Jun 8, 2026

Copy link
Copy Markdown

CodeAnt AI Incremental review completed.

@coderabbitai coderabbitai Bot 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.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/components/AskDoubt.tsx (1)

410-423: ⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Associate the inline error with the Subject input for screen readers

Line 421 adds a visual error, but the input on Line 410 is not wired with aria-invalid/aria-describedby, so assistive tech may not announce the validation state.

Suggested fix
 <input
+    id="doubt-subject"
     type="text"
     value={subject}
@@
     className="w-full bg-slate-100 dark:bg-white/5 border border-slate-200 dark:border-white/10 rounded-2xl p-4 text-slate-900 dark:text-white focus:outline-none focus:border-blue-500/50 font-bold text-sm"
+    aria-invalid={subjectWasEdited && !subject.trim()}
+    aria-describedby={subjectWasEdited && !subject.trim() ? "doubt-subject-error" : undefined}
     required
 />
 {subjectWasEdited && !subject.trim() && (
-<p className="text-red-400 text-xs font-semibold px-1 mt-1">Subject is required.</p>
+<p id="doubt-subject-error" className="text-red-400 text-xs font-semibold px-1 mt-1">Subject is required.</p>
 )}

As per coding guidelines, "**/*.tsx: Review for: Accessibility (aria labels, keyboard navigation)`".

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/components/AskDoubt.tsx` around lines 410 - 423, The Subject input in
AskDoubt is showing a visual error but lacks accessibility attributes; update
the input element (where value={subject} and onChange calls
setSubject/setSubjectWasEdited) to include aria-invalid={subjectWasEdited &&
!subject.trim()} and aria-describedby pointing to the error message ID, and give
the <p> error node a stable id (e.g., subject-error) that is rendered only when
the error is shown so screen readers announce the validation state.

Source: Coding guidelines

🧹 Nitpick comments (1)
src/components/AskDoubt.tsx (1)

422-422: ⚡ Quick win

Move validation copy to constants

Line 422 introduces a hardcoded user-facing string ("Subject is required."), which should come from a shared constant to stay consistent with the TSX guideline and future i18n/copy updates.

As per coding guidelines, "**/*.tsx: Review for: No hardcoded strings (use constants)`".

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/components/AskDoubt.tsx` at line 422, Replace the hardcoded string
"Subject is required." in the AskDoubt component with a shared validation
constant: add/export a constant (e.g., SUBJECT_REQUIRED or
VALIDATION_MESSAGES.SUBJECT_REQUIRED) in your shared validation/copy constants
module and import it into AskDoubt.tsx, then use that constant in the <p>
element (the JSX that currently renders the text with className "text-red-400
text-xs font-semibold px-1 mt-1") so the message is centralized for consistency
and future i18n.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@src/components/AskDoubt.tsx`:
- Around line 410-423: The Subject input in AskDoubt is showing a visual error
but lacks accessibility attributes; update the input element (where
value={subject} and onChange calls setSubject/setSubjectWasEdited) to include
aria-invalid={subjectWasEdited && !subject.trim()} and aria-describedby pointing
to the error message ID, and give the <p> error node a stable id (e.g.,
subject-error) that is rendered only when the error is shown so screen readers
announce the validation state.

---

Nitpick comments:
In `@src/components/AskDoubt.tsx`:
- Line 422: Replace the hardcoded string "Subject is required." in the AskDoubt
component with a shared validation constant: add/export a constant (e.g.,
SUBJECT_REQUIRED or VALIDATION_MESSAGES.SUBJECT_REQUIRED) in your shared
validation/copy constants module and import it into AskDoubt.tsx, then use that
constant in the <p> element (the JSX that currently renders the text with
className "text-red-400 text-xs font-semibold px-1 mt-1") so the message is
centralized for consistency and future i18n.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: b770834a-2844-4a38-9320-1e1652b5cba0

📥 Commits

Reviewing files that changed from the base of the PR and between 82aab9b and bf08aea.

📒 Files selected for processing (1)
  • src/components/AskDoubt.tsx

@muditd27

muditd27 commented Jun 8, 2026

Copy link
Copy Markdown
Contributor Author

Hi @knoxiboy, I looked into the failing checks. The TypeScript error is in src/app/api/replies/route.ts (line 66), a type mismatch in the SQL query, unrelated to my change in AskDoubt.tsx. The Unit Tests are now passing. The Build Check failure likely stems from the same TypeScript error. My change only adds a 3-line inline validation message in AskDoubt.tsx and doesn't touch any API routes.

@codeant-ai

codeant-ai Bot commented Jun 9, 2026

Copy link
Copy Markdown

CodeAnt AI is running Incremental review

@github-actions github-actions Bot added size/s and removed size/xs labels Jun 9, 2026
@codeant-ai codeant-ai Bot added the size:S label Jun 9, 2026
@github-actions github-actions Bot removed the size:S label Jun 9, 2026
@codeant-ai

codeant-ai Bot commented Jun 9, 2026

Copy link
Copy Markdown

CodeAnt AI Incremental review completed.

@coderabbitai coderabbitai Bot 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.

🧹 Nitpick comments (1)
src/app/api/replies/route.ts (1)

61-72: ⚡ Quick win

Add explicit email guard and consider reusing existing membership query.

The email! assertion at line 69 is safe under current flow (lines 56-57 guarantee email exists when classroomId is set), but this invariant isn't obvious and could break if the code is refactored.

Additionally, if we reach line 64 with a truthy classroomId, we've already passed lines 49-55, meaning the membership was already fetched at line 50. The second query is redundant and adds an unnecessary database round-trip.

♻️ Suggested improvement

A minimal fix is to add an explicit guard for email:

 if (doubt.type === 'teacher') {
     if (!doubt.classroomId) {
         return errorResponse("Access denied", 403);
     }
+    if (!email) {
+        return errorResponse("Access denied", 403);
+    }
     const [membership] = await db
         .select()
         .from(membershipsTable)
         .where(
             and(
-                eq(membershipsTable.userEmail, email!),
+                eq(membershipsTable.userEmail, email),
                 eq(membershipsTable.classroomId, doubt.classroomId)
             )
         );

For a more thorough refactor, consider hoisting the membership variable from line 50 to a wider scope so it can be reused here, avoiding the duplicate query.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/app/api/replies/route.ts` around lines 61 - 72, The code uses a
non-obvious non-null assertion on email and repeats a memberships query; add an
explicit guard that returns errorResponse("Access denied", 403) if email is
falsy before any use, and refactor to reuse the already-fetched membership
instead of running a second DB query — hoist the initial membership variable
(the result of the first db.select() from membershipsTable) to a scope visible
where you check doubt.classroomId and use that membership for access checks
rather than calling db.select() again; ensure all references to membershipsTable
and membership use the hoisted variable and remove the duplicate query.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@src/app/api/replies/route.ts`:
- Around line 61-72: The code uses a non-obvious non-null assertion on email and
repeats a memberships query; add an explicit guard that returns
errorResponse("Access denied", 403) if email is falsy before any use, and
refactor to reuse the already-fetched membership instead of running a second DB
query — hoist the initial membership variable (the result of the first
db.select() from membershipsTable) to a scope visible where you check
doubt.classroomId and use that membership for access checks rather than calling
db.select() again; ensure all references to membershipsTable and membership use
the hoisted variable and remove the duplicate query.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 13bb8d6c-6b74-4d89-8c7c-8a017b6e4ee5

📥 Commits

Reviewing files that changed from the base of the PR and between bf08aea and 7cd30dc.

📒 Files selected for processing (1)
  • src/app/api/replies/route.ts

@muditd27

muditd27 commented Jun 9, 2026

Copy link
Copy Markdown
Contributor Author

Hi @knoxiboy, I've resolved the TypeScript error in replies/route.ts and added the missing framer-motion package. All checks are passing now except Vercel authorization which is a repo-level config. Ready for review.

@knoxiboy
knoxiboy merged commit 699e01d into knoxiboy:main Jun 9, 2026
17 of 18 checks passed
@github-actions github-actions Bot added gssoc:approved Approved for GSSoC mentor:knoxiboy Reviewed by mentor knoxiboy quality:clean Clean code quality and removed size/s review-needed labels Jun 9, 2026
@knoxiboy knoxiboy removed the mentor:knoxiboy Reviewed by mentor knoxiboy label Jun 11, 2026
Repository owner deleted a comment from github-actions Bot Jun 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gssoc:approved Approved for GSSoC gssoc'26 GSSoC program issue level:beginner Beginner level task quality:clean Clean code quality type:bug Bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add disabled and validation states to doubt and AI prompt forms

2 participants