/// <summary> /// Called when the object is validating the fields. /// </summary> /// <param name="validationContext">The validation context.</param> protected override void OnValidatingFields(IValidationContext validationContext) { if (string.IsNullOrWhiteSpace(Name)) { validationContext.AddFieldValidationResult(new FieldValidationResult(NameProperty,ValidationResultType.Error, "Name cannot be empty.")); } }
/// <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; 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) { if (!string.IsNullOrEmpty(dataErrorInfo[modelProperty])) { validationContext.AddFieldValidationResult(FieldValidationResult.CreateError(mapping.ViewModelProperty, dataErrorInfo[modelProperty])); hasSetFieldError = true; } } // IDataWarningInfo var dataWarningInfo = model as IDataWarningInfo; if (dataWarningInfo != null) { if (!string.IsNullOrEmpty(dataWarningInfo[modelProperty])) { validationContext.AddFieldValidationResult(FieldValidationResult.CreateWarning(mapping.ViewModelProperty, dataWarningInfo[modelProperty])); hasSetFieldWarning = true; } } // INotifyDataErrorInfo & INotifyDataWarningInfo if (_modelErrorInfo.ContainsKey(mapping.ModelProperty)) { var modelErrorInfo = _modelErrorInfo[mapping.ModelProperty]; if (!hasSetFieldError) { foreach (string error in modelErrorInfo.GetErrors(modelProperty)) { if (!string.IsNullOrEmpty(error)) { validationContext.AddFieldValidationResult(FieldValidationResult.CreateError(mapping.ViewModelProperty, error)); break; } } } if (!hasSetFieldWarning) { foreach (string warning in modelErrorInfo.GetWarnings(modelProperty)) { if (!string.IsNullOrEmpty(warning)) { validationContext.AddFieldValidationResult(FieldValidationResult.CreateWarning(mapping.ViewModelProperty, warning)); break; } } } } } } }