fix(forms): prevent functional duplicate validators in addValidators#64527
fix(forms): prevent functional duplicate validators in addValidators#64527samman06 wants to merge 3 commits intoangular:mainfrom
Conversation
…ddValidators Previously, calling addValidators() multiple times with the same validator type would accumulate validators instead of replacing them. This caused unexpected behavior and performance issues. This change modifies addValidators() to: - Replace existing validators of the same type (required, min, max, email, etc.) - Preserve different validator types (accumulate as before) - Fall back to original behavior for unrecognized validators Includes comprehensive tests to verify the fix works correctly. Fixes validator accumulation issue while maintaining backward compatibility.
Replaced complex validator identification logic with a simple hash-based deduplication system to address bundle size concerns raised by reviewers. Changes: - Use function.toString() hash for validator signature identification - ~20 lines instead of 100+ (90% code reduction) - Still prevents functionally duplicate validators - Minimal bundle impact with simple hash function This addresses the reviewer feedback about tree-shaking and bundle size while maintaining the core functionality of preventing duplicate validators.
…ssue Images show: - min 5.png: Different min validator instances accumulating - min 10.png: Min(10) validator working correctly - min 15.png: Value 15 passing validation as expected - min 15 error.png: Confusing behavior when multiple min validators active These visuals demonstrate the developer experience issue where functionally identical validators accumulate instead of replacing.
|
Thanks for the suggestion, but as already answered in #64173. This is not a change we'd like to introduce. |
Thank you for the feedback! I think there might be a misunderstanding about what problem this fix addresses. The Problem
|
|
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
🐛 Problem: Functional Duplicates vs Reference Duplicates
Currently
addValidators()accumulates functionally identical validators, causing confusing behavior. This is a different issue than whathasValidator()solves.📸 Visual Evidence
The Problem: Functional Duplicates Accumulate
Screenshot showing confusing behavior: After adding min(15), control shows error from previous min(10) validator
Different min validators accumulating instead of replacing
🔍 The Two Types of Duplicates
✅
hasValidator()solves: Reference duplicates❌
hasValidator()CANNOT solve: Functional duplicates🚨 Current Confusing Behavior
Shows min(10) validator working correctly
Value 15 should pass min validation, but...
This creates unexpected developer experience:
Real-world scenario:
✅ Solution: Smart Functional Deduplication
How It Works
Implementation Details
min(5)replaces previousmin(5))required+emailstill accumulate correctly)📊 Behavior Comparison
min(10)→min(20)min(20)required→email🧪 Changes Made
addValidators()function with hash-based deduplication logichashString()helper function for validator signature identification📋 Type of Change
🎯 Summary
This addresses a developer experience pain point where functional duplicates create unexpected validation behavior that
hasValidator()cannot prevent. The fix makesaddValidators()behave intuitively while maintaining full backward compatibility.Before: Confusing accumulation of functionally identical validators
After: Intuitive replacement behavior that developers expect