private void ValidateModel() { var model = CurrentEditContext.Model; var vc = new ValidationContext(model); var vrs = new List <ValidationResult>(); MessageStore.Clear(); Validator.TryValidateObject(model, vc, vrs, true); foreach (var vr in vrs) { if (!vr.MemberNames.Any()) { MessageStore.Add(new FieldIdentifier(model, string.Empty), vr.ErrorMessage); } else { foreach (var mn in vr.MemberNames) { MessageStore.Add(new FieldIdentifier(model, mn), vr.ErrorMessage); } } } CurrentEditContext.NotifyValidationStateChanged(); }
private async void ValidationRequested(object sender, ValidationRequestedEventArgs args) { MessageStore.Clear(); var validationContext = new ValidationContext <object>(EditContext.Model); ValidationResult result = await Validator.ValidateAsync(validationContext); AddValidationResult(EditContext.Model, result); }
private void ValidateField(FieldIdentifier fieldIdentifier) { var model = fieldIdentifier.Model; var allProperties = model.GetType().GetProperties(); var previousValidationMessages = allProperties.ToDictionary( p => p.Name, p => CurrentEditContext.GetValidationMessages(new FieldIdentifier(model, p.Name)).ToArray()); MessageStore.Clear(); foreach (var prop in allProperties) { var vrs = new List <ValidationResult>(); var vc = new ValidationContext(model) { MemberName = prop.Name }; var fi = new FieldIdentifier(model, prop.Name); var compareAttr = GetCompareAttrOrNull(prop); if (prop.Name == fieldIdentifier.FieldName) { if (!Validator.TryValidateProperty(prop.GetValue(model), vc, vrs)) { MessageStore.Add(fi, vrs.Select(r => r.ErrorMessage)); } } else if (compareAttr != null && compareAttr.OtherPropertyName == fieldIdentifier.FieldName) { var compareAttrMessage = compareAttr.GetErrorMessage(model, vc); var prevMessagesForField = previousValidationMessages[prop.Name].Where(vm => !vm.EqualsInvariant(compareAttrMessage)).ToArray(); MessageStore.Add(fi, prevMessagesForField); if (!Validator.TryValidateValue(prop.GetValue(model), vc, vrs, new[] { compareAttr })) { MessageStore.Add(fi, vrs.Select(r => r.ErrorMessage)); } } else { MessageStore.Add(fi, previousValidationMessages[prop.Name]); } } CurrentEditContext.NotifyValidationStateChanged(); }
private async void FieldChanged(object sender, FieldChangedEventArgs args) { FieldIdentifier fieldIdentifier = args.FieldIdentifier; MessageStore.Clear(fieldIdentifier); var propertiesToValidate = new[] { fieldIdentifier.FieldName }; var fluentValidationContext = new ValidationContext <object>( instanceToValidate: fieldIdentifier.Model, propertyChain: new FluentValidation.Internal.PropertyChain(), validatorSelector: new FluentValidation.Internal.MemberNameValidatorSelector(propertiesToValidate) ); ValidationResult result = await Validator.ValidateAsync(fluentValidationContext); AddValidationResult(fieldIdentifier.Model, result); }
public virtual void CloseConnection(string clientUid) { Socket.Disconnect(clientUid); Socket.JoinDisconnect(clientUid); _messageStore.Clear(clientUid); }