diff --git a/src/Framework/Framework/ViewModel/Validation/ViewModelValidator.cs b/src/Framework/Framework/ViewModel/Validation/ViewModelValidator.cs index ad7e0388cd..24d50b2fdf 100644 --- a/src/Framework/Framework/ViewModel/Validation/ViewModelValidator.cs +++ b/src/Framework/Framework/ViewModel/Validation/ViewModelValidator.cs @@ -66,6 +66,8 @@ private IEnumerable ValidateViewModel(object? viewMode // validate all properties on the object var map = viewModelSerializationMapper.GetMap(viewModel.GetType()); + var dotvvmConfiguration = (DotvvmConfiguration)validationItems[typeof(DotvvmConfiguration)]!; + foreach (var property in map.Properties.Where(p => p.TransferToServer)) { var value = property.PropertyInfo.GetValue(viewModel); @@ -74,6 +76,7 @@ private IEnumerable ValidateViewModel(object? viewMode if (property.ValidationRules.Any()) { var context = new ValidationContext(viewModel, validationItems) { MemberName = property.Name }; + context.InitializeServiceProvider(dotvvmConfiguration.ServiceProvider.GetService); foreach (var rule in property.ValidationRules) { @@ -103,8 +106,11 @@ private IEnumerable ValidateViewModel(object? viewMode if (viewModel is IValidatableObject) { - foreach (var error in ((IValidatableObject)viewModel).Validate( - new ValidationContext(viewModel, validationItems))) + var validationContext = new ValidationContext(viewModel, validationItems); + validationContext.InitializeServiceProvider(dotvvmConfiguration.ServiceProvider.GetService); + var errors = ((IValidatableObject)viewModel).Validate(validationContext); + + foreach (var error in errors) { var paths = new List(); if (error.MemberNames != null) @@ -114,6 +120,7 @@ private IEnumerable ValidateViewModel(object? viewMode paths.Add(memberPath); } } + if (!paths.Any()) { paths.Add(string.Empty);