本文整理汇总了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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}