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

System.ArgumentException: ''The Password field is required.' is not a valid value for property 'ErrorStr'.' #1004

Closed
jhm-ciberman opened this issue Oct 4, 2021 · 4 comments
Assignees
Labels
👌 accepted New feature or request has be accepted, just wait

Comments

@jhm-ciberman
Copy link

Describe the bug

When you implement the MVVM pattern, is usual to implement the interface INotifyDataErrorInfo.

public interface INotifyDataErrorInfo
{
    bool HasErrors { get; }

    event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;

    IEnumerable GetErrors(string propertyName);
}

This interface returns the error of the ViewModel as a non generic IEnumerable. By definition, this IEnumerable can contain ANY object, not just strings. For example, the Library Microsoft.Toolkit.Mvvm implements the interface in the following way:

//
// Summary:
//     A base class for objects implementing the System.ComponentModel.INotifyDataErrorInfo
//     interface. This class also inherits from Microsoft.Toolkit.Mvvm.ComponentModel.ObservableObject,
//     so it can be used for observable items too.
public abstract class ObservableValidator : ObservableObject, INotifyDataErrorInfo
{
    public bool HasErrors { get; }

    public event EventHandler<DataErrorsChangedEventArgs>? ErrorsChanged;

    public IEnumerable<ValidationResult> GetErrors(string? propertyName = null);
    
    // Other methods for setting properties and validation
    // [ ... ]
    protected void ValidateAllProperties();

    protected internal void ValidateProperty(object? value, [CallerMemberName] string? propertyName = null);
}

This library returns an enumerable of System.ComponentModel.DataAnnotations.ValidationResult. But it could be any object.
The problem is that in the newer version of HandyControl, all input fields support Validation, but they assume the contents of the IEnumerable are strings:

isError = Validation.GetHasError(this);
if (isError)
{
SetCurrentValue(ErrorStrProperty, Validation.GetErrors(this)[0].ErrorContent);
}

So, this exception is thrown, because System.ComponentModel.DataAnnotations.ValidationResult is not a string.

System.ArgumentException: ''The Password field is required.' is not a valid value for property 'ErrorStr'.'

The correct way, would be:

isError = Validation.GetHasError(this);
if (isError)
{
    SetCurrentValue(ErrorStrProperty, Validation.GetErrors(this)[0].ErrorContent?.ToString());
}

To Reproduce
Implement INotifyDataErrorInfo with GetErrors returning an IEnumerable of custom error objects instead of strings and an exception will be thrown.

Environment (please complete the following information):

  • .net: net5
  • IDE vs2019
  • Version HandyControls 3.3.8 (I use the forked version, but I think this bug report goes here)
@NaBian
Copy link
Member

NaBian commented Oct 12, 2021

I'll deal with it after work on the 13th

@NaBian NaBian added the 👌 accepted New feature or request has be accepted, just wait label Oct 12, 2021
@jhm-ciberman
Copy link
Author

There is no hurry. There is at least one month until we release our software. Thanks for all the amazing work with HandyControl 🎉♥️🙏

@NaBian NaBian closed this as completed in cf8cd2d Oct 13, 2021
@NaBian
Copy link
Member

NaBian commented Oct 13, 2021

If you have a problem next time, you can push a pr without waiting for me. HC is everyone's, not mine.

@jhm-ciberman
Copy link
Author

Thank you a lot! I will!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
👌 accepted New feature or request has be accepted, just wait
Projects
None yet
Development

No branches or pull requests

2 participants