Skip to content

Fix js error when triggering change event on hidden field#2255

Merged
Crabcyborg merged 1 commit into
masterfrom
fix_js_error_when_triggering_change_event_on_hidden_field
Feb 20, 2025
Merged

Fix js error when triggering change event on hidden field#2255
Crabcyborg merged 1 commit into
masterfrom
fix_js_error_when_triggering_change_event_on_hidden_field

Conversation

@Crabcyborg
Copy link
Copy Markdown
Contributor

@Crabcyborg Crabcyborg commented Feb 20, 2025

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Feb 20, 2025

Walkthrough

The changes introduce an additional check in the validateField function within js/formidable.js. The new logic determines whether the field has a container. If a container is not present (typically for hidden fields), the function returns early without proceeding with JavaScript validation. No removals were made, so the existing functionality remains unchanged.

Changes

File Change Summary
js/formidable.js Added a conditional check in validateField to skip validation when the field container does not exist.

Sequence Diagram(s)

sequenceDiagram
    participant VF as validateField()
    participant FC as Field Container Check

    VF->>VF: Check for field container
    alt Field container is absent (hidden field)
        VF-->>VF: Return early (skip validation)
    else Field container present
        VF-->>VF: Continue with validation logic
    end
Loading

Possibly related PRs

Suggested reviewers

  • AbdiTolesa
  • garretlaxton
✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown
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 (1)
js/formidable.js (1)

271-279: Consider consolidating hidden field handling into a utility function.

The codebase has multiple places where hidden fields are checked and handled differently. Consider extracting this logic into a utility function like isHiddenField or shouldValidateField to improve maintainability and consistency.

Example implementation:

+function isHiddenField(field) {
+  return field.type === 'hidden' && !field.closest('.frm_form_field');
+}

 function maybeValidateChange(field) {
   if (field.type === 'url') {
     maybeAddHttpToUrl(field);
   }
   const form = field.closest('form');
-  if (form && hasClass(form, 'frm_js_validate')) {
+  if (form && hasClass(form, 'frm_js_validate') && !isHiddenField(field)) {
     validateField(field);
   }
 }

 function validateField(field) {
   let errors, key;

   errors = [];
   const fieldContainer = field.closest('.frm_form_field');

-  if (!fieldContainer) {
-    // Hidden fields do not have a field container and do not require JS validation.
-    return;
-  }

+  if (isHiddenField(field)) {
+    return;
+  }

   if (hasClass(fieldContainer, 'frm_required_field') && !hasClass(field, 'frm_optional')) {
     errors = checkRequiredField(field, errors);
   }

Also applies to: 299-325

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2bcae7b and 250a022.

📒 Files selected for processing (1)
  • js/formidable.js (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (6)
  • GitHub Check: PHP 8 tests in WP trunk
  • GitHub Check: Cypress
  • GitHub Check: PHP 7.4 tests in WP trunk
  • GitHub Check: PHP 8 tests in WP trunk
  • GitHub Check: PHP 7.4 tests in WP trunk
  • GitHub Check: Cypress
🔇 Additional comments (2)
js/formidable.js (2)

305-308: LGTM! The changes effectively prevent JS errors for hidden fields.

The added check for field containers is a robust solution that:

  1. Prevents null reference errors when validating hidden fields
  2. Maintains clean code by explicitly documenting the behavior
  3. Aligns with the PR's objective to fix JS errors for hidden fields

305-308:

Details

❓ Verification inconclusive

Verify handling of dynamically shown hidden fields.

The current implementation assumes that hidden fields never require JS validation. Please verify that this assumption holds true when:

  1. Hidden fields are made visible through JavaScript
  2. Hidden fields are used for dynamic form behavior
  3. Hidden fields contain validation rules that should be enforced

Run this script to check for dynamic visibility changes of hidden fields:


🏁 Script executed:

#!/bin/bash
# Search for code that might change visibility of hidden fields
rg -A 3 "\.show\(\)|\.hide\(\)|style\.display|\.visible|\.hidden" 

Length of output: 61724


Re-check dynamic validation for fields that transition from hidden to visible.

The current implementation skips JS validation when a field lacks a container (i.e. is hidden). Since our codebase includes several instances where elements are dynamically shown or hidden (as seen in various files like formidable_admin.js and others), please verify that any field initially hidden—and therefore not validated—will be properly revalidated if it later becomes visible. In particular, confirm that:

  • Dynamically revealed fields trigger a re-check of any associated validation rules.
  • There is adequate handling for validation errors on fields that transition from hidden to visible.

@Crabcyborg Crabcyborg merged commit efed3f8 into master Feb 20, 2025
@Crabcyborg Crabcyborg deleted the fix_js_error_when_triggering_change_event_on_hidden_field branch February 20, 2025 19:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant