Skip to content

t509: fix: template_selection field validation maps to template_id rule key#800

Merged
superdav42 merged 2 commits intomainfrom
bugfix/template-validation
Apr 12, 2026
Merged

t509: fix: template_selection field validation maps to template_id rule key#800
superdav42 merged 2 commits intomainfrom
bugfix/template-validation

Conversation

@superdav42
Copy link
Copy Markdown
Collaborator

@superdav42 superdav42 commented Apr 12, 2026

Summary

  • The template_selection checkout field submits its value as template_id in POST data, but the validation rule lookup used field['id'] (template_selection) directly — no rule key existed under that name, so the required check never fired.
  • Adds a $field_to_rule_key map so the rule is registered under template_id; also adds min:1 for template_id because Rakit treats 0 as "present" under required alone.
  • Fixes the validation error label from "Template ID" → "Template Selection" for UX consistency.

Changes

  • EDIT: inc/checkout/class-checkout.php$field_to_rule_key map, mapped-key rule lookup, min:1 for template_id, label fix

Verification

# Manual: add a required template_selection field to a checkout form, submit with no template selected
# Expected: validation error "Template Selection is required"
# Automated: vendor/bin/phpunit --filter Checkout

Resolves #799

Summary by CodeRabbit

  • Bug Fixes
    • Improved validation handling in checkout forms to ensure required fields are properly enforced.
    • Fixed an issue preventing invalid template selections from being caught during validation.
    • Updated error messaging labels for improved clarity when validation fails.

@superdav42 superdav42 added the origin:interactive Created by interactive user session label Apr 12, 2026
@superdav42
Copy link
Copy Markdown
Collaborator Author

Merge Summary

PR: #800 — t509: fix: template_selection field validation maps to template_id rule key
Issue: Resolves #799
Branch: bugfix/template-validation → main

What was done

Fixed checkout validation so a required attribute on the template_selection signup field actually enforces a selection:

  1. Root cause: class-checkout.php looked up the validation rule under field['id'] (template_selection), but the POST key is template_id — no rule existed under template_selection, so required was silently skipped.
  2. Fix: Added $field_to_rule_key map (template_selection → template_id) and used the mapped key for all rule registration.
  3. Edge case: Rakit treats integer 0 as "present" under required, so added min:1 for template_id to reject unselected templates.
  4. UX: Fixed validation error label "Template ID" → "Template Selection".

Files changed

  • inc/checkout/class-checkout.php (+31, -4)

Verification

Manual: checkout form with required template_selection field, submit without selecting → validation error "Template Selection is required" is shown.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 12, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 0dadd617-5039-46b9-9f53-85fe6a37a28a

📥 Commits

Reviewing files that changed from the base of the PR and between 2d4054f and a246286.

📒 Files selected for processing (1)
  • inc/checkout/class-checkout.php

📝 Walkthrough

Walkthrough

The checkout validation system now maps field IDs to their corresponding validation rule keys, ensuring the template_selection field's required validation fires correctly against the template_id rule. A min:1 constraint was added to reject zero values, and the error label was updated for clarity.

Changes

Cohort / File(s) Summary
Checkout Validation Fix
inc/checkout/class-checkout.php
Added $field_to_rule_key mapping to redirect field ID lookups to correct rule keys. Applied required rule to mapped keys. Extended template_id validation with min:1 constraint to reject zero values. Updated validation alias label from "Template ID" to "Template Selection".

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 A template once lost in the validation maze,
Now hopping through the mapped pathways with ease,
No sneaky zeros slip through anymore—
The field finds its rule, and the form's at peace!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 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 main fix: mapping the template_selection field validation to the template_id rule key, which is the core issue being resolved.
Linked Issues check ✅ Passed All three acceptance criteria from issue #799 are met: field validation now fires via the mapping, template_id=0 is rejected with min:1, and the error label was updated to 'Template Selection'.
Out of Scope Changes check ✅ Passed All changes are directly related to resolving issue #799: the field-to-rule-key mapping, min:1 validation rule, and label update are all within scope.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

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

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch bugfix/template-validation

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
Copy link
Copy Markdown

🔨 Build Complete - Ready for Testing!

📦 Download Build Artifact (Recommended)

Download the zip build, upload to WordPress and test:

🌐 Test in WordPress Playground (Very Experimental)

Click the link below to instantly test this PR in your browser - no installation needed!
Playground support for multisite is very limitied, hopefully it will get better in the future.

🚀 Launch in Playground

Login credentials: admin / password

@github-actions
Copy link
Copy Markdown

🔨 Build Complete - Ready for Testing!

📦 Download Build Artifact (Recommended)

Download the zip build, upload to WordPress and test:

🌐 Test in WordPress Playground (Very Experimental)

Click the link below to instantly test this PR in your browser - no installation needed!
Playground support for multisite is very limitied, hopefully it will get better in the future.

🚀 Launch in Playground

Login credentials: admin / password

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 12, 2026

Performance Test Results

Performance test results for b525b6d are in 🛎️!

Note: the numbers in parentheses show the difference to the previous (baseline) test run. Differences below 2% or 0.5 in absolute values are not shown.

URL: /

Run DB Queries Memory Before Template Template WP Total LCP TTFB LCP - TTFB
0 41 37.78 MB 916.00 ms (+26.50 ms / +3% ) 171.50 ms (+10.00 ms / +6% ) 1125.50 ms (+34.50 ms / +3% ) 2194.00 ms (+112.00 ms / +5% ) 2099.95 ms (+118.65 ms / +6% ) 98.30 ms (+7.85 ms / +8% )
1 56 49.02 MB 982.00 ms (+22.00 ms / +2% ) 153.50 ms 1136.00 ms 2182.00 ms (+50.00 ms / +2% ) 2093.25 ms (+48.00 ms / +2% ) 87.05 ms

@superdav42 superdav42 merged commit 1736c4a into main Apr 12, 2026
11 checks passed
superdav42 added a commit that referenced this pull request Apr 12, 2026
superdav42 added a commit that referenced this pull request Apr 12, 2026
…gets_Test

Add 7 unit tests covering the template_selection → template_id validation
mapping fix from PR #800:
- template_selection required attribute maps to template_id rule key
- min:1 guard rejects template_id=0 during checkout
- positive template_id passes validation
- no min:1 added when template_selection field is absent
- template_id=0 allowed with base rule (admin/network context)
- non-template required fields still map to themselves

Fix Dashboard_Widgets_Test failure caused by PR #785 adding
is_network_admin() guard to enqueue_scripts(). The test now uses
set_current_screen('dashboard-network') to simulate the network admin
context. Also adds a new test confirming the per-site dashboard does
NOT enqueue wu-activity-stream.
superdav42 added a commit that referenced this pull request Apr 12, 2026
…gets_Test (#801)

Add 7 unit tests covering the template_selection → template_id validation
mapping fix from PR #800:
- template_selection required attribute maps to template_id rule key
- min:1 guard rejects template_id=0 during checkout
- positive template_id passes validation
- no min:1 added when template_selection field is absent
- template_id=0 allowed with base rule (admin/network context)
- non-template required fields still map to themselves

Fix Dashboard_Widgets_Test failure caused by PR #785 adding
is_network_admin() guard to enqueue_scripts(). The test now uses
set_current_screen('dashboard-network') to simulate the network admin
context. Also adds a new test confirming the per-site dashboard does
NOT enqueue wu-activity-stream.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

origin:interactive Created by interactive user session

Projects

None yet

Development

Successfully merging this pull request may close these issues.

t509: fix: template_selection validation never fires — field ID maps to wrong rule key

1 participant