/// <summary> /// Class Contructor /// </summary> /// <param name="value"></param> /// <param name="fieldName"></param> /// <param name="model"></param> /// <param name="validationMessageStore"></param> /// <param name="message"></param> public IntValidator(int value, string fieldName, object model, ValidationMessageStore validationMessageStore, string message) : base(value, fieldName, model, validationMessageStore, message) { }
private static void ValidateField(EditContext editContext, ValidationMessageStore messages, in FieldIdentifier fieldIdentifier)
protected override void OnInitialized() { _editContext = new EditContext(Article); _messages = new ValidationMessageStore(_editContext); base.OnInitialized(); }
private static async Task ValidateField(this ValidateForm editForm, EditContext editContext, ValidationMessageStore messages, FieldChangedEventArgs args) { // 获取验证消息 var validationResults = new List <ValidationResult>(); var validationContext = new ValidationContext(args.FieldIdentifier.Model) { MemberName = args.FieldIdentifier.FieldName, DisplayName = args.FieldIdentifier.GetDisplayName() }; await editForm.ValidateFieldAsync(validationContext, validationResults); messages.Clear(args.FieldIdentifier); messages.Add(args.FieldIdentifier, validationResults.Where(v => !string.IsNullOrEmpty(v.ErrorMessage)).Select(result => result.ErrorMessage !)); editContext.NotifyValidationStateChanged(); }
private void HandleFieldChanged(object sender, FieldChangedEventArgs e) { ValidationMessageStore.Clear(e.FieldIdentifier); }
void EditContextChanged() { MessageStore = new ValidationMessageStore(EditContext); ConnectEditContextEvents(); }
private static async Task ValidateModel(this ValidateForm editForm, EditContext?editContext, ValidationMessageStore messages) { if (editContext != null) { var validationContext = new ValidationContext(editContext.Model); var validationResults = new List <ValidationResult>(); await editForm.ValidateObject(validationContext, validationResults); messages.Clear(); foreach (var validationResult in validationResults.Where(v => !string.IsNullOrEmpty(v.ErrorMessage))) { foreach (var memberName in validationResult.MemberNames) { messages.Add(editContext.Field(memberName), validationResult.ErrorMessage !); } } editContext.NotifyValidationStateChanged(); } }
/// <summary> /// Class Contructor /// </summary> /// <param name="value"></param> /// <param name="fieldName"></param> /// <param name="model"></param> /// <param name="validationMessageStore"></param> /// <param name="message"></param> public DecimalValidator(decimal value, string fieldName, object model, ValidationMessageStore validationMessageStore, string message) : base(value, fieldName, model, validationMessageStore, message) { }
public abstract void Validate(ValidationMessageStore messages);
void ValidateField(EditContext editContext, ValidationMessageStore validationMessageStore, in FieldIdentifier fieldIdentifier)
protected override void OnInit() { if (CurrentEditContext == null) { throw new InvalidOperationException($"{nameof(FluentValidationValidator)} requires a cascading " + $"parameter of type {nameof(EditContext)}. For example, you can use {nameof(FluentValidationValidator)} " + $"inside an {nameof(EditForm)}."); } var validator = Validator ?? GetValidatorForModel(CurrentEditContext.Model); if (validator == null) { throw new InvalidOperationException($"{nameof(FluentValidationValidator)} requires either a " + $"parameter of type {nameof(IValidator)}, or validator for type {nameof(CurrentEditContext.Model)} " + $"should be reachable via reflection from the calling assembly."); } var messages = new ValidationMessageStore(CurrentEditContext); var modelName = CurrentEditContext.Model.GetType().FullName; // This field collects errors that are model-related but not specific to concrete property on the model var modelErrorField = new FieldIdentifier(CurrentEditContext.Model, string.Empty); var _event = _eventAggregator?.GetEvent <ValidationEvent>(); // This subscription fires when Submit button is pressed, // acts as a guard against not validated still-focused field formValidationSub = Observable.FromEventPattern <ValidationRequestedEventArgs>( handler => CurrentEditContext.OnValidationRequested += handler, handler => CurrentEditContext.OnValidationRequested -= handler) .Subscribe(e => { messages.Clear(); var validationResults = validator.Validate(CurrentEditContext.Model); if (validationResults.IsValid) { _event?.Publish(new ValidationEventArgs(true, modelName)); if (OnValidation.HasDelegate) { OnValidation.InvokeAsync(true); } } else { _event?.Publish(new ValidationEventArgs(false, modelName)); if (OnValidation.HasDelegate) { OnValidation.InvokeAsync(false); } foreach (var validationResult in validationResults.Errors) { messages.Add(CurrentEditContext.Field(validationResult.PropertyName), validationResult.ErrorMessage); } } CurrentEditContext.NotifyValidationStateChanged(); }); // This subscription fires every time an input field loses a focus, main workhorse fieldValidationSub = Observable.FromEventPattern <FieldChangedEventArgs>( handler => CurrentEditContext.OnFieldChanged += handler, handler => CurrentEditContext.OnFieldChanged -= handler) .Subscribe(e => { var validationResults = validator.Validate(CurrentEditContext.Model); if (validationResults.IsValid) { messages.Clear(); _event?.Publish(new ValidationEventArgs(true, modelName)); if (OnValidation.HasDelegate) { OnValidation.InvokeAsync(true); } } else { _event?.Publish(new ValidationEventArgs(false, modelName)); if (OnValidation.HasDelegate) { OnValidation.InvokeAsync(false); } messages.Clear(e.EventArgs.FieldIdentifier); messages.AddRange(e.EventArgs.FieldIdentifier, validationResults.Errors .Where(failure => failure.PropertyName == e.EventArgs.FieldIdentifier.FieldName) .Select(failure => failure.ErrorMessage)); // add errors that are not specific to field, e.g. complex rules messages.Clear(modelErrorField); messages.AddRange(modelErrorField, validationResults.Errors .Where(failure => failure.PropertyName == string.Empty) .Select(failure => failure.ErrorMessage)); } CurrentEditContext.NotifyValidationStateChanged(); }); }
void SetupEditContext() { ValidationMessageStore = new ValidationMessageStore(EditContext); HookUpEditContextEvents(); }
private protected virtual void ValidateField(EditContext editContext, ValidationMessageStore messages, in FieldIdentifier fieldIdentifier)
private void EditContextChanged() { ValidationMessageStore = new ValidationMessageStore(EditContext); HookUpEditContextEvents(); }
/// <summary> /// String Validation Extension /// </summary> /// <param name="value"></param> /// <param name="fieldName"></param> /// <param name="model"></param> /// <param name="validationMessageStore"></param> /// <param name="message"></param> /// <returns></returns> public static IntValidator Validation(this int value, string fieldName, object model, ValidationMessageStore validationMessageStore, string message = null) { var validation = new IntValidator(value, fieldName, model, validationMessageStore, message); return(validation); }
public DateTimeValidator(DateTime value, string fieldName, object model, ValidationMessageStore validationMessageStore, string message) : base(value, fieldName, model, validationMessageStore, message) { }
protected override void OnInitialized() { _messageStore = new ValidationMessageStore(CurrentEditContext); }
public void DissociateFromValidationMessageStore(ValidationMessageStore validationMessageStore) => _validationMessageStores?.Remove(validationMessageStore);