Skip to content

Commit b1f4999

Browse files
committed
Added support for invalid property paths like Rows[0] - stores them as model errors
1 parent 52fac03 commit b1f4999

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

vNext.BlazorComponents.FluentValidation/FluentValidationValidator.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
using Microsoft.Extensions.Logging;
1212
using System;
1313
using System.Collections.Generic;
14+
using System.Globalization;
1415
using System.Linq;
1516
using System.Linq.Expressions;
17+
using System.Threading;
1618
using System.Threading.Tasks;
1719

1820
namespace vNext.BlazorComponents.FluentValidation
@@ -115,7 +117,6 @@ protected virtual async Task<ValidationResult> ValidateModel(ValidationMessageSt
115117
{
116118
ValidationContext<object> context = CreateValidationContext(validator);
117119

118-
119120
Task<ValidationResult> validateAsyncTask = validator.ValidateAsync(context);
120121
if (updateValidationState)
121122
{
@@ -128,15 +129,18 @@ protected virtual async Task<ValidationResult> ValidateModel(ValidationMessageSt
128129
messages.Clear();
129130
foreach (var failure in validationResults.Errors.Where(f => f.Severity <= Severity))
130131
{
132+
string errorMessage = MapValidationFailureToMessage(failure, validationResults, context);
133+
ILogger<FluentValidationValidator>? logger = null;
131134
try
132135
{
133136
var fieldIdentifier = ToFieldIdentifier(EditContext, failure.PropertyName);
134-
string errorMessage = MapValidationFailureToMessage(failure, validationResults, context);
135137
messages.Add(fieldIdentifier, errorMessage);
136138
}
137139
catch (InvalidOperationException ex)
138140
{
139-
ServiceProvider.GetService<ILogger<FluentValidationValidator>>()?.LogError(ex, $"An error occured while parsing ValidationFailure(PropertyName={failure.PropertyName})");
141+
logger ??= ServiceProvider.GetService<ILogger<FluentValidationValidator>>();
142+
logger?.LogError(ex, $"An error occured while parsing ValidationFailure(PropertyName={failure.PropertyName})");
143+
messages.Add(new FieldIdentifier(context.InstanceToValidate, string.Empty), errorMessage);
140144
}
141145
}
142146

@@ -220,7 +224,7 @@ protected static FieldIdentifier ToFieldIdentifier(EditContext editContext, stri
220224
while (true)
221225
{
222226
var nextTokenEnd = propertyPath.IndexOfAny(Separators);
223-
if (nextTokenEnd < 0)
227+
if (nextTokenEnd <= 0)
224228
{
225229
return new FieldIdentifier(obj, propertyPath);
226230
}
@@ -242,7 +246,7 @@ protected static FieldIdentifier ToFieldIdentifier(EditContext editContext, stri
242246
{
243247
// we've got an Item property
244248
var indexerType = prop.GetIndexParameters()[0].ParameterType;
245-
var indexerValue = Convert.ChangeType(nextToken, indexerType);
249+
var indexerValue = Convert.ChangeType(nextToken, indexerType, CultureInfo.InvariantCulture);
246250
newObj = prop.GetValue(obj, new object[] { indexerValue });
247251
}
248252
else if (obj is IEnumerable<object> objEnumerable && int.TryParse(nextToken, out int indexerValue)) //e.g. hashset

0 commit comments

Comments
 (0)