thoemmi / TinyLittleMvvm

A small MVVM library for WPF built on top of MahApps.Metro, supporting .NET Framework >= 4.7.2 and .NET Core >= 3
MIT License
131 stars 22 forks source link

GetErrorForProperty #2

Closed ghost closed 8 years ago

ghost commented 8 years ago

Hi @thoemmi

Was wondering can you add the GetErrorForProperty method to PropertyChangedBase

Like this https://github.com/OxyProgrammer/XPence/blob/master/WPF%20Client/XPence/XPence.Infrastructure/BaseClasses/ViewModelBase.cs

So I can use it in my project

protected override string GetErrorForProperty(string propertyName)
        {
            string error = null;
            switch (propertyName)
            {
                case "ICCID":
                    if (string.IsNullOrWhiteSpace(ICCID))
                        error = "ICCID is mandatory.";
                    else
                    {
                        if (null != ICCID && ICCID.Length > 20)
                            error = "Max 20 chars permitted.";
                    }
                    break;
                case "IMSI":
                    if (string.IsNullOrWhiteSpace(IMSI))
                        error = "ICCID is mandatory.";
                    else
                    {
                        if (null != IMSI && IMSI.Length > 15)
                            error = "Max 15 chars permitted.";
                    }
                    break;
                case "MSISDN":
                    if (string.IsNullOrWhiteSpace(MSISDN))
                        error = "MSISDN is mandatory.";
                    else
                    {
                        if (null != MSISDN && MSISDN.Length > 15)
                            error = "Max 15 chars permitted.";
                    }
                    break;
            }
            CommandManager.InvalidateRequerySuggested();
            return error ?? Entity[propertyName];
        }

If that makes any sense sorry still beginner dev

ghost commented 8 years ago

@thoemmi Please bring updates to TinyLittleMvvm Its very helpful

thoemmi commented 8 years ago

TinyLittleMvvm includes the class ValidationPropertyChangedBase, inherited from PropertyChangedBase. Does it provide the functionality you're asking for?

ghost commented 8 years ago

@thoemmi yes it does but not sure how it should work can you assist me,

My above method is a override method so in my SimcardViewModel.cs I should use ValidationPropertyChangedBase instead of PropertyChangeBase

Please see my file below and give me a helping hand SimCardViewModel.zip

ghost commented 8 years ago

Hi @thoemmi, did you manage to have a look at my SimCardViewModel

thoemmi commented 8 years ago

For an example of ValidationPropertyChangedBase see the demo application in this repository. Especially have a look at SampleDialogViewModel.cs.

Some notes about your SimCardViewModel: You can simplify your calls of NotifyOfPropertyChange as you can pass an expression too. i.e. instead of writing

NotifyOfPropertyChange(GetPropertyName(() => MSISDN));

call

NotifyOfPropertyChange(() => MSISDN);

And IsValid can be changed to

public bool IsValid { get { return !HasErrors; } }
ghost commented 8 years ago

@thoemmi Thank you so much sorry to pick your brain but below should I take this out?

/// <summary>
        /// Gets the error string for a property value against a property's name.
        /// </summary>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        protected override string OnErrorsChanged(string propertyName)
        {
            string error = null;
            switch (propertyName)
            {
                case "ICCID":
                    if (string.IsNullOrWhiteSpace(ICCID))
                        error = "ICCID is mandatory.";
                    else
                    {
                        if (null != ICCID && ICCID.Length > 20)
                            error = "Max 20 chars permitted.";
                    }
                    break;
                case "IMSI":
                    if (IMSI.Length > 15)
                        error = "Max 15 chars permitted.";
                    break;
                case "MSISDN":
                    if (MSISDN.Length > 15)
                        error = "Max 15 chars permitted.";
                    break;
            }
            CommandManager.InvalidateRequerySuggested();
            return error ?? Entity[propertyName];
        }
thoemmi commented 8 years ago

I don't think that your code is necessary. Instead, you can add your validation rules in the constructor of SimCardViewModel:

public SimCardViewModel()
{
    AddValidationRule(() => ICCID, text => !String.IsNullOrEmpty(text), "ICCID is mandatory.");
    AddValidationRule(() => ICCID, text => !String.IsNullOrEmpty(text) && text.Length <= 20, "Max 20 chars permitted.");
    AddValidationRule(() => IMSI, text => !String.IsNullOrEmpty(text) && text.Length <= 15, "Max 15 chars permitted.");
    AddValidationRule(() => MSISDN, text => !String.IsNullOrEmpty(text) && text.Length <= 15, "Max 15 chars permitted.");
}
ghost commented 8 years ago

@thoemmi Thank you I appreciate the help :+1: