CommunityToolkit / CommunityToolkit/dotnet

ObservableValidator and inheritance

Open
#672 1 comment 0 reactions 0 assignees View on GitHub
bug :bug:
Dominant language
C#
Stars
3.8k
Forks
400
PR merge metrics
No merged PRs in 30d

Description

### Describe the bug

I noticed that if my VM is part of an inheritance hierarchy the validation doesn't work.

E.g. I have this structure:

ChildVM: BaseVM

BaseVM: ObservableObject

In my child VM, I call the Save method in the base VM. The base VM then calls ValidateAllProperties. The result is always false for HasErrors.

If I pull all the code up into the child VM it works ok, so I am confident the code itself is right.

Is this expected behaviour, and if so what do I need to make it work?

### Regression

_No response_

### Steps to reproduce

```text
public partial class BaseVm: ObservableValidator
{
[Required(AllowEmptyStrings = false, ErrorMessage = "Firstname required")]
[ObservableProperty]
public string _contact1Name;

public async Task SaveContacts()
{
bool result = false;

try
{
ValidateAllProperties();
if (HasErrors) // ** Always false **
{
//....
}
}
}

public partial class SupportVm : BaseVm
{
[RelayCommand]
public async Task NextButtonPressed()
{
bool result = await base.Save();
// ....
}
}
```

### Expected behavior

If the required property is blank an HasErrors should be true.

### Screenshots

_No response_

### IDE and version

VS 2022

### IDE version

Version 17.5.4

### Nuget packages

- [ ] CommunityToolkit.Common
- [ ] CommunityToolkit.Diagnostics
- [ ] CommunityToolkit.HighPerformance
- [X] CommunityToolkit.Mvvm (aka MVVM Toolkit)

### Nuget package version(s)

8.1.0

### Additional context

_No response_

### Help us help you

No, just wanted to report this

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.