From 2da97e580971f45fdcf86d76e7921d012193ec78 Mon Sep 17 00:00:00 2001 From: Kishore Date: Wed, 20 Aug 2025 15:51:43 +0530 Subject: [PATCH 1/2] Resolved the OtpInput value mismatch issue for input types text and password. --- maui/src/OtpInput/SfOtpInput.cs | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/maui/src/OtpInput/SfOtpInput.cs b/maui/src/OtpInput/SfOtpInput.cs index 42337e02..b818fc0d 100644 --- a/maui/src/OtpInput/SfOtpInput.cs +++ b/maui/src/OtpInput/SfOtpInput.cs @@ -937,27 +937,16 @@ static void OnValuePropertyChanged(BindableObject bindable, object oldValue, obj string newValueStr = newValue?.ToString() ?? string.Empty; string oldValueStr = oldValue?.ToString() ?? string.Empty; + newValueStr = new string(newValueStr.Where(c => !char.IsControl(c)).ToArray()); + oldValueStr = new string(oldValueStr.Where(c => !char.IsControl(c)).ToArray()); - if (otpInput.Type == OtpInputType.Number) + if (otpInput.ValueChanged != null) { - newValueStr = new string(newValueStr.Where(c => !char.IsControl(c)).ToArray()); - oldValueStr = new string(oldValueStr.Where(c => !char.IsControl(c)).ToArray()); + RaiseValueChangedEvent(otpInput, oldValueStr, newValueStr); } - if (otpInput.ValueChanged != null) - { - if (otpInput.Type == OtpInputType.Number) - { - RaiseValueChangedEvent(otpInput, oldValueStr, newValueStr); - } - else - { - RaiseValueChangedEvent(otpInput, oldValue?.ToString() ?? string.Empty, newValue?.ToString() ?? string.Empty); - } - } - - otpInput.UpdateValue(bindable, newValue?? string.Empty); - } + otpInput.UpdateValue(bindable, newValue ?? string.Empty); + } } /// From 040f0f13ad90a5e95868fa187c01d87ad9eb63ff Mon Sep 17 00:00:00 2001 From: Kishore Date: Thu, 21 Aug 2025 12:56:49 +0530 Subject: [PATCH 2/2] updated the changes. --- maui/src/OtpInput/SfOtpInput.cs | 8 +---- .../Editors/SfOtpInputUnitTests.cs | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/maui/src/OtpInput/SfOtpInput.cs b/maui/src/OtpInput/SfOtpInput.cs index b818fc0d..547b3bee 100644 --- a/maui/src/OtpInput/SfOtpInput.cs +++ b/maui/src/OtpInput/SfOtpInput.cs @@ -939,12 +939,7 @@ static void OnValuePropertyChanged(BindableObject bindable, object oldValue, obj string oldValueStr = oldValue?.ToString() ?? string.Empty; newValueStr = new string(newValueStr.Where(c => !char.IsControl(c)).ToArray()); oldValueStr = new string(oldValueStr.Where(c => !char.IsControl(c)).ToArray()); - - if (otpInput.ValueChanged != null) - { - RaiseValueChangedEvent(otpInput, oldValueStr, newValueStr); - } - + RaiseValueChangedEvent(otpInput, oldValueStr, newValueStr); otpInput.UpdateValue(bindable, newValue ?? string.Empty); } } @@ -959,7 +954,6 @@ static void OnLengthPropertyChanged(BindableObject bindable, object oldValue, ob { if (bindable is SfOtpInput otpInput && newValue is double newLength && oldValue is double oldLength) { - if (newLength <= 0) { newLength = oldLength; diff --git a/maui/tests/Syncfusion.Maui.Toolkit.UnitTest/Editors/SfOtpInputUnitTests.cs b/maui/tests/Syncfusion.Maui.Toolkit.UnitTest/Editors/SfOtpInputUnitTests.cs index 0c17db14..da040726 100644 --- a/maui/tests/Syncfusion.Maui.Toolkit.UnitTest/Editors/SfOtpInputUnitTests.cs +++ b/maui/tests/Syncfusion.Maui.Toolkit.UnitTest/Editors/SfOtpInputUnitTests.cs @@ -474,6 +474,37 @@ public void RaiseValueChangedEvent_ShouldTrigger_ValueChangedEvent() Assert.True(eventTriggered); } + [Theory] + [InlineData(OtpInputType.Number, "12", "12")] + [InlineData(OtpInputType.Text, "ab", "ab")] + [InlineData(OtpInputType.Password, "xy", "xy")] + public void ValueChangedEvent_WithPartialInput_DoesNotContainNullCharacters(OtpInputType type, string input, string expected) + { + // Arrange + var otpInput = new SfOtpInput + { + Length = 4, + Type = type + }; + string? newValueFromEvent = null; + string? oldValueFromEvent = null; + otpInput.ValueChanged += (sender, e) => + { + newValueFromEvent = e.NewValue; + oldValueFromEvent = e.OldValue; + }; + // Act + otpInput.Value = input; + // Assert + Assert.Equal(expected, newValueFromEvent); + Assert.Equal(string.Empty, oldValueFromEvent); + if (newValueFromEvent != null) + { + Assert.DoesNotContain('\0', newValueFromEvent!); + } + } + + #endregion #region Public Methods