wpfcaliburn.microsyncfusionfluentvalidationinotifydataerrorinfo

Need to expose HasError property from ValidationBaseClass to view Model with fluent validation


I need help implementing INotifyDataErrorInfo interface with SfTextBoxEx placed inside SfTextInputLayout.

WPF with MVVM using Caliburn.Micro framework.

Please find following project structure to understand by question.

1) FolderName : Infrastructure File : ValidatedPropertyChangedBase.cs

--It contains implementation for INotifyDataErrorInfo.

2) FolderName : Model File : TestModel.cs --Contains two properties with inherited from ValidatedPropertyChangedBase --Also contains the class for Validation of properties using FluentValidation

4) FolderName : View File : HomeView.xaml --Standard window with two SfTextInputLayout with SfTextBoxExt

4) FolderName : ViewModel File : HomeViewModel.cs --standard view model with Model as property with Validation triggering. --Here if i put breakpoint on validation method i can see that error returned is correct but somehow it is not triggering the UI to show the error message. --I've already set ValidatesOnNotifyDataError=true while binding the property with textbox.

Please check and guide me if something is wrong. NOTE : I am using syncfusion controls but I tried with standard text box also, it is not triggering the errorchanged event.

Also I've used IntoifyDataError implementation from: https://www.thetechgrandma.com/2017/05/wpf-prism-inotifydataerrorinfo-and.html

--ValidatedPropertyChangedBase

public abstract class ValidatedPropertyChangedBase : PropertyChangedBase, INotifyDataErrorInfo
{
    private readonly Dictionary<string, List<string>> _errors = new Dictionary<string, List<string>>();

    


    public void SetError(string propertyName, string errorMessage)
    {
        if (!_errors.ContainsKey(propertyName))
            _errors.Add(propertyName, new List<string> { errorMessage });

        RaiseErrorsChanged(propertyName);
    }

    protected void ClearError(string propertyName)
    {
        if (_errors.ContainsKey(propertyName))
            _errors.Remove(propertyName);

        RaiseErrorsChanged(propertyName);
    }

    protected void ClearAllErrors()
    {
        var errors = _errors.Select(error => error.Key).ToList();

        foreach (var propertyName in errors)
            ClearError(propertyName);
    }
    public void RaiseErrorsChanged(string propertyName)
    {
        ErrorsChanged(this, new DataErrorsChangedEventArgs(propertyName));
    }

    public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged = delegate { return; };


    public bool HasErrors
    {
        get { return _errors.Any(x => x.Value != null && x.Value.Count > 0); }
    }
    public IEnumerable GetErrors(string propertyName)
    {
        if (String.IsNullOrEmpty(propertyName) ||
           !_errors.ContainsKey(propertyName)) return null;
        return _errors[propertyName];
    }

}

--ViewModel

public class HomeViewModel : ValidatedPropertyChangedBase
{

    private TestModel _model;
    public TestModel Model
    {
        get { return _model; }
        set
        {
            if (_model != value)
            {
                _model = value;
                NotifyOfPropertyChange(() => Model);
            }
        }
    }

    public HomeViewModel()
    {
        Model = new TestModel();
    }

    public void ValidateData()
    {
        ClearAllErrors();
        var validator = new TestModelValidation();
        FluentValidation.Results.ValidationResult result = validator.Validate(Model);
        foreach (var error in result.Errors)
        {
            SetError(error.PropertyName, error.ErrorMessage);
        }

        if (result.IsValid)
        {
            MessageBox.Show("Data good to save !");
        }

    }
}

--View

<Window
x:Class="ValidationDemo.Views.HomeView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:convertor="clr-namespace:ValidationDemo.Infrastructure"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:ValidationDemo.Views"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:sf="http://schemas.syncfusion.com/wpf"
Title="HomeView"
Width="800"
Height="450"
mc:Ignorable="d">
<Window.Resources>
    <Style
        TargetType="sf:SfTextInputLayout">
        <Setter Property="Width" Value="200" />
    </Style>
</Window.Resources>
<StackPanel
    HorizontalAlignment="Center"
    VerticalAlignment="Center"
    Orientation="Vertical">
    <sf:SfTextInputLayout
        Hint="First Name">
        <sf:SfTextBoxExt
            Text="{Binding Path=Model.FirstName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnNotifyDataErrors=True}" />
    </sf:SfTextInputLayout>
    <sf:SfTextInputLayout
        Hint="Last Name">
        <sf:SfTextBoxExt
            Text="{Binding Path=Model.FirstName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnNotifyDataErrors=True}" />
    </sf:SfTextInputLayout>
    <TextBox
            Text="{Binding Path=Model.FirstName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnNotifyDataErrors=True}" />
    <TextBox
            Text="{Binding Path=Model.FirstName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnNotifyDataErrors=True}" />
    <Button
        x:Name="ValidateData"
        Content="Validate Data" />
</StackPanel>

--Model with Fluent validation implemented:

public class TestModel : ValidatedPropertyChangedBase
{

    private string _firstName;
    public string FirstName
    {
        get { return _firstName; }
        set
        {
            if (_firstName != value)
            {
                _firstName = value;
                NotifyOfPropertyChange(() => FirstName);
            }
        }
    }

    private string _lastName;
    public string LastName
    {
        get { return _lastName; }
        set
        {
            if (_lastName != value)
            {
                _lastName = value;
                NotifyOfPropertyChange(() => LastName);
            }
        }
    }
}

public class TestModelValidation:AbstractValidator<TestModel>
{
    public TestModelValidation()
    {
        RuleFor(t => t.FirstName)
            .NotEmpty()
            .WithMessage("Please enter first name");

        RuleFor(t => t.FirstName)
            .NotEmpty()
            .WithMessage("Please enter last name");
    }
}

Solution

  • Was able to figure out the problem, was with control exposing the HasError from view Model.

    ErrorText="{Binding RelativeSource={RelativeSource Mode=Self}, Path=InputView.(Validation.Errors), Converter={StaticResource EC}}"
            HasError="{Binding RelativeSource={RelativeSource Mode=Self}, Path=InputView.(Validation.Errors), Converter={StaticResource ECO}}"
    

    Added the properties on the control with convertors and it fixed the issue.