-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Fixed Exception should throw on value fails on validation #32707
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: main
Are you sure you want to change the base?
Conversation
|
Hey there @@SubhikshaSf4851! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed. |
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.
Pull Request Overview
This PR fixes a behavioral issue where BindableObject.SetValueCore was silently returning when a value failed validation, instead of throwing an exception as documented in Microsoft's official documentation. The fix changes the behavior to throw an ArgumentException when the ValidateValue callback returns false, aligning with the documented behavior and matching the pattern used in Xamarin.Forms.
Key Changes
- Changed
SetValueCoremethod to throwArgumentExceptioninstead of returning silently when validation fails - Added comprehensive unit test to verify the exception throwing behavior
- Maintained the error message format consistent with other validation failures in the codebase
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/Controls/src/Core/BindableObject.cs | Updated SetValueCore to throw ArgumentException when ValidateValue callback returns false, replacing the silent return with explicit exception throwing |
| src/Controls/tests/Core.UnitTests/BindablePropertyUnitTests.cs | Added new test SetValueThrowsExceptionWhenValidationFails to verify that validation failures now throw ArgumentException with the correct message format |
|
|
||
| if (property.ValidateValue != null && !property.ValidateValue(this, value)) | ||
| { | ||
| Application.Current?.FindMauiContext()?.CreateLogger<BindableObject>()?.LogWarning($"Value is an invalid value for {property.PropertyName}"); |
Copilot
AI
Nov 18, 2025
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.
The LogWarning call is redundant when an exception is about to be thrown. When an exception is thrown, it's clear that an error occurred, and the exception message itself provides the necessary information. The logging statement should be removed to avoid redundant error output.
Consider removing the logging line:
if (property.ValidateValue != null && !property.ValidateValue(this, value))
{
throw new ArgumentException($"Value is an invalid value for {property.PropertyName}", nameof(value));
}| Application.Current?.FindMauiContext()?.CreateLogger<BindableObject>()?.LogWarning($"Value is an invalid value for {property.PropertyName}"); |
| { | ||
| Application.Current?.FindMauiContext()?.CreateLogger<BindableObject>()?.LogWarning($"Value is an invalid value for {property.PropertyName}"); | ||
| return; | ||
| throw new ArgumentException($"Value is an invalid value for {property.PropertyName}", nameof(value)); |
Copilot
AI
Nov 18, 2025
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.
The XML documentation for the SetValue method should be updated to include the new ArgumentException that can be thrown when validation fails. Looking at the code at line 473-479, the documentation currently only lists ArgumentNullException, but should also document that ArgumentException is thrown when the value fails validation.
The XML documentation should be updated to include:
/// <exception cref="ArgumentException">Thrown when the value is invalid according to the assigned logic in <see cref="BindableProperty.ValidateValueDelegate"/>.</exception>This follows the pattern used in the CoerceValue method (line 803 in the full file context).
Note
Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!
RootCause :
The current implementation of
BindableObject.SetValueCoredoes not throw an exception when a value fails on validation.Instead, it logs a warning and returns early.
Description of Change :
SetValueCore(inBindableObject.cs) to throw anArgumentExceptionwhen theValidateValuecallback returnsfalse, instead of silently returning without setting the value.Reference :
Source : Microsoft Docs — Validation Callbacks
Xamarin : Reference
Issues Fixed
Fixes #25823
Tested the behavior in the following platforms
BeforeFix25823.mov
AfterFix25823.mov