<commit_before>﻿// -----------------------------------------------------------------------
// <copyright file="ValidatingViewModelBase.cs" company="">
// Copyright 2016 <NAME>
// </copyright>
// -----------------------------------------------------------------------

using FluentValidation;
using FluentValidation.Results;
using ReactiveUI;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;

namespace QDMSServer.ViewModels
{
    public abstract class ValidatingViewModelBase<T> : ReactiveObject, INotifyDataErrorInfo where T : class
    {
        public T Model { get; }
        private readonly AbstractValidator<T> _validator;
        private Dictionary<string, List<string>> _errors = new Dictionary<string, List<string>>();
        private bool _hasErrors;

        protected ValidatingViewModelBase(T model, AbstractValidator<T> validator)
        {
            Model = model;
            _validator = validator;

            //we have to use pass-through properties for validation to work...
            //the issue is that INotifyPropertyChanged and INotifyDataErrorInfo must be in the same class
            //and I don't want to saddle the models with validating themselves...
            //it would require an additional library reference in QDMSClient, etc.
            this.PropertyChanged += (s, e) => Validate(e.PropertyName);
        }

        protected void Validate(string propertyName = null)
        {
            if (string.IsNullOrEmpty(propertyName))
            {
                ValidateObject();
            }
            else
            {
                ValidateProperty(propertyName);
            }
        }

        /// <summary>
        /// Validate only a particular property
        /// </summary>
        /// <param name="propertyName"></param>
        private void ValidateProperty(string propertyName)
        {
            var result = _validator.Validate(Model, propertyName);
            if (!_errors.ContainsKey(propertyName)) _errors.Add(propertyName, new List<string>());

            var newErrors = result.Errors.Select(x => x.ErrorMessage).ToList();
            var notify = newErrors.Intersect(_errors[propertyName]).Count() != newErrors.Count;
            _errors[propertyName] = newErrors;
            HasErrors = _errors.Any(x => x.Value.Count > 0);

            if (notify)
            {
                ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(propertyName));
            }
        }

        /// <summary>
        /// Validate the entire object
        /// </summary>
        private void ValidateObject()
        {
            var result = _validator.Validate(Model);
            HasErrors = result.IsValid;

            var newErrors = GetErrorDict(result.Errors);
            var oldErrors = _errors;
            _errors = newErrors;
            NotifyChangedErrors(oldErrors, newErrors);
        }
<commit_msg>Fixed ValidatingViewModelBase not updating HasErrors on validating entire object<commit_after>﻿// -----------------------------------------------------------------------
// <copyright file="ValidatingViewModelBase.cs" company="">
// Copyright 2016 <NAME>
// </copyright>
// -----------------------------------------------------------------------

using FluentValidation;
using FluentValidation.Results;
using ReactiveUI;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;

namespace QDMSServer.ViewModels
{
    public abstract class ValidatingViewModelBase<T> : ReactiveObject, INotifyDataErrorInfo where T : class
    {
        public T Model { get; }
        private readonly AbstractValidator<T> _validator;
        private Dictionary<string, List<string>> _errors = new Dictionary<string, List<string>>();
        private bool _hasErrors;

        protected ValidatingViewModelBase(T model, AbstractValidator<T> validator)
        {
            Model = model;
            _validator = validator;

            //we have to use pass-through properties for validation to work...
            //the issue is that INotifyPropertyChanged and INotifyDataErrorInfo must be in the same class
            //and I don't want to saddle the models with validating themselves...
            //it would require an additional library reference in QDMSClient, etc.
            this.PropertyChanged += (s, e) => Validate(e.PropertyName);
        }

        protected void Validate(string propertyName = null)
        {
            if (string.IsNullOrEmpty(propertyName))
            {
                ValidateObject();
            }
            else
            {
                ValidateProperty(propertyName);
            }
        }

        /// <summary>
        /// Validate only a particular property
        /// </summary>
        /// <param name="propertyName"></param>
        private void ValidateProperty(string propertyName)
        {
            var result = _validator.Validate(Model, propertyName);
            if (!_errors.ContainsKey(propertyName)) _errors.Add(propertyName, new List<string>());

            var newErrors = result.Errors.Select(x => x.ErrorMessage).ToList();
            var notify = newErrors.Intersect(_errors[propertyName]).Count() != newErrors.Count;
            _errors[propertyName] = newErrors;
            HasErrors = _errors.Any(x => x.Value.Count > 0);

            if (notify)
            {
                ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(propertyName));
            }
        }

        /// <summary>
        /// Validate the entire object
        /// </summary>
        private void ValidateObject()
        {
            var result = _validator.Validate(Model);
            HasErrors = result.IsValid;

            var newErrors = GetErrorDict(result.Errors);
            var oldErrors = _errors;
            _errors = newErrors;
            NotifyChangedErrors(oldErrors, newErrors);
            HasErrors = _errors.Any(x => x.Value.Count > 0);
        }

{'fraction_non_alphanumeric': 0.13855820105820105, 'fraction_numerical': 0.0018187830687830687, 'mean_word_length': 1.8519566242338519, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '363797', 'n_tokens_mistral': 1434, 'n_tokens_neox': 1423, 'n_words': 382}