本文整理汇总了C#中ModelMetadata.GetValidators方法的典型用法代码示例。如果您正苦于以下问题:C# ModelMetadata.GetValidators方法的具体用法?C# ModelMetadata.GetValidators怎么用?C# ModelMetadata.GetValidators使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ModelMetadata
的用法示例。
在下文中一共展示了ModelMetadata.GetValidators方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetValidators_should_return_validators_for_item_in_list_and_validators_for_itself
public void GetValidators_should_return_validators_for_item_in_list_and_validators_for_itself()
{
// Arrange
var model = new ParentClass
{
Children = new List<NestedClass>
{
new NestedClass(),
new NestedClass()
}
};
// Action
var metaData = new ModelMetadata(ModelMetadataProviders.Current,
typeof(ParentClass),
() => model.Children,
typeof(List<NestedClass>),
"Children");
var validators = metaData.GetValidators().ToList();
// Assert
Assert.AreEqual(3, validators.Count());
}
示例2: LoadMaxLength
private static void LoadMaxLength(HtmlHelper html, ModelMetadata metadata, IDictionary<string, object> editorAttributes)
{
Throw.IfNullArgument(editorAttributes, "editorAttributes");
var maxlength = -1;
var validators = metadata.GetValidators(html.ViewContext.Controller.ControllerContext);
var lengthLimits = validators.OfType<StringLengthAttributeAdapter>();
foreach (var limit in lengthLimits)
{
foreach (var rule in limit.GetClientValidationRules())
{
var max = rule.ValidationParameters.Get("max", -1);
if (max != -1)
{
if (maxlength == -1 || max < maxlength)
maxlength = max;
}
}
}
if (maxlength != -1 && !editorAttributes.ContainsKey("maxlength"))
editorAttributes.Add("maxlength", maxlength);
}
示例3: CreateModelFieldFromMeta
public static ModelField CreateModelFieldFromMeta(ModelMetadata propertyMetadata, Ext.Net.Model storeModel = null, ModelFieldAttribute modelAttr = null, ControllerContext controllerContext = null)
{
var modelField = new ModelField();
if (modelAttr == null)
{
modelAttr = propertyMetadata.AdditionalValues.ContainsKey(ModelFieldAttribute.KEY) ? (ModelFieldAttribute)propertyMetadata.AdditionalValues[ModelFieldAttribute.KEY] : null;
}
modelField.Name = propertyMetadata.PropertyName;
if (Store.IsDate(propertyMetadata.ModelType))
{
modelField.Type = ModelFieldType.Date;
}
else if (Store.IsInteger(propertyMetadata.ModelType))
{
modelField.Type = ModelFieldType.Int;
}
else if (Store.IsFloat(propertyMetadata.ModelType))
{
modelField.Type = ModelFieldType.Float;
}
else if (Store.IsBoolean(propertyMetadata.ModelType))
{
modelField.Type = ModelFieldType.Boolean;
}
else if (propertyMetadata.ModelType == typeof(string))
{
modelField.Type = ModelFieldType.String;
}
if (propertyMetadata.IsComplexType && modelAttr != null)
{
modelField.IsComplex = true;
}
if (modelAttr != null && storeModel != null)
{
modelAttr.CopyTo(modelField, storeModel);
}
if(storeModel != null)
{
storeModel.Fields.Add(modelField);
}
ValidationCollection validations = new ValidationCollection();
if (propertyMetadata.AdditionalValues.ContainsKey(AbstractValidationAttribute.KEY))
{
validations = (ValidationCollection)propertyMetadata.AdditionalValues[AbstractValidationAttribute.KEY];
}
foreach (ModelValidator v in propertyMetadata.GetValidators(controllerContext))
{
var rule = v.GetClientValidationRules().FirstOrDefault();
if (rule != null)
{
switch (rule.ValidationType)
{
case "required":
validations.Add(new PresenceValidation { Message = rule.ErrorMessage });
break;
case "regex":
if (rule.ValidationParameters.ContainsKey("pattern"))
{
validations.Add(new FormatValidation { Matcher = rule.ValidationParameters["pattern"].ToString(), Message = rule.ErrorMessage });
}
break;
case "length":
int num;
LengthValidation length = new LengthValidation();
length.Message = rule.ErrorMessage;
if (rule.ValidationParameters.ContainsKey("max"))
{
if (int.TryParse(rule.ValidationParameters["max"].ToString(), out num))
{
length.Max = num;
}
else
{
continue;
}
}
if (rule.ValidationParameters.ContainsKey("min"))
{
if (int.TryParse(rule.ValidationParameters["min"].ToString(), out num))
{
length.Min = num;
}
else
{
continue;
}
}
validations.Add(length);
break;
}
}
//.........这里部分代码省略.........
示例4: ApplyFieldValidationMetadata
private static FieldValidationMetadata ApplyFieldValidationMetadata(HtmlHelper htmlHelper, ModelMetadata modelMetadata, string modelName)
{
FieldValidationMetadata validationMetadataForField = htmlHelper.ViewContext.FormContext.GetValidationMetadataForField(modelName, true);
foreach (IEnumerable<ModelClientValidationRule> rules in
(from v in modelMetadata.GetValidators(htmlHelper.ViewContext.Controller.ControllerContext) select v.GetClientValidationRules()))
{
foreach (var rule in rules)
{
validationMetadataForField.ValidationRules.Add(rule);
}
}
return validationMetadataForField;
}