Skip to content
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

Placeholder for validation error messages #137

Merged
merged 3 commits into from
Jan 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ public static ReactiveProperty<T> SetValidateAttribute<T>(this ReactiveProperty<
{
var memberExpression = (MemberExpression)selfSelector.Body;
var propertyInfo = (PropertyInfo)memberExpression.Member;
var display = propertyInfo.GetCustomAttribute<DisplayAttribute>();
var attrs = propertyInfo.GetCustomAttributes<ValidationAttribute>().ToArray();
var context = new ValidationContext(self)
{
MemberName = nameof(ReactiveProperty<T>.Value)
DisplayName = display?.GetName() ?? propertyInfo.Name,
MemberName = nameof(ReactiveProperty<T>.Value),
};

if (attrs.Length != 0)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions Test/ReactiveProperty.Tests/ReactiveProperty.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,17 @@
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
</ItemGroup>
<ItemGroup>
<Compile Update="Strings\Resource.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Resource.resx</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="Strings\Resource.resx">
<Generator>PublicResXFileCodeGenerator</Generator>
<LastGenOutput>Resource.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
</Project>
63 changes: 62 additions & 1 deletion Test/ReactiveProperty.Tests/ReactivePropertyValidationTest.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reactive.Linq;
using System.Threading.Tasks;
using Microsoft.Reactive.Testing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Reactive.Bindings;
using ReactiveProperty.Tests.Strings;

namespace ReactiveProperty.Tests
{
Expand Down Expand Up @@ -136,7 +138,7 @@ public async Task AsyncValidation_FailedCase()
var errorMessage = "error occured!!";
rprop.Value = "dummy"; //--- push value
tcs.SetResult(errorMessage); //--- validation error!
await Task.Yield();
await Task.Delay(10);

rprop.HasErrors.IsTrue();
error.IsNotNull();
Expand Down Expand Up @@ -237,6 +239,45 @@ public void IgnoreInitErrorAndUpdateValue()
rp.Value = "";
rp.HasErrors.IsTrue();
}

[TestMethod]
public void CustomValidationErrorMessage()
{
var target = new TestTarget();
target.CustomValidationErrorMessageProperty.Value = "";
var errorMessage = target
.CustomValidationErrorMessageProperty
.GetErrors(nameof(TestTarget.CustomValidationErrorMessageProperty))
.Cast<string>()
.First();
errorMessage.Is("Custom validation error message for CustomValidationErrorMessageProperty");
}

[TestMethod]
public void CustomValidationErrorMessageWithDisplayName()
{
var target = new TestTarget();
target.CustomValidationErrorMessageWithDisplayNameProperty.Value = "";
var errorMessage = target
.CustomValidationErrorMessageWithDisplayNameProperty
.GetErrors(nameof(TestTarget.CustomValidationErrorMessageWithDisplayNameProperty))
.Cast<string>()
.First();
errorMessage.Is("Custom validation error message for CustomName");
}

[TestMethod]
public void CustomValidationErrorMessageWithResource()
{
var target = new TestTarget();
target.CustomValidationErrorMessageWithResourceProperty.Value = "";
var errorMessage = target
.CustomValidationErrorMessageWithResourceProperty
.GetErrors(nameof(TestTarget.CustomValidationErrorMessageWithResourceProperty))
.Cast<string>()
.First();
errorMessage.Is("Oops!? FromResource is required.");
}
}

internal class TestTarget
Expand All @@ -249,6 +290,17 @@ internal class TestTarget

public ReactiveProperty<string> TaskValidationTestProperty { get; private set; }

[Required(ErrorMessage = "Custom validation error message for {0}")]
public ReactiveProperty<string> CustomValidationErrorMessageProperty { get; }

[Required(ErrorMessage = "Custom validation error message for {0}")]
[Display(Name = "CustomName")]
public ReactiveProperty<string> CustomValidationErrorMessageWithDisplayNameProperty { get; }

[Required(ErrorMessageResourceType = typeof(Resource), ErrorMessageResourceName = nameof(Resource.ValidationErrorMessage))]
[Display(ResourceType = typeof(Resource), Name = nameof(Resource.ValidationTargetPropertyName))]
public ReactiveProperty<string> CustomValidationErrorMessageWithResourceProperty { get; }

public TestTarget()
{
RequiredProperty = new ReactiveProperty<string>()
Expand All @@ -267,6 +319,15 @@ public TestTarget()
}
return await Task.FromResult((string)null);
});

CustomValidationErrorMessageProperty = new ReactiveProperty<string>()
.SetValidateAttribute(() => CustomValidationErrorMessageProperty);

CustomValidationErrorMessageWithDisplayNameProperty = new ReactiveProperty<string>()
.SetValidateAttribute(() => CustomValidationErrorMessageWithDisplayNameProperty);

CustomValidationErrorMessageWithResourceProperty = new ReactiveProperty<string>()
.SetValidateAttribute(() => CustomValidationErrorMessageWithResourceProperty);
}
}
}