Skip to content

Commit 94aa3f6

Browse files
authored
Merge pull request #8 from magahl/fix-validate-error-on-nested
Validating whole model every time
2 parents d6d935e + 069d808 commit 94aa3f6

File tree

4 files changed

+19
-29
lines changed

4 files changed

+19
-29
lines changed

TabBlazor.FluentValidation.Server/Pages/AsyncForm.razor

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
<InputText class="form-control" @bind-Value="Model.Directory"></InputText>
1919
<ValidationMessage For="(() => Model.Directory)"></ValidationMessage>
2020
</div>
21+
<div class="mb-3">
22+
<label class="form-label">Person Name</label>
23+
<InputText class="form-control" @bind-Value="Model.Owner.Name"></InputText>
24+
<ValidationMessage For="(() => Model.Owner.Name)"></ValidationMessage>
25+
</div>
2126

2227
<Button Type="ButtonType.Button" Text="Save" class="mt-1" OnClick="Save"></Button>
2328
</TablerForm>

TabBlazor.FluentValidation.Server/Pages/AsyncForm.razor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public partial class AsyncForm
1515

1616
protected override void OnInitialized()
1717
{
18-
Model = new Document() { Name = "Magnus", Directory = "C:/Test" };
18+
Model = new Document() { Name = "Magnus", Directory = "C:/Test", Owner = new Person() { Name = "Kalle" } };
1919
}
2020

2121
private async Task Save()

TabBlazor.FluentValidation.Server/Validation/Document.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ public class Document
66
{
77
public string Name { get; set; }
88
public string Directory { get; set; }
9+
10+
public Person Owner { get; set; }
911
}
1012

1113
public class DocumentValidator : AbstractValidator<Document>
@@ -25,5 +27,8 @@ public DocumentValidator()
2527
return result;
2628
})
2729
.WithMessage("Directory must be C:/Temp");
30+
31+
RuleFor(x => x.Owner)
32+
.SetValidator(new PersonValidator());
2833
}
2934
}

src/TabBlazor.FluentValidation/FluentValidationSubscription.cs

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ public class FluentValidationSubscription : IDisposable
1919
private readonly ValidationMessageStore messages;
2020

2121
public FluentValidationSubscription(
22-
EditContext editContext,
22+
EditContext editContext,
2323
IServiceProvider serviceProvider,
24-
bool disableAssemblyScanning,
25-
IValidator validator,
24+
bool disableAssemblyScanning,
25+
IValidator validator,
2626
FluentValidationValidator fluentValidationValidator)
2727
{
2828
this.editContext = editContext;
@@ -33,11 +33,11 @@ public FluentValidationSubscription(
3333

3434
messages = new ValidationMessageStore(editContext);
3535

36-
editContext.OnValidationRequested += ValidateModel;
37-
editContext.OnFieldChanged += ValidateField;
36+
editContext.OnFieldChanged += (_, _) => ValidateModel();
37+
editContext.OnValidationRequested += (_, _) => ValidateModel();
3838
}
3939

40-
private async void ValidateModel(object sender, ValidationRequestedEventArgs e)
40+
private async void ValidateModel()
4141
{
4242
validator ??= GetValidatorForModel(serviceProvider, editContext.Model, disableAssemblyScanning);
4343

@@ -75,26 +75,6 @@ private async void ValidateModel(object sender, ValidationRequestedEventArgs e)
7575
}
7676
}
7777

78-
private async void ValidateField(object sender, FieldChangedEventArgs e)
79-
{
80-
var fieldIdentifier = e.FieldIdentifier;
81-
var properties = new[] { fieldIdentifier.FieldName };
82-
var context = new ValidationContext<object>(fieldIdentifier.Model, new PropertyChain(),
83-
new MemberNameValidatorSelector(properties));
84-
85-
validator ??= GetValidatorForModel(serviceProvider, fieldIdentifier.Model, disableAssemblyScanning);
86-
87-
if (validator is not null)
88-
{
89-
var validationResults = await validator.ValidateAsync(context);
90-
91-
messages.Clear(fieldIdentifier);
92-
messages.Add(fieldIdentifier, validationResults.Errors.Select(error => error.ErrorMessage));
93-
94-
editContext.NotifyValidationStateChanged();
95-
}
96-
}
97-
9878
private IValidator GetValidatorForModel(IServiceProvider serviceProvider, object model,
9979
bool disableAssemblyScanning)
10080
{
@@ -233,8 +213,8 @@ private FieldIdentifier ToFieldIdentifier(in EditContext editContext, in string
233213

234214
public void Dispose()
235215
{
236-
editContext.OnFieldChanged -= ValidateField;
237-
editContext.OnValidationRequested -= ValidateModel;
216+
editContext.OnFieldChanged -= (_, _) => ValidateModel();
217+
editContext.OnValidationRequested -= (_, _) => ValidateModel();
238218
messages.Clear();
239219
editContext.NotifyValidationStateChanged();
240220
}

0 commit comments

Comments
 (0)