pglazkov / MvvmValidation

Lightweight library that helps reduce boilerplate when implementing validation in XAML MVVM applications
MIT License
167 stars 31 forks source link

How to do Validation during Propoerty Set #2

Closed disklosr closed 8 years ago

disklosr commented 8 years ago

I'm interested in using this library as an implementation for INotifyDataErrorInfo interface. And I want to run validation each time I set a propoerty. Problem is that the Validate() method in this library expects an expression as an input, while I need an overload of it that accepts a string instead. This prevents me from using it inside my generic setter:

protected virtual void SetValue<T>(ref T field, T val, [CallerMemberName] string propertyName = null, Action onValueChanged = null)
{
    if (EqualityComparer<T>.Default.Equals(field, val))
        return;

    field = val;
    RaisePropertyChanged(propertyName);
    //Need to add validator.Validate(propertyName) here but can't because my param is a string
    onValueChanged?.Invoke();
}

Do you have any workaround for this or another approach of executing validation during set? Thanks.

pglazkov commented 8 years ago

There is an overload of the Validate method that takes object as the argument. You can use that. This code should compile:

validator.Validate(propertyName);
disklosr commented 8 years ago

Great, exactly what I need. I also noticed the NotifyDataErrorInfoAdapter type isn't included in the nuget package. When are you planning to update it?

pglazkov commented 8 years ago

It is included in the portable version of the library, but for some reason NuGet downloads the .NET 4.0 version that doesn't include it (because .NET 4.0 doesn't have the INotifyDataErrorInfo interface). I will look into that.

In the meanwhile you could download the binaries of the library from CodePlex and reference the portable library manually without using NuGet. I will try to update NuGet packages ASAP.

Sorry for the inconvenience and thanks for your feedback!

disklosr commented 8 years ago

I'll wait for you to update the nuget package as I don't like referencing binaries directly.

Let me know when you update it. Thanks a lot for your quick support.

pglazkov commented 8 years ago

Fixed the NuGet package for .NET 4.5. You can try it by installing a pre-release version from NuGet: https://www.nuget.org/packages/MvvmValidation/2.1.0-beta