-
Notifications
You must be signed in to change notification settings - Fork 90
gw-gravity-forms-rounding.php
: Fixed an issue with rounding now updating form total.
#1126
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
base: master
Are you sure you want to change the base?
Conversation
…ating form total.
WalkthroughThe JavaScript function responsible for rounding input values in Gravity Forms was updated to dispatch a native Changes
Possibly related PRs
Suggested reviewers
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 phpcs (3.7.2)gravity-forms/gw-gravity-forms-rounding.phpWarning: PHP Startup: Invalid date.timezone value 'UTC', using 'UTC' instead in Unknown on line 0 Fatal error: Uncaught Error: Class "Phar" not found in /usr/local/bin/phpcs:3 ✨ Finishing Touches
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. 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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this 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-gravity-forms-rounding.php (1)
188-196
: Consider adding error handling for the native event dispatch.While the implementation is correct, consider adding error handling around the
dispatchEvent
call to gracefully handle edge cases where the DOM element might not support the method.self.applyRoundingActions = function( $input ) { var value = self.getRoundedValue( $input ); if( $input.val() != value ) { $input.val( value ).change(); const input = $input[0]; - // Ensure Gravity Forms detects the programmatic value change by dispatching a native input event - input.dispatchEvent( new Event( 'input', { bubbles: true } ) ); + // Ensure Gravity Forms detects the programmatic value change by dispatching a native input event + if ( input && typeof input.dispatchEvent === 'function' ) { + input.dispatchEvent( new Event( 'input', { bubbles: true } ) ); + } } };
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
gravity-forms/gw-gravity-forms-rounding.php
(2 hunks)
🧰 Additional context used
🧠 Learnings (1)
📓 Common learnings
Learnt from: saifsultanc
PR: gravitywiz/snippet-library#1079
File: gp-populate-anything/gppa-acf-repeater-mapper.php:78-87
Timestamp: 2025-04-17T02:43:14.227Z
Learning: When working with Gravity Forms, `rgar()` is a utility function that safely retrieves values from arrays and can be used to implement cleaner safety checks compared to direct conditionals.
Learnt from: veryspry
PR: gravitywiz/snippet-library#1077
File: gravity-forms/gw-require-alt-text-description-post-image.php:23-32
Timestamp: 2025-05-30T13:53:21.893Z
Learning: In Gravity Forms snippets, use 'gravityforms' as the translation domain for internationalization functions instead of creating custom domains, to maintain consistency with the related plugin.
⏰ Context from checks skipped due to timeout of 90000ms (1)
- GitHub Check: Danger JS
🔇 Additional comments (2)
gravity-forms/gw-gravity-forms-rounding.php (2)
192-194
: LGTM! Good implementation of native input event dispatching.The addition of native input event dispatching correctly addresses the issue where Gravity Forms wasn't detecting programmatic value changes for form total recalculation. Using
bubbles: true
ensures the event propagates properly through the DOM.
222-222
: Excellent fix for type coercion issues.The change from loose inequality (
!=
) to strict inequality (!==
) prevents unintended type coercion between numeric zero and empty strings, which was causing the minimum rounding functionality to fail when values were entered manually.
Context
⛑️ Ticket(s): https://secure.helpscout.net/conversation/2981539269/85760
Summary
The GF Rounding snippet isn't working as expected.
ISSUE 1
The minimum rounding doesn't work when the value is entered. It works only when you're using the number field controls to increase or decrease the value.
We had
if( value != '' )
, which we changed toif( value !== '' )
Switching from loose inequality (
!=
) to strict inequality (!==
) to prevent unintended type coercion. This ensures that numeric 0 and empty string '' are treated as distinct values during comparison. With!=
, javascript converts0 != ''
as a numeric comparison with both values as0
.ISSUE 2
When the rounding is applied to a quantity value, it doesn't update the total.
Using
$input.val( value ).change();
alone wasn’t enough to trigger Gravity Forms' total recalculation in some cases. Gravity Forms listens for native input events on fields like quantity and product fields for live calculations. We add a native input event dispatch after setting the value, ensuring Gravity Forms detects the change and recalculates totals as if the user had typed the value manually.BEFORE (Samuel's loom):
https://www.loom.com/share/c94f0966f80f495db537dc68e7d5da81
AFTER:
https://www.loom.com/share/b012dd305bdb4ed1b800c2ef3957bcf9