Skip to content

Commit

Permalink
Initialized service provider in validation context.
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalTichy authored and tomasherceg committed Oct 10, 2024
1 parent b6cc7e8 commit 6d3fc11
Showing 1 changed file with 9 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ private IEnumerable<ViewModelValidationError> 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);
Expand All @@ -74,6 +76,7 @@ private IEnumerable<ViewModelValidationError> 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)
{
Expand Down Expand Up @@ -103,8 +106,11 @@ private IEnumerable<ViewModelValidationError> 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<string>();
if (error.MemberNames != null)
Expand All @@ -114,6 +120,7 @@ private IEnumerable<ViewModelValidationError> ValidateViewModel(object? viewMode
paths.Add(memberPath);
}
}

if (!paths.Any())
{
paths.Add(string.Empty);
Expand Down

0 comments on commit 6d3fc11

Please sign in to comment.