/// <summary> /// Validate the whole form and trigger client UI update. /// </summary> /// <param name="editContext"></param> /// <param name="messages"></param> private async void ValidateModel(EditContext editContext, ValidationMessageStore messages) { // <EditForm> should now be able to run async validations: // https://github.com/dotnet/aspnetcore/issues/11914 var validationResults = await TryValidateModel(editContext); messages.Clear(); var graph = new ModelGraphCache(editContext.Model); foreach (var error in validationResults.Errors) { var(propertyValue, propertyName) = graph.EvalObjectProperty(error.PropertyName); // while it is impossible to have a validation error for a null child property, better be safe than sorry... if (propertyValue != null) { var fieldID = new FieldIdentifier(propertyValue, propertyName); messages.Add(fieldID, error.ErrorMessage); } } editContext.NotifyValidationStateChanged(); }
/// <summary> /// Validate the whole form and trigger client UI update. /// </summary> /// <param name="editContext"></param> /// <param name="messages"></param> private void ValidateModel(EditContext editContext, ValidationMessageStore messages) { // WARNING: DO NOT USE Async Void + ValidateAsync here // Explanation: Blazor UI will get VERY BUGGY for some reason if you do that. (Field CSS lagged behind validation) var validationResults = TryValidateModel(editContext); messages.Clear(); var graph = new ModelGraphCache(editContext.Model); foreach (var error in validationResults.Errors) { var(propertyValue, propertyName) = graph.EvalObjectProperty(error.PropertyName); // while it is impossible to have a validation error for a null child property, better be safe than sorry... if (propertyValue != null) { var fieldID = new FieldIdentifier(propertyValue, propertyName); messages.Add(fieldID, error.ErrorMessage); } } editContext.NotifyValidationStateChanged(); }