protected override void OnInitialized() { base.OnInitialized(); _editContext = new EditContext(Model); if (FormProvider != null) { FormProvider.AddForm(this); } if (OnFieldChanged.HasDelegate) { _editContext.OnFieldChanged += OnFieldChangedHandler; } if (OnValidationRequested.HasDelegate) { _editContext.OnValidationRequested += OnValidationRequestedHandler; } if (OnValidationStateChanged.HasDelegate) { _editContext.OnValidationStateChanged += OnValidationStateChangedHandler; } if (ValidateMode.IsIn(FormValidateMode.Rules, FormValidateMode.Complex)) { _editContext.OnFieldChanged += RulesModeOnFieldChanged; _editContext.OnValidationRequested += RulesModeOnValidationRequested; } }
private void RulesModeOnValidationRequested(object sender, ValidationRequestedEventArgs args) { if (!ValidateMode.IsIn(FormValidateMode.Rules, FormValidateMode.Complex)) { return; } _rulesValidator.ClearErrors(); var errors = new Dictionary <FieldIdentifier, List <string> >(); foreach (var formItem in _formItems) { var result = formItem.ValidateField(); if (result.Length > 0) { errors[formItem.GetFieldIdentifier()] = result.Select(r => r.ErrorMessage).ToList(); } } _rulesValidator.DisplayErrors(errors); }
private void RulesModeOnFieldChanged(object sender, FieldChangedEventArgs args) { if (!ValidateMode.IsIn(FormValidateMode.Rules, FormValidateMode.Complex)) { return; } _rulesValidator.ClearError(args.FieldIdentifier); var formItem = _formItems .Single(t => t.GetFieldIdentifier().FieldName == args.FieldIdentifier.FieldName); var result = formItem.ValidateField(); if (result.Length > 0) { var errors = new Dictionary <FieldIdentifier, List <string> >(); errors[args.FieldIdentifier] = result.Select(r => r.ErrorMessage).ToList(); _rulesValidator.DisplayErrors(errors); } }
protected override void Dispose(bool disposing) { if (OnFieldChanged.HasDelegate) { _editContext.OnFieldChanged -= OnFieldChangedHandler; } if (OnValidationRequested.HasDelegate) { _editContext.OnValidationRequested -= OnValidationRequestedHandler; } if (OnValidationStateChanged.HasDelegate) { _editContext.OnValidationStateChanged -= OnValidationStateChangedHandler; } if (ValidateMode.IsIn(FormValidateMode.Rules, FormValidateMode.Complex)) { _editContext.OnFieldChanged -= RulesModeOnFieldChanged; _editContext.OnValidationRequested -= RulesModeOnValidationRequested; } base.Dispose(disposing); }