protected override void OnValidating(IValidationContext validationContext) { if (string.IsNullOrWhiteSpace(Name)) { validationContext.Add(FieldValidationResult.CreateError(nameof(Name), "'Name' for the LogFilterGroup is required")); } }
protected override void OnValidatedFields(IValidationContext validationContext) { if (string.IsNullOrEmpty(TextMessage)) { validationContext.Add(new FieldValidationResult("TextMessage", ValidationResultType.Error, "Text message cannot be empty")); } base.OnValidatedFields(validationContext); }
protected override void OnValidating(IValidationContext validationContext) { if (string.IsNullOrWhiteSpace(Name)) { validationContext.Add(FieldValidationResult.CreateError(nameof(Name), LanguageHelper.GetString("Controls_LogViewer_LogFilterGroupEditor_NameForTheLogFilterGroupIsRequired"))); } }
/// <summary> /// Called when the object is validating the business rules. /// </summary> /// <param name="validationContext">The validation context.</param> protected override void OnValidatingBusinessRules(IValidationContext validationContext) { base.OnValidatingBusinessRules(validationContext); lock (_modelLock) { foreach (var modelObject in _modelObjects) { if (!_modelObjectsInfo[modelObject.Key].SupportValidation) { continue; } // IDataErrorInfo var dataErrorInfo = modelObject.Value as IDataErrorInfo; if (dataErrorInfo != null && !string.IsNullOrEmpty(dataErrorInfo.Error)) { validationContext.Add(BusinessRuleValidationResult.CreateError(dataErrorInfo.Error)); } // IDataWarningInfo var dataWarningInfo = modelObject.Value as IDataWarningInfo; if (dataWarningInfo != null && !string.IsNullOrEmpty(dataWarningInfo.Warning)) { validationContext.Add(BusinessRuleValidationResult.CreateWarning(dataWarningInfo.Warning)); } // INotifyDataErrorInfo & INotifyDataWarningInfo ModelErrorInfo modelErrorInfo; if (_modelErrorInfo.TryGetValue(modelObject.Key, out modelErrorInfo)) { foreach (var error in modelErrorInfo.GetErrors(string.Empty)) { validationContext.Add(BusinessRuleValidationResult.CreateError(error)); } foreach (var warning in modelErrorInfo.GetWarnings(string.Empty)) { validationContext.Add(BusinessRuleValidationResult.CreateWarning(warning)); } } } } }
public static void AddValidationError(this IValidationContext validationContext, string message, string tag = null) { Argument.IsNotNull(() => validationContext); var businessRuleValidationResult = new BusinessRuleValidationResult(ValidationResultType.Error, message) { Tag = tag }; validationContext.Add(businessRuleValidationResult); }
protected virtual void ValidateMandatorySwitches(IValidationContext validationContext, IEnumerable <OptionDefinition> optionDefinitions, HashSet <string> handledOptions) { Log.Debug("Checking if all required options are specified"); foreach (var optionDefinition in optionDefinitions) { if (optionDefinition.IsMandatory && !handledOptions.Contains(optionDefinition.ShortName)) { var message = string.Format(_languageService.GetString("CommandLine_RequiredSwitchNotSpecified"), optionDefinition); Log.Error(message); validationContext.Add(FieldValidationResult.CreateError(optionDefinition.GetSwitchDisplay(), message)); } } }
/// <summary> /// Called when the object is validating the fields. /// </summary> /// <param name="validationContext">The validation context.</param> protected override void OnValidatingFields(IValidationContext validationContext) { base.OnValidatingFields(validationContext); // Map all field errors and warnings from the model to this viewmodel foreach (var viewModelToModelMap in _viewModelToModelMap) { var mapping = viewModelToModelMap.Value; lock (_modelLock) { if (!_modelObjectsInfo[mapping.ModelProperty].SupportValidation) { continue; } } var model = GetValue(mapping.ModelProperty); string[] modelProperties = mapping.ValueProperties; foreach (var modelProperty in modelProperties) { bool hasSetFieldError = false; bool hasSetFieldWarning = false; // IDataErrorInfo var dataErrorInfo = model as IDataErrorInfo; if (dataErrorInfo != null && !string.IsNullOrEmpty(dataErrorInfo[modelProperty])) { validationContext.Add(FieldValidationResult.CreateError(mapping.ViewModelProperty, dataErrorInfo[modelProperty])); hasSetFieldError = true; } // IDataWarningInfo var dataWarningInfo = model as IDataWarningInfo; if (dataWarningInfo != null && !string.IsNullOrEmpty(dataWarningInfo[modelProperty])) { validationContext.Add(FieldValidationResult.CreateWarning(mapping.ViewModelProperty, dataWarningInfo[modelProperty])); hasSetFieldWarning = true; } // INotifyDataErrorInfo & INotifyDataWarningInfo if (_modelErrorInfo.TryGetValue(mapping.ModelProperty, out var modelErrorInfo)) { if (!hasSetFieldError) { foreach (string error in modelErrorInfo.GetErrors(modelProperty)) { if (!string.IsNullOrEmpty(error)) { validationContext.Add(FieldValidationResult.CreateError(mapping.ViewModelProperty, error)); break; } } } if (!hasSetFieldWarning) { foreach (string warning in modelErrorInfo.GetWarnings(modelProperty)) { if (!string.IsNullOrEmpty(warning)) { validationContext.Add(FieldValidationResult.CreateWarning(mapping.ViewModelProperty, warning)); break; } } } } } } }