本文整理汇总了C#中System.ComponentModel.DataAnnotations.ValidationAttribute类的典型用法代码示例。如果您正苦于以下问题:C# ValidationAttribute类的具体用法?C# ValidationAttribute怎么用?C# ValidationAttribute使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ValidationAttribute类属于System.ComponentModel.DataAnnotations命名空间,在下文中一共展示了ValidationAttribute类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ViewModelPropertyValidationRule
public ViewModelPropertyValidationRule(string clientRule, ValidationAttribute sourceValidationAttribute, string errorMessage, params object[] parameters)
{
ClientRuleName = clientRule;
SourceValidationAttribute = sourceValidationAttribute;
ErrorMessage = errorMessage;
Parameters = parameters;
}
示例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);
}
示例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;
}
示例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;
}
示例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;
}
}
示例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];
}
示例7: MyValidator
public MyValidator(ValidationAttribute attribute, string errorMsg, ModelMetadata metadata, ControllerContext controllerContext, IEnumerable<ModelClientValidationRule> create)
: base(metadata, controllerContext)
{
_attribute = attribute;
_errorMsg = errorMsg;
_create = create;
}
示例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;
}
示例9: DataAnnotationsModelValidator
public DataAnnotationsModelValidator(ModelMetadata metadata, ControllerContext context, ValidationAttribute attribute)
: base(metadata, context)
{
if (attribute == null) {
throw new ArgumentNullException("attribute");
}
Attribute = attribute;
}
示例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;
}
示例11: DataAnnotationsModelValidator
public DataAnnotationsModelValidator(ValidationAttribute attribute, IStringLocalizer stringLocalizer)
{
if (attribute == null)
{
throw new ArgumentNullException(nameof(attribute));
}
Attribute = attribute;
_stringLocalizer = stringLocalizer;
}
示例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;
}
示例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);
}
}
示例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);
}
示例15: DataAnnotationsModelValidator
public DataAnnotationsModelValidator(IEnumerable<ModelValidatorProvider> validatorProviders, ValidationAttribute attribute)
: base(validatorProviders)
{
if (attribute == null)
{
throw Error.ArgumentNull("attribute");
}
Attribute = attribute;
}