Skip to content

feat: add client-side JS validation to checkout flow#397

Merged
superdav42 merged 1 commit intomainfrom
feature/checkout-js-validation
Mar 24, 2026
Merged

feat: add client-side JS validation to checkout flow#397
superdav42 merged 1 commit intomainfrom
feature/checkout-js-validation

Conversation

@superdav42
Copy link
Copy Markdown
Collaborator

Summary

  • Exposes PHP validation rules to JavaScript via wp_localize_script (laravel-jsvalidation approach) so client-side and server-side rules stay in sync without duplication
  • Adds validate_client_side() to the Vue checkout app that runs before the AJAX call, giving instant per-field error feedback
  • Server-only rules (uniqueness checks, DB lookups) are intentionally skipped client-side and still validated via AJAX

What changed

PHP (inc/checkout/class-checkout.php)

  • get_js_validation_rules() — new method that parses the PHP rule strings (required, email, min:N, max:N, alpha_dash, lowercase, same:field, integer, accepted) into a JS-friendly array; skips server-only rules (unique_site, unique:\WP_User, products, country, state, city)
  • get_checkout_variables() — exposes validation_rules and field_labels to the wu_checkout JS object via wp_localize_script
  • Added i18n strings for all new validation messages (translatable)

JavaScript (assets/js/checkout.js)

  • validate_client_side(values) — new Vue method that evaluates the exposed rules against current form values; uses the existing errors array and get_error() mechanism for per-field display (no new UI needed)
  • validate_form() — calls validate_client_side() first; only proceeds to AJAX if client-side passes, avoiding unnecessary network round-trips

Design decisions

  • No new UI: reuses the existing <span v-if="get_error(...)"> per-field error display already present in all field templates
  • No new library: pure vanilla JS rule evaluation — no external dependency
  • Extensible: wu_checkout_js_validation_rules filter lets plugins add/modify rules; wu_checkout_validation_rules filter already covers the PHP side
  • Graceful degradation: if wu_checkout.validation_rules is absent (e.g. older cached page), the client-side check is a no-op and AJAX validation still runs

Testing

  1. Load a checkout form with required fields (email, username, password, site URL)
  2. Submit without filling required fields — errors appear instantly without a network request
  3. Fill fields with invalid data (bad email format, username with spaces, short password) — per-field errors appear immediately
  4. Fill all fields correctly — form proceeds to AJAX validation as before

Closes #279

Expose PHP validation rules to JavaScript via wp_localize_script so
client-side validation stays in sync with server-side rules without
duplicating logic (laravel-jsvalidation approach).

Changes:
- Checkout::get_js_validation_rules() — parses the PHP rule strings
  (required, email, min, max, alpha_dash, lowercase, same, integer,
  accepted) into a JS-friendly array; skips server-only rules
  (unique_site, unique:\WP_User, products, country, state, city)
- Checkout::get_checkout_variables() — exposes validation_rules and
  field_labels to wu_checkout JS object via wp_localize_script
- checkout.js validate_client_side() — runs rules client-side before
  the AJAX call; shows per-field errors instantly via existing Vue
  get_error() / errors array mechanism
- validate_form() — calls validate_client_side() first; only proceeds
  to AJAX if client-side passes (avoids unnecessary round-trips)
- i18n strings for all new validation messages added to PHP and JS

Closes #279
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Mar 24, 2026

Warning

Rate limit exceeded

@superdav42 has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 15 minutes and 2 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 2eefdbd3-cd77-4a01-a1c0-4dd9d936ebff

📥 Commits

Reviewing files that changed from the base of the PR and between 922d85c and a197a5b.

📒 Files selected for processing (2)
  • assets/js/checkout.js
  • inc/checkout/class-checkout.php
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/checkout-js-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.

@superdav42 superdav42 merged commit 1b06942 into main Mar 24, 2026
8 of 10 checks passed
@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

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve chekout with js validation

1 participant