Fix: Required validation for Ninja Forms integration#821
Merged
dannyvankooten merged 1 commit intoibericode:mainfrom Feb 18, 2026
Merged
Conversation
b062c96 to
40ae920
Compare
40ae920 to
fd270c6
Compare
Member
|
Merged. Thank you @faisalahammad! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🎯 Summary
Fixes an issue where the "Mailchimp opt-in" field in Ninja Forms would not prevent form submission when marked as "Required" but left unchecked. This PR implements strict validation logic for the checkbox field.
📋 Issue Reference
Fixes #717
🔍 Problem Description
Current Behavior
In Ninja Forms, when the "Mailchimp for WordPress" checkbox field is added to a form and the "Required" toggle is enabled, users can still submit the form without checking the box. This happens because Ninja Forms treats an unchecked checkbox (value
0) as a valid submission unless specific validation logic overrides it.Expected Behavior
If the field is marked as "Required", the form submission should be blocked with an error message ("This field is required") if the checkbox is not checked.
Root Cause
The
MC4WP_Ninja_Forms_Fieldclass extendsNF_Abstracts_Inputbut did not implement a customvalidate()method to handle the specific behavior of unchecked checkboxes (which submit a value of0, passing the default empty check in some contexts or failing to trigger the required error correctly).✨ Solution Overview
Approach Taken
I implemented a
validate()method inMC4WP_Ninja_Forms_Fieldthat strictly checks:required.0.If both are true, it returns a validation error.
Why This Approach
This leverages Ninja Forms' native validation pipeline. By overriding the
validatemethod in the field class itself, we ensure the rule is enforced consistently whenever the field is used, without relying on fragile frontend JavaScript or external hooks.🔧 Changes Made
Files Modified
integrations/ninja-forms/class-field.php- Addedvalidate()method to enforce required state.tests/NinjaFormsValidationTest.php- (New) Added unit tests to verify usage.Detailed Changes
1.
integrations/ninja-forms/class-field.phpBefore:
No
validate()method existed; it relied on the parent class which didn't handle the0value correctly for this specific required context.After:
Why This Works:
It explicitly checks the
requiredflag and thevalue. Since unchecked checkboxes submit0or empty string,empty($field['value'])correctly identifies the invalid state.🧪 Testing Performed
Test Environment
Manual Testing
Test Case 1: Required Field Validation
Steps:
Expected Result: Validation error "This field is required" appears.
Actual Result: ✅ Validation error appeared and submission was blocked.
Test Case 2: Successful Submission
Steps:
Expected Result: Form submits successfully.
Actual Result: ✅ Form submitted.
Automated Testing
Test Coverage:
✅ No breaking changes
Screen recording
Plugin build/zip
Uploading mailchimp-for-wp-issue-717.zip…
📚 Documentation Updates
✅ PR Checklist