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


C# ValidationContext.IsNull方法代码示例

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


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

示例1: PerformCustomValidation

		/// <summary>
		/// Validates the specified value with respect to the current validation attribute.
		/// </summary>
		/// <param name="value">
		/// The value to validate.
		/// </param>
		/// <param name="validationContext">
		/// The context information about the validation operation.
		/// </param>
		/// <returns>
		/// An instance of the <see cref="T:System.ComponentModel.DataAnnotations.ValidationResult"/> class.
		/// </returns>
		protected override ValidationResult PerformCustomValidation(object value, ValidationContext validationContext)
		{
			if (value.IsNull())
			{
				return ValidationResult.Success;
			}

			if (validationContext.IsNull())
			{
				validationContext = new ValidationContext(value, null, null) { DisplayName = "The value" };
			}

			var memberNames = new[] { validationContext.MemberName };

			int length = value.ToString().Length;

			if (length > this.maximumLength)
			{
				this.ErrorMessage = string.Format(ValidationMessages.ValueGreaterThanMaximumLength, validationContext.DisplayName, this.maximumLength);

				return new ValidationResult(this.ErrorMessage, memberNames);
			}

			return ValidationResult.Success;
		}
开发者ID:KaraokeStu,项目名称:LeadPipe.Net,代码行数:37,代码来源:MaximumLengthAttribute.cs

示例2: PerformCustomValidation

		/// <summary>
		/// Validates the specified value with respect to the current validation attribute.
		/// </summary>
		/// <param name="value">
		/// The value to validate.
		/// </param>
		/// <param name="validationContext">
		/// The context information about the validation operation.
		/// </param>
		/// <returns>
		/// An instance of the <see cref="T:System.ComponentModel.DataAnnotations.ValidationResult"/> class.
		/// </returns>
		protected override ValidationResult PerformCustomValidation(object value, ValidationContext validationContext)
		{
			if (value.IsNull())
			{
				return ValidationResult.Success;
			}

			if (validationContext.IsNull())
			{
				validationContext = new ValidationContext(value, null, null) { DisplayName = "The value" };
			}

			var memberNames = new[] { validationContext.MemberName };

			double valueAsDouble;

			var isDouble = double.TryParse(Convert.ToString(value), out valueAsDouble);

			if (!isDouble)
			{
				this.ErrorMessage = validationContext.DisplayName.FormattedWith(ValidationMessages.ValueMustBeNumeric);

				return new ValidationResult(this.ErrorMessage, memberNames);
			}

			if (valueAsDouble > this.maximum)
			{
				this.ErrorMessage = string.Format(ValidationMessages.ValueMustBeLessThanOrEqualTo, validationContext.DisplayName, this.maximum);

				return new ValidationResult(this.ErrorMessage, memberNames);
			}

			return ValidationResult.Success;
		}
开发者ID:KaraokeStu,项目名称:LeadPipe.Net,代码行数:46,代码来源:MaximumAttribute.cs

示例3: PerformCustomValidation

		/// <summary>
		/// Validates the specified value with respect to the current validation attribute.
		/// </summary>
		/// <param name="value">
		/// The value to validate.
		/// </param>
		/// <param name="validationContext">
		/// The context information about the validation operation.
		/// </param>
		/// <returns>
		/// An instance of the <see cref="T:System.ComponentModel.DataAnnotations.ValidationResult"/> class.
		/// </returns>
		protected override ValidationResult PerformCustomValidation(object value, ValidationContext validationContext)
		{
			if (value.IsNull())
			{
				return ValidationResult.Success;
			}

			string convertedValue;
			if (validationContext.IsNull())
			{
				validationContext = new ValidationContext(value, null, null) { DisplayName = "The value" };
			}

			var memberNames = new string[] { validationContext.MemberName };

			try
			{
				convertedValue = Convert.ToString(value);
			}
			catch (FormatException)
			{
				this.ErrorMessage = validationContext.DisplayName.FormattedWith(ValidationMessages.ValueMustBeString);

				return new ValidationResult(this.ErrorMessage);
			}

			if (convertedValue.ContainsLeadingWhiteSpace())
			{
				this.ErrorMessage = validationContext.DisplayName.FormattedWith(ValidationMessages.CanNotContainLeadingWhitespace);

				return new ValidationResult(this.ErrorMessage, memberNames);
			}

			return ValidationResult.Success;
		}
开发者ID:KaraokeStu,项目名称:LeadPipe.Net,代码行数:47,代码来源:NoLeadingWhitespaceAttribute.cs

示例4: PerformCustomValidation

		/// <summary>
		/// Validates the specified value with respect to the current validation attribute.
		/// </summary>
		/// <param name="value">
		/// The value to validate.
		/// </param>
		/// <param name="validationContext">
		/// The context information about the validation operation.
		/// </param>
		/// <returns>
		/// An instance of the <see cref="T:System.ComponentModel.DataAnnotations.ValidationResult"/> class.
		/// </returns>
		protected override ValidationResult PerformCustomValidation(object value, ValidationContext validationContext)
		{
			if (value.IsNull())
			{
				return ValidationResult.Success;
			}

			string convertedValue;

			if (validationContext.IsNull())
			{
				validationContext = new ValidationContext(value, null, null) { DisplayName = "The value" };
			}

			var memberNames = new[] { validationContext.MemberName };

			try
			{
				convertedValue = Convert.ToString(value);
			}
			catch (FormatException)
			{
				this.ErrorMessage = validationContext.DisplayName.FormattedWith(ValidationMessages.ValueMustBeString);

				return new ValidationResult(this.ErrorMessage);
			}

			// Remove any of the extra permitted characters before applying the IsAlpha rule...
			foreach (string character in this.extraCharacters)
			{
				convertedValue = convertedValue.Replace(character, string.Empty);
			}

			// The convertedValue might be an empty string after we strip out the extra characters. This should not
			// cause validation to fail.
			if (!string.IsNullOrEmpty(convertedValue) && !convertedValue.IsAlpha())
			{
				if (this.extraCharacters.Length == 0)
				{
					this.ErrorMessage = validationContext.DisplayName.FormattedWith(ValidationMessages.CanOnlyContainLetters);
				}
				else
				{
					this.ErrorMessage = string.Format(
						ValidationMessages.CanOnlyContainLettersAndSpecialCharacters,
						validationContext.DisplayName,
						string.Join(string.Empty, this.extraCharacters));
				}

				return new ValidationResult(this.ErrorMessage, memberNames);
			}

			return ValidationResult.Success;
		}
开发者ID:KaraokeStu,项目名称:LeadPipe.Net,代码行数:66,代码来源:AlphaAttribute.cs

示例5: PerformCustomValidation

		/// <summary>
		/// Validates the specified value with respect to the current validation attribute.
		/// </summary>
		/// <param name="value">
		/// The value to validate.
		/// </param>
		/// <param name="validationContext">
		/// The context information about the validation operation.
		/// </param>
		/// <returns>
		/// An instance of the <see cref="T:System.ComponentModel.DataAnnotations.ValidationResult"/> class.
		/// </returns>
		protected override ValidationResult PerformCustomValidation(object value, ValidationContext validationContext)
		{
			if (value.IsNull())
			{
				return ValidationResult.Success;
			}

			string convertedValue;
			if (validationContext.IsNull())
			{
				validationContext = new ValidationContext(value, null, null) { DisplayName = "The value" };
			}

			var memberNames = new[] { validationContext.MemberName };

			try
			{
				convertedValue = Convert.ToString(value);
			}
			catch (FormatException)
			{
				this.ErrorMessage = validationContext.DisplayName.FormattedWith(ValidationMessages.ValueMustBeString);

				return new ValidationResult(this.ErrorMessage);
			}

			if (!convertedValue.IsPrintableCharacter())
			{
				var englishAlphabetProvider = new EnglishAlphabetProvider();
				string extendedCharacters = string.Join(string.Empty, englishAlphabetProvider.GetPrintableCharacters());

				this.ErrorMessage = string.Format(ValidationMessages.CanOnlyContainPrintableCharacters, validationContext.DisplayName, extendedCharacters);

				return new ValidationResult(this.ErrorMessage, memberNames);
			}

			return ValidationResult.Success;
		}
开发者ID:KaraokeStu,项目名称:LeadPipe.Net,代码行数:50,代码来源:PrintableCharacterAttribute.cs


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