当前位置: 首页>>代码示例>>C#>>正文


C# ValidationAttribute.GetValidationResult方法代码示例

本文整理汇总了C#中System.ComponentModel.DataAnnotations.ValidationAttribute.GetValidationResult方法的典型用法代码示例。如果您正苦于以下问题:C# ValidationAttribute.GetValidationResult方法的具体用法?C# ValidationAttribute.GetValidationResult怎么用?C# ValidationAttribute.GetValidationResult使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.ComponentModel.DataAnnotations.ValidationAttribute的用法示例。


在下文中一共展示了ValidationAttribute.GetValidationResult方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Validate

        public IEnumerable<ModelValidationError> Validate(object instance, ValidationAttribute attribute, PropertyDescriptor descriptor, NancyContext context)
        {
            var validationContext = new ValidationContext(instance, null, null)
                          {
                              MemberName = ((MatchAttribute)attribute).SourceProperty
                          };

            var result = attribute.GetValidationResult(instance, validationContext);

            if(result != null)
            {
                yield return new ModelValidationError(result.MemberNames, attribute.ErrorMessage);
            }

            yield break;
        }
开发者ID:danielmarbach,项目名称:marten,代码行数:16,代码来源:CustomDataAdapter.cs

示例2: Validate

        /// <summary>
        /// Validates the given instance.
        /// </summary>
        /// <param name="instance">The instance that should be validated.</param>
        /// <param name="attribute">The <see cref="ValidationAttribute"/> that should be handled.</param>
        /// <param name="descriptor">A <see cref="PropertyDescriptor"/> instance for the property that is being validated.</param>
        /// <returns>An <see cref="IEnumerable{T}"/> of <see cref="ModelValidationRule"/> instances.</returns>
        public virtual IEnumerable<ModelValidationError> Validate(object instance, ValidationAttribute attribute, PropertyDescriptor descriptor)
        {
            var context =
                new ValidationContext(instance, null, null)
                {
                    MemberName = descriptor == null ? null : descriptor.Name
                };

            if(descriptor != null)
            {
                instance = descriptor.GetValue(instance);
            }

            var result =
                attribute.GetValidationResult(instance, context);

            if (result != null)
            {
                yield return new ModelValidationError(result.MemberNames, string.Join(" ", result.MemberNames.Select(attribute.FormatErrorMessage)));
            }
        }
开发者ID:jijamw,项目名称:Nancy,代码行数:28,代码来源:DataAnnotationsValidatorAdapter.cs

示例3: validate

 private bool validate(ValidationAttribute v, object value)
 {
     return v.GetValidationResult(value, new ValidationContext(ValidationContext, null, null)) == ValidationResult.Success;
 }
开发者ID:cjmurph,项目名称:PmsService,代码行数:4,代码来源:ObservableObject.cs

示例4: TryValidate

        /// <summary>
        /// Tests whether a value is valid against a single <see cref="ValidationAttribute"/> using the <see cref="ValidationContext"/>.
        /// </summary>
        /// <param name="value">The value to be tested for validity.</param>
        /// <param name="validationContext">Describes the property member to validate.</param>
        /// <param name="attribute">The validation attribute to test.</param>
        /// <param name="validationError">The validation error that occurs during validation.  Will be <c>null</c> when the return value is <c>true</c>.</param>
        /// <returns><c>true</c> if the value is valid.</returns>
        /// <exception cref="ArgumentNullException">When <paramref name="validationContext"/> is null.</exception>
        private static bool TryValidate(object value, ValidationContext validationContext, ValidationAttribute attribute, out ValidationError validationError)
        {
            if (validationContext == null) {
                throw new ArgumentNullException("validationContext");
            }

            ValidationResult validationResult = attribute.GetValidationResult(value, validationContext);
            if (validationResult != ValidationResult.Success) {
                validationError = new ValidationError(attribute, value, validationResult);
                return false;
            }

            validationError = null;
            return true;
        }
开发者ID:expanz,项目名称:expanz-Microsoft-XAML-SDKs,代码行数:24,代码来源:Validator.cs

示例5: Validate

        /// <summary>
        /// Validates the given instance.
        /// </summary>
        /// <param name="instance">The instance that should be validated.</param>
        /// <param name="attribute">The <see cref="ValidationAttribute"/> that should be handled.</param>
        /// <param name="descriptor">A <see cref="PropertyDescriptor"/> instance for the property that is being validated.</param>
        /// <param name="context">The <see cref="NancyContext"/> of the current request.</param>
        /// <returns>An <see cref="IEnumerable{T}"/> of <see cref="ModelValidationRule"/> instances.</returns>
        public virtual IEnumerable<ModelValidationError> Validate(object instance, ValidationAttribute attribute, PropertyDescriptor descriptor, NancyContext context)
        {
            var validationContext =
                new ValidationContext(instance, null, null)
                {
                    MemberName = descriptor == null ? null : descriptor.Name
                };

            // When running on Mono the Display attribute is not auto populated so for now we do it ourselves
            if (IsRunningOnMono)
            {
                var displayName = this.GetDisplayNameForMember(instance, validationContext.MemberName);
                if (!string.IsNullOrEmpty(displayName))
                {
                    validationContext.DisplayName = displayName;
                }
            }

            if (descriptor != null)
            {
                // Display(Name) will auto populate the context, while DisplayName() needs to be manually set
                if (validationContext.MemberName == validationContext.DisplayName && !string.IsNullOrEmpty(descriptor.DisplayName))
                {
                    validationContext.DisplayName = descriptor.DisplayName;
                }

                instance = descriptor.GetValue(instance);
            }

            var result =
                attribute.GetValidationResult(instance, validationContext);

            if (result != null)
            {
                yield return this.GetValidationError(result, validationContext, attribute);
            }
        }
开发者ID:rdterner,项目名称:Nancy,代码行数:45,代码来源:DataAnnotationsValidatorAdapter.cs

示例6: GetFinalMemberName

 private static string GetFinalMemberName(ValidationAttribute attrib, object contextModel, object mamberValue, string givenMemberName, string givenDisplayName)
 {
     return attrib.GetValidationResult(mamberValue, new ValidationContext(contextModel)
     {
         MemberName = givenMemberName,
         DisplayName = givenDisplayName
     })?.MemberNames.Single();
 }
开发者ID:jwaliszko,项目名称:ExpressiveAnnotations,代码行数:8,代码来源:AttribsTest.cs


注:本文中的System.ComponentModel.DataAnnotations.ValidationAttribute.GetValidationResult方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。