• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C# DataAnnotations.ValidationAttribute类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



ValidationAttribute类属于System.ComponentModel.DataAnnotations命名空间,在下文中一共展示了ValidationAttribute类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。

示例1: ViewModelPropertyValidationRule

 public ViewModelPropertyValidationRule(string clientRule, ValidationAttribute sourceValidationAttribute, string errorMessage, params object[] parameters)
 {
     ClientRuleName = clientRule;
     SourceValidationAttribute = sourceValidationAttribute;
     ErrorMessage = errorMessage;
     Parameters = parameters;
 }
开发者ID:darilek,项目名称:dotvvm,代码行数:7,代码来源:ViewModelPropertyValidationRule.cs


示例2: GetRules

 /// <summary>
 /// Gets the rules the adapter provides.
 /// </summary>
 /// <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 override IEnumerable<ModelValidationRule> GetRules(ValidationAttribute attribute, PropertyDescriptor descriptor)
 {
     yield return new StringLengthValidationRule(attribute.FormatErrorMessage,
         new[] { descriptor.Name },
         ((StringLengthAttribute)attribute).MinimumLength,
         ((StringLengthAttribute)attribute).MaximumLength);
 }
开发者ID:RadifMasud,项目名称:Nancy,代码行数:13,代码来源:StringLengthValidatorAdapter.cs


示例3: ValidationAttributeValidator

        /// <summary>
        ///     Creates an instance of <see cref = "ValidationAttributeValidator" /> class.
        /// </summary>
        /// <param name = "validationAttribute">
        ///     Validation attribute used to validate a property or an entity.
        /// </param>
        public ValidationAttributeValidator(ValidationAttribute validationAttribute, DisplayAttribute displayAttribute)
        {
            //Contract.Requires(validationAttribute != null);

            _validationAttribute = validationAttribute;
            _displayAttribute = displayAttribute;
        }
开发者ID:jimmy00784,项目名称:entityframework,代码行数:13,代码来源:ValidationAttributeValidator.cs


示例4: ValidationAttributeValidator

        // <summary>
        // Creates an instance of <see cref="ValidationAttributeValidator" /> class.
        // </summary>
        // <param name="validationAttribute"> Validation attribute used to validate a property or an entity. </param>
        public ValidationAttributeValidator(ValidationAttribute validationAttribute, DisplayAttribute displayAttribute)
        {
            DebugCheck.NotNull(validationAttribute);

            _validationAttribute = validationAttribute;
            _displayAttribute = displayAttribute;
        }
开发者ID:Cireson,项目名称:EntityFramework6,代码行数:11,代码来源:ValidationAttributeValidator.cs


示例5: GetAttributeValidation

        public static void GetAttributeValidation(ValidationAttribute attr, IDictionary<string, object> htmlAttributes)
        {
            var str = string.Empty;
              if (attr is RequiredAttribute)
              {
              StringLengthAttribute a = attr as StringLengthAttribute;
              htmlAttributes["required"] = null;

              }
              else if (attr is StringLengthAttribute)
              {
              StringLengthAttribute a = attr as StringLengthAttribute;
              htmlAttributes["maxlength"] = a.MaximumLength;
              htmlAttributes["minlength"] = a.MinimumLength;

              }
              else if (attr is RegularExpressionAttribute)
              {
              RegularExpressionAttribute a = attr as RegularExpressionAttribute;
              htmlAttributes["pattern"] = a.Pattern;

              }
              else if (attr is IdenticalAttribute)
              {
              IdenticalAttribute a = attr as IdenticalAttribute;
              htmlAttributes["data-fv-identical"] = "true";
              htmlAttributes["data-fv-identical-field"] = a.Field;
              }

              if (attr.ErrorMessage.IsNotNull()) {
              htmlAttributes[ ValidateMesAttr[attr.GetType()]]= attr.ErrorMessage;
              }
        }
开发者ID:alittletired,项目名称:SolutionPlatform,代码行数:33,代码来源:ValidationHelper.cs


示例6: Create

        /// <summary>
        /// Create client validation rules for Data Annotation attributes.
        /// </summary>
        /// <param name="attribute">Attribute</param>
        /// <param name="errorMessage">Not formatted error message (should contain {0} etc}</param>
        /// <returns>A collection of rules (or an empty collection)</returns>
        public virtual IEnumerable<ModelClientValidationRule> Create(ValidationAttribute attribute, string errorMessage)
        {
            if (attribute is RangeAttribute)
            {
                var attr = (RangeAttribute) attribute;
                return new[]
                           {
                               new ModelClientValidationRangeRule(errorMessage, attr.Minimum, attr.Maximum)
                           };
            }
            if (attribute is RegularExpressionAttribute)
            {
                var attr = (RegularExpressionAttribute) attribute;
                return new[] {new ModelClientValidationRegexRule(errorMessage, attr.Pattern)};
            }
            if (attribute is RequiredAttribute)
            {
                var attr = (RequiredAttribute) attribute;
                return new[] {new ModelClientValidationRequiredRule(errorMessage)};
            }
            if (attribute is StringLengthAttribute)
            {
                var attr = (StringLengthAttribute) attribute;
                return new[]
                           {
                               new ModelClientValidationStringLengthRule(errorMessage, attr.MinimumLength,
                                                                         attr.MaximumLength)
                           };
            }

            return new ModelClientValidationRule[0];
        }
开发者ID:iceball12,项目名称:griffin.mvccontrib,代码行数:38,代码来源:ValidationAttributeAdapterFactory.cs


示例7: MyValidator

			public MyValidator(ValidationAttribute attribute, string errorMsg, ModelMetadata metadata, ControllerContext controllerContext, IEnumerable<ModelClientValidationRule> create)
				: base(metadata, controllerContext)
			{
				_attribute = attribute;
				_errorMsg = errorMsg;
				_create = create;
			}
开发者ID:damirarh,项目名称:griffin.mvccontrib,代码行数:7,代码来源:LocalizedModelValidatorProvider.cs


示例8: DynamoDataAnnotationsModelValidator

		public DynamoDataAnnotationsModelValidator(IServiceProvider provider, ModelMetadata metadata, ControllerContext context, ValidationAttribute attribute)
			: base(metadata, context, attribute)
		{
			if (provider == null)
				throw new ArgumentNullException("provider");

			_provider = provider;
		}
开发者ID:Kingefosa,项目名称:Dynamo.IoC,代码行数:8,代码来源:DynamoDataAnnotationsModelValidator.cs


示例9: DataAnnotationsModelValidator

        public DataAnnotationsModelValidator(ModelMetadata metadata, ControllerContext context, ValidationAttribute attribute)
            : base(metadata, context)
        {
            if (attribute == null) {
                throw new ArgumentNullException("attribute");
            }

            Attribute = attribute;
        }
开发者ID:jenrom,项目名称:Spikes,代码行数:9,代码来源:DataAnnotationsModelValidator.cs


示例10: AttributeValidationItem

 /// <summary>
 /// Initializes a new instance of the AttributeValidationItem class.
 /// </summary>
 /// <param name="attribute"></param>
 /// <param name="propertyName"></param>
 public AttributeValidationItem(ValidationAttribute attribute, string propertyName)
 {
     if (attribute == null)
         throw new ArgumentNullException("attribute", "attribute is null.");
     if (String.IsNullOrEmpty(propertyName))
         throw new ArgumentException("propertyName is null or empty.", "propertyName");
     _attribute = attribute;
     _propertyName = propertyName;
 }
开发者ID:xebialabs-community,项目名称:xld-manifest-editor,代码行数:14,代码来源:AttributeValidationItem.cs


示例11: DataAnnotationsModelValidator

        public DataAnnotationsModelValidator(ValidationAttribute attribute, IStringLocalizer stringLocalizer)
        {
            if (attribute == null)
            {
                throw new ArgumentNullException(nameof(attribute));
            }

            Attribute = attribute;
            _stringLocalizer = stringLocalizer;
        }
开发者ID:huoxudong125,项目名称:Mvc,代码行数:10,代码来源:DataAnnotationsModelValidator.cs


示例12: GetErrorMessages

 public static IEnumerable<string> GetErrorMessages(ValidationAttribute[] attributes,
     object value, ValidationContext context)
 {
     var errorMessages = attributes
             .Select(v => v.GetValidationResult(value, context))
             .Where(r => r != null)
             .Select(r => r.ErrorMessage)
             .Where(e => !string.IsNullOrEmpty(e));
     return errorMessages;
 }
开发者ID:guozanhua,项目名称:phmi,代码行数:10,代码来源:Validator.cs


示例13: HandleErrorMessage

        protected virtual void HandleErrorMessage(string languageKey, ValidationAttribute attribute)
        {
            if (attribute.ErrorMessageResourceName == null)
            {
                attribute.ErrorMessage = LangResource.Resources.GetString(languageKey);

                if (string.IsNullOrEmpty(attribute.ErrorMessage))
                    attribute.ErrorMessage = string.Format("[[{0}]]", languageKey);
            }
        }
开发者ID:sbudihar,项目名称:SIRIUSrepo,代码行数:10,代码来源:LocalizedDataAnnotationsModelMetadataProvider.cs


示例14: AdapterFactory_RegistersAdapters_ForDataAnnotationAttributes

        public void AdapterFactory_RegistersAdapters_ForDataAnnotationAttributes(
               ValidationAttribute attribute,
               Type expectedAdapterType)
        {
            // Arrange and Act
            var adapter = _validationAttributeAdapterProvider.GetAttributeAdapter(attribute, stringLocalizer: null);

            // Assert
            Assert.IsType(expectedAdapterType, adapter);
        }
开发者ID:phinq19,项目名称:git_example,代码行数:10,代码来源:ValidationAttributeAdapterProviderTest.cs


示例15: DataAnnotationsModelValidator

        public DataAnnotationsModelValidator(IEnumerable<ModelValidatorProvider> validatorProviders, ValidationAttribute attribute)
            : base(validatorProviders)
        {
            if (attribute == null)
            {
                throw Error.ArgumentNull("attribute");
            }

            Attribute = attribute;
        }
开发者ID:chrissimon-au,项目名称:aspnetwebstack,代码行数:10,代码来源:DataAnnotationsModelValidator.cs


示例16: GetRules

        /// <summary>
        /// Gets the rules the adapter provides.
        /// </summary>
        /// <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 override IEnumerable<ModelValidationRule> GetRules(ValidationAttribute attribute, PropertyDescriptor descriptor)
        {
            var requiredAttribute = (RequiredAttribute) attribute;

            yield return new NotNullValidationRule(attribute.FormatErrorMessage, new[] { descriptor.Name });

            if (!requiredAttribute.AllowEmptyStrings)
            {
                yield return new NotEmptyValidationRule(attribute.FormatErrorMessage, new[] { descriptor.Name });
            }
        }
开发者ID:RadifMasud,项目名称:Nancy,代码行数:17,代码来源:RequiredValidatorAdapter.cs


示例17: AdapterFactory_RegistersAdapters_ForDataTypeAttributes

        public void AdapterFactory_RegistersAdapters_ForDataTypeAttributes(
            ValidationAttribute attribute,
            string expectedRuleName)
        {
            // Arrange & Act
            var adapter = _validationAttributeAdapterProvider.GetAttributeAdapter(attribute, stringLocalizer: null);

            // Assert
            var dataTypeAdapter = Assert.IsType<DataTypeAttributeAdapter>(adapter);
            Assert.Equal(expectedRuleName, dataTypeAdapter.RuleName);
        }
开发者ID:phinq19,项目名称:git_example,代码行数:11,代码来源:ValidationAttributeAdapterProviderTest.cs


示例18: GetMessageContext

 /// <summary>
 /// Initializes a new instance of the <see cref="GetMessageContext"/> class.
 /// </summary>
 /// <param name="attribute">The attribute.</param>
 /// <param name="containerType">Model that the property is in.</param>
 /// <param name="propertyName">Name of the property.</param>
 /// <param name="cultureInfo">Requested language.</param>
 public GetMessageContext(ValidationAttribute attribute, Type containerType, string propertyName,
                          CultureInfo cultureInfo)
 {
     if (attribute == null) throw new ArgumentNullException("attribute");
     if (containerType == null) throw new ArgumentNullException("containerType");
     if (propertyName == null) throw new ArgumentNullException("propertyName");
     if (cultureInfo == null) throw new ArgumentNullException("cultureInfo");
     _attribute = attribute;
     _containerType = containerType;
     _propertyName = propertyName;
     _cultureInfo = cultureInfo;
 }
开发者ID:paulzhousz,项目名称:YOYOHRMS,代码行数:19,代码来源:GetMessageContext.cs


示例19: CustomDataAnnotationsModelValidator

        public CustomDataAnnotationsModelValidator(ModelMetadata metadata, ControllerContext context,
            ValidationAttribute attribute)
            : base(metadata, context, attribute)
        {
            if (Attribute.ErrorMessageResourceType == null)
            {
                Attribute.ErrorMessageResourceType = ResourceType;
            }

            if (Attribute.ErrorMessageResourceName == null)
            {
                Attribute.ErrorMessageResourceName = ResourceNameFunc(Attribute);
            }
        }
开发者ID:jmptrader,项目名称:griffin,代码行数:14,代码来源:DataAnnotationsValidator.cs


示例20: GetRules

        /// <summary>
        /// Gets the rules the adapter provides.
        /// </summary>
        /// <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 override IEnumerable<ModelValidationRule> GetRules(ValidationAttribute attribute, PropertyDescriptor descriptor)
        {
            var ra = (RangeAttribute)attribute;

            yield return new ComparisonValidationRule(attribute.FormatErrorMessage,
                new[] { descriptor.Name },
                ComparisonOperator.GreaterThanOrEqual,
                Convert(ra.OperandType, ra.Minimum));

            yield return new ComparisonValidationRule(attribute.FormatErrorMessage,
                new[] { descriptor.Name },
                ComparisonOperator.LessThanOrEqual,
                Convert(ra.OperandType, ra.Maximum));
        }
开发者ID:RadifMasud,项目名称:Nancy,代码行数:20,代码来源:RangeValidatorAdapter.cs



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# DataAnnotations.ValidationContext类代码示例发布时间:2022-05-26
下一篇:
C# Primitives.ImportDefinition类代码示例发布时间:2022-05-26
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap