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

[BUG] Validation by property "error sharing" when last property is the same #60

Closed
ins0mniaque opened this issue Jan 31, 2020 · 1 comment
Labels
bug Something isn't working

Comments

@ins0mniaque
Copy link

Describe the bug

Using 2 validation rules ending with the same property name, e.g.

this.ValidationRule(
            viewModel => viewModel.Origin.Name,
            name => !string.IsNullOrWhiteSpace(name),
            "You must specify a valid origin name");

this.ValidationRule(
            viewModel => viewModel.Destination.Name,
            name => !string.IsNullOrWhiteSpace(name),
            "You must specify a valid destination name");

and then

this.BindValidation(ViewModel, vm => vm.Origin.Name, view => view.OriginNameError.Text);
this.BindValidation(ViewModel, vm => vm.Destination.Name, view => view.DestinationNameError.Text);

results in view.OriginNameError and view.DestinationNameError having all "*.Name" errors.

Steps To Reproduce

Use example mentioned before.

Expected behavior

Errors with the same last property name should not "share" errors.

Screenshots

Environment

  • OS: Windows / .NET Core 3.0
  • Device: Windows
  • Version: commit aa218ee
  • Working Version: None

but the bug is present on all platforms.

Additional context

I have fixed this bug by replacing all instances of "Body.GetMemberInfo().Name" in the code by "Body.GetPropertyPath()", which returns "Origin.Name" and "Destination.Name" instead of only the "Name".

I know you might be in the process of refactoring property validation, but if you are ready, I'll make a pull request.

GetPropertyPath code:

// <copyright file="ReactiveUI.Validation/src/ReactiveUI.Validation/Extensions/ExpressionExtensions.cs" company=".NET Foundation">
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
// </copyright>

using System.Linq.Expressions;
using System.Text;

namespace ReactiveUI.Validation.Extensions
{
    /// <summary>
    /// Extensions methods associated to <see cref="Expression"/> instances.
    /// </summary>
    public static class ExpressionExtensions
    {
        /// <summary>
        /// Returns a property path expression as a string.
        /// </summary>
        /// <param name="expression">The property path expression.</param>
        /// <returns>The property path string representing the expression.</returns>
        public static string GetPropertyPath(this Expression expression)
        {
            var path = new StringBuilder();
            while (expression is MemberExpression memberExpression)
            {
                if (path.Length > 0)
                    path.Insert(0, '.');

                path.Insert(0, memberExpression.Member.Name);

                expression = memberExpression.Expression;
            }
            return path.ToString();
        }
    }
}
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 25, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant