本文整理汇总了C#中Catel.Data.ValidationContext.GetFieldValidations方法的典型用法代码示例。如果您正苦于以下问题:C# ValidationContext.GetFieldValidations方法的具体用法?C# ValidationContext.GetFieldValidations怎么用?C# ValidationContext.GetFieldValidations使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Catel.Data.ValidationContext
的用法示例。
在下文中一共展示了ValidationContext.GetFieldValidations方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Validate
/// <summary>
/// Validates the current object for field and business rule errors.
/// </summary>
/// <param name="force">If set to <c>true</c>, a validation is forced (even if the object knows it is already validated).</param>
/// <param name="validateDataAnnotations">If set to <c>true</c>, the data annotations will be checked. This value is only used if <paramref name="force"/> is set to <c>true</c>.</param>
/// <remarks>
/// To check whether this object contains any errors, use the <see cref="INotifyDataErrorInfo.HasErrors"/> property.
/// </remarks>
internal void Validate(bool force, bool validateDataAnnotations)
{
if (SuspendValidation)
{
return;
}
if (IsValidating)
{
return;
}
IsValidating = true;
var existingValidationContext = (ValidationContext)_validationContext;
bool hasErrors = existingValidationContext.HasErrors;
bool hasWarnings = existingValidationContext.HasWarnings;
var validationContext = new ValidationContext();
var changes = new List<ValidationContextChange>();
var validator = GetValidator();
if (validator != null)
{
validator.BeforeValidation(this, existingValidationContext.GetFieldValidations(), existingValidationContext.GetBusinessRuleValidations());
}
OnValidating(validationContext);
CatchUpWithSuspendedAnnotationsValidation();
if (force && validateDataAnnotations)
{
var type = GetType();
var ignoredOrFailedPropertyValidations = _propertyValuesIgnoredOrFailedForValidation[type];
// In forced mode, validate all registered properties for annotations
var catelTypeInfo = PropertyDataManager.GetCatelTypeInfo(type);
foreach (var propertyData in catelTypeInfo.GetCatelProperties())
{
var propertyInfo = propertyData.Value.GetPropertyInfo(type);
if (propertyInfo == null || !propertyInfo.HasPublicGetter)
{
// Note: non-public getter, do not validate
ignoredOrFailedPropertyValidations.Add(propertyData.Key);
continue;
}
var propertyValue = GetValue(propertyData.Value);
ValidatePropertyUsingAnnotations(propertyData.Key, propertyValue, propertyData.Value);
}
#if !WINDOWS_PHONE && !NETFX_CORE && !PCL && !NET35
// Validate non-catel properties as well for attribute validation
foreach (var propertyInfo in catelTypeInfo.GetNonCatelProperties())
{
if (_firstAnnotationValidation)
{
if (propertyInfo.Value.IsDecoratedWithAttribute(typeof(ExcludeFromValidationAttribute)))
{
ignoredOrFailedPropertyValidations.Add(propertyInfo.Key);
}
}
if (!propertyInfo.Value.HasPublicGetter)
{
// Note: non-public getter, do not validate
ignoredOrFailedPropertyValidations.Add(propertyInfo.Key);
continue;
}
// TODO: Should we check for annotations attributes?
if (ignoredOrFailedPropertyValidations.Contains(propertyInfo.Key))
{
continue;
}
try
{
var propertyValue = propertyInfo.Value.PropertyInfo.GetValue(this, null);
ValidatePropertyUsingAnnotations(propertyInfo.Key, propertyValue, null);
}
catch (Exception ex)
{
Log.Warning(ex, "Failed to validate property '{0}.{1}', adding it to the ignore list", type.Name, propertyInfo.Key);
ignoredOrFailedPropertyValidations.Add(propertyInfo.Key);
}
}
_firstAnnotationValidation = false;
#endif
//.........这里部分代码省略.........
示例2: ReturnsRightValidationsForEmptyContextWithTag
public void ReturnsRightValidationsForEmptyContextWithTag()
{
var context = new ValidationContext();
var fieldValidations = context.GetFieldValidations((object)"tag");
Assert.AreEqual(0, fieldValidations.Count);
}
示例3: ReturnsRightValidationsForEmptyContextWithTagAndPropertyName
public void ReturnsRightValidationsForEmptyContextWithTagAndPropertyName()
{
var context = new ValidationContext();
var fieldValidations = context.GetFieldValidations("MyProperty", "tag");
Assert.AreEqual(0, fieldValidations.Count);
}
示例4: CorrectlySynchronizesWithRemovedBusinessRuleError
public void CorrectlySynchronizesWithRemovedBusinessRuleError()
{
var fieldValidationResults1 = new List<IFieldValidationResult>();
fieldValidationResults1.Add(FieldValidationResult.CreateWarning("MyProperty", "FieldWarning"));
fieldValidationResults1.Add(FieldValidationResult.CreateError("MyProperty", "FieldError"));
var businessRuleValidationResults1 = new List<IBusinessRuleValidationResult>();
businessRuleValidationResults1.Add(BusinessRuleValidationResult.CreateWarning("BusinessRuleWarning"));
businessRuleValidationResults1.Add(BusinessRuleValidationResult.CreateError("BusinessRuleError"));
var context1 = new ValidationContext(fieldValidationResults1, businessRuleValidationResults1);
var fieldValidationResults2 = new List<IFieldValidationResult>();
fieldValidationResults2.Add(FieldValidationResult.CreateWarning("MyProperty", "FieldWarning"));
fieldValidationResults2.Add(FieldValidationResult.CreateError("MyProperty", "FieldError"));
var businessRuleValidationResults2 = new List<IBusinessRuleValidationResult>();
businessRuleValidationResults2.Add(BusinessRuleValidationResult.CreateWarning("BusinessRuleWarning"));
var context2 = new ValidationContext(fieldValidationResults2, businessRuleValidationResults2);
context1.SynchronizeWithContext(context2);
// First context should now equal second context
int counter;
var fieldValidations = context1.GetFieldValidations();
Assert.AreEqual(2, fieldValidations.Count);
counter = -1;
counter++;
Assert.AreEqual("MyProperty", fieldValidations[counter].PropertyName);
Assert.AreEqual("FieldWarning", fieldValidations[counter].Message);
Assert.AreEqual(ValidationResultType.Warning, fieldValidations[counter].ValidationResultType);
counter++;
Assert.AreEqual("MyProperty", fieldValidations[counter].PropertyName);
Assert.AreEqual("FieldError", fieldValidations[counter].Message);
Assert.AreEqual(ValidationResultType.Error, fieldValidations[counter].ValidationResultType);
var businessRuleValidations = context1.GetBusinessRuleValidations();
Assert.AreEqual(1, businessRuleValidations.Count);
counter = -1;
counter++;
Assert.AreEqual("BusinessRuleWarning", businessRuleValidations[counter].Message);
Assert.AreEqual(ValidationResultType.Warning, businessRuleValidations[counter].ValidationResultType);
}