Skip to content

gw-quantity-as-decimal.php: Fixed an issue with Calculation Product field not editable with decimal values. #720

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

saifsultanc
Copy link
Contributor

Context

⛑️ Ticket(s): https://secure.helpscout.net/conversation/2386150351/55864?folderId=3808239

Summary

https://github.com/gravitywiz/snippet-library/blob/master/gravity-forms/gw-quantity-as-decimal.php
The snippet doesn't work when editing the quantity of a calculated product field on the backend. This happens when the "Output HTML5" setting is enabled on Gravity Forms. On the Calculation field, Gravity Forms runs this logic:

$qty_min_attr = GFFormsModel::is_html5_enabled() ? "min='0'" : '';

This adds the "min='0'" to the input field (For example, <input type="number" name="input_3.3" value="5.2" id="ginput_quantity_305_3" class="ginput_quantity" size="10" min="0">. This field would not be editable with decimal values now.

Screenshot 2023-10-12 at 8 12 43 PM

To fix this, we can add the step attribute

@saifsultanc saifsultanc added the bug Something isn't working label Oct 12, 2023
@saifsultanc saifsultanc self-assigned this Jan 17, 2025
Copy link

coderabbitai bot commented Mar 10, 2025

Walkthrough

The changes update the logic in the GW_Quantity_Decimal class within the Gravity Forms integration. The init method now conditionally adds filters based on the Gravity Forms version (>=2.8) or the manual enabling of HTML5 support. If neither condition is met, the function exits early. In addition, a new fix_content method has been introduced to modify the HTML of quantity input fields by ensuring a step="any" attribute is present when working with decimal values.

Changes

File Change Summary
gravity-forms/gw-quantity-as-decimal.php - Updated init method: Now conditionally registers filters based on Gravity Forms version or HTML5 config.
- Added new fix_content method which adjusts input content to include step="any" for decimal quantity fields.

Sequence Diagram(s)

sequenceDiagram
    participant GF as Gravity Forms Plugin
    participant QD as GW_Quantity_Decimal

    GF->>QD: Call init()
    alt Gravity Forms version >= 2.8 or HTML5 enabled
        QD->>Filters: Register render and modify input filters
    else Not met
        QD->>GF: Return early (skip filters)
    end
    Note over QD: Later in processing, when handling a quantity field input
    GF->>QD: Process input field
    QD->>QD: Call fix_content() to modify input HTML (adding step="any" if needed)
Loading

📜 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 4f83701 and 5e19360.

📒 Files selected for processing (1)
  • gravity-forms/gw-quantity-as-decimal.php (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • gravity-forms/gw-quantity-as-decimal.php

🪧 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.
  • @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

github-actions bot commented Mar 10, 2025

Warnings
⚠️ When ready, don't forget to request reviews on this pull request from your fellow wizards.

Generated by 🚫 dangerJS against 5e19360

Copy link

@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)
gravity-forms/gw-quantity-as-decimal.php (1)

96-114: Good implementation that targets only necessary fields

The fix_content function effectively addresses the original issue by:

  1. Only targeting inputs with the ginput_quantity class
  2. Only modifying fields that have decimal values
  3. Only adding the step="any" attribute when it's not already present

This implementation satisfies the previous review request to "make sure it's only targeting the desired fields."

One minor suggestion - consider adding a check for the min attribute in the HTML to make the targeting even more specific, since that's what causes the original issue according to the PR objectives:

if ( preg_match('/\bvalue=[\'"]([\d]+\.[\d]+)[\'"]/i', $inputTag, $valueMatch ) ) {
-   if ( ! preg_match('/\bstep=[\'"]any[\'"]/i', $inputTag ) ) {
+   if ( ! preg_match('/\bstep=[\'"]any[\'"]/i', $inputTag ) && preg_match('/\bmin=[\'"]0[\'"]/i', $inputTag) ) {
        $inputTag = preg_replace( '/<input/i', '<input step="any"', $inputTag, 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 278e693 and 4f83701.

📒 Files selected for processing (1)
  • gravity-forms/gw-quantity-as-decimal.php (2 hunks)
🔇 Additional comments (3)
gravity-forms/gw-quantity-as-decimal.php (3)

46-49: Good approach for version compatibility

Adding this conditional check ensures backward compatibility while fixing the issue for newer Gravity Forms versions or when HTML5 is manually enabled. Early return pattern is appropriate here.


51-55: Proper organization of filter hooks

Moving these filter hooks inside the conditional block ensures they're only added when needed (GF 2.8+ or HTML5 enabled). The comments clearly explain the reasoning behind the conditions.


56-56: Well-placed content filter

Adding the gform_field_content filter to handle the fix is appropriate, as it will process the field after it's been rendered but before it's displayed to the user.

coderabbitai bot added a commit that referenced this pull request Mar 10, 2025
…ield`

Docstrings generation was requested by @saifsultanc.

* #720 (comment)

The following files were modified:

* `gravity-forms/gw-quantity-as-decimal.php`
Copy link

coderabbitai bot commented Mar 10, 2025

Note

Generated docstrings for this pull request at #1045

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

Successfully merging this pull request may close these issues.

2 participants