protected virtual void ValidateUsingDataAnnotation(object newValidationValue) { ValidationStarted?.Invoke(); var messages = new ValidationMessageStore(EditContext); EditContextValidator.ValidateField(EditContext, messages, fieldIdentifier, MessageLocalizer ?? Options.ValidationMessageLocalizer); var matchStatus = messages[fieldIdentifier].Any() ? ValidationStatus.Error : ValidationStatus.Success; var matchMessages = matchStatus == ValidationStatus.Error ? messages[fieldIdentifier] : null; // Sometime status will stay the same and error message will change // eg. StringLength > empty string > Required if (Status != matchStatus || !Comparers.AreEqual(Messages?.ToArray(), matchMessages?.ToArray())) { Status = matchStatus; Messages = matchMessages; NotifyValidationStatusChanged(Status, Messages); } }
/// <summary> /// Runs the validation process. /// </summary> public ValidationStatus Validate() { if (UsePattern && patternRegex != null) { var matchStatus = patternRegex.IsMatch(inputComponent.ValidationValue?.ToString() ?? string.Empty) ? ValidationStatus.Success : ValidationStatus.Error; if (Status != matchStatus) { Status = matchStatus; NotifyValidationStatusChanged(Status); } } else if (EditContext != null && hasFieldIdentifier) { ValidationStarted?.Invoke(); var messages = new ValidationMessageStore(EditContext); EditContext.ValidateField(messages, fieldIdentifier); Status = messages[fieldIdentifier].Any() ? ValidationStatus.Error : ValidationStatus.Success; LastErrorMessage = Status == ValidationStatus.Error ? string.Join("; ", messages[fieldIdentifier]) : null; NotifyValidationStatusChanged(Status, LastErrorMessage); } else { var validatorHandler = Validator; if (validatorHandler != null) { ValidationStarted?.Invoke(); var validatorEventArgs = new ValidatorEventArgs(inputComponent.ValidationValue); validatorHandler(validatorEventArgs); if (Status != validatorEventArgs.Status) { Status = validatorEventArgs.Status; LastErrorMessage = Status == ValidationStatus.Error ? validatorEventArgs.ErrorText : null; NotifyValidationStatusChanged(Status, LastErrorMessage); } } } return(Status); }
protected virtual void ValidateUsingValidator(Action <ValidatorEventArgs> validatorHandler, object newValidationValue) { ValidationStarted?.Invoke(); var validatorEventArgs = new ValidatorEventArgs(newValidationValue); validatorHandler(validatorEventArgs); var matchMessages = Status == ValidationStatus.Error ? new string[] { validatorEventArgs.ErrorText } : null; if (Status != validatorEventArgs.Status || !Comparers.AreEqual(Messages?.ToArray(), matchMessages)) { Status = validatorEventArgs.Status; Messages = matchMessages; NotifyValidationStatusChanged(Status, Messages); } }
/// <inheritdoc/> public void NotifyValidationStarted() { ValidationStarted?.Invoke(); }