Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows 10 (Universal Windows App) data validation

I was trying to figure out how to do the data validation under UWP, but according to what I have found out, there is basically nothing I can implemented yet.

Due to that I tried to implement my custom validation logic. Problem I have now is, that I am showing error information on one TextBlock rather than directly under the specific TextBox which contains data error.

This is what I do at the moment:

public class Customer : ViewModel
{
    private string _Name = default(string);
    public string Name { get { return _Name; } set { SetProperty(ref _Name, value); OnPropertyChanged("IsValid"); } }


    private string _Surname = default(string);
    public string Surname { get { return _Surname; } set { SetProperty(ref _Surname, value); OnPropertyChanged("IsValid"); } }

    private DateTime _DateOfBirth = default(DateTime);
    public DateTime DateOfBirth { get { return _DateOfBirth; } set { SetProperty(ref _DateOfBirth, value); OnPropertyChanged("IsValid"); } }

    public int ID { get; set; }

    public bool IsValid
    {
        get
        {
            //restart error info
            _ErrorInfo = default(string);
            if (string.IsNullOrWhiteSpace(Name))
                _ErrorInfo += "Name cannot be empty!" + Environment.NewLine;

            if (string.IsNullOrWhiteSpace(Surname))
                _ErrorInfo += "Surname cannot be empty!" + Environment.NewLine;

            //raise property changed
            OnPropertyChanged("ErrorInfo");

            return !string.IsNullOrWhiteSpace(Name) &&
                !string.IsNullOrWhiteSpace(Surname);
        }
    }


    private string _ErrorInfo = default(string);
    public string ErrorInfo { get { return _ErrorInfo; } set { SetProperty(ref _ErrorInfo, value); } }

}

Question:

How to adjust my code, so that rather than having one label with all error information, I can assign label under each textbox and display validation error there? Should I use Dictionary for this? If yes, how can I bind it to my View?

like image 830
Robert J. Avatar asked Dec 25 '22 13:12

Robert J.


1 Answers

I have quickly become a fan of using Prism, see this wonderful demonstration User input validation with Prism and data annotations on the UWP.

Its better than anything I could type here.

like image 198
Saxar Avatar answered Dec 27 '22 20:12

Saxar