本文整理汇总了C#中FormContext.GetValidationMetadataForField方法的典型用法代码示例。如果您正苦于以下问题:C# FormContext.GetValidationMetadataForField方法的具体用法?C# FormContext.GetValidationMetadataForField怎么用?C# FormContext.GetValidationMetadataForField使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类FormContext
的用法示例。
在下文中一共展示了FormContext.GetValidationMetadataForField方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetValidationMetadataForField_Create_CreatesNewMetadataIfNotFound
public void GetValidationMetadataForField_Create_CreatesNewMetadataIfNotFound()
{
// Arrange
FormContext context = new FormContext();
// Act
FieldValidationMetadata result = context.GetValidationMetadataForField("fieldName", true /* createIfNotFound */);
// Assert
Assert.NotNull(result);
Assert.Equal("fieldName", result.FieldName);
Assert.Single(context.FieldValidators);
Assert.Equal(result, context.FieldValidators["fieldName"]);
}
示例2: Validate_AddsClientValidationMetadata
public void Validate_AddsClientValidationMetadata()
{
var originalProviders = ModelValidatorProviders.Providers.ToArray();
ModelValidatorProviders.Providers.Clear();
try
{
// Arrange
HtmlHelper htmlHelper = MvcHelper.GetHtmlHelper(GetViewDataWithModelErrors());
FormContext formContext = new FormContext()
{
FormId = "form_id"
};
htmlHelper.ViewContext.ClientValidationEnabled = true;
htmlHelper.ViewContext.FormContext = formContext;
ModelClientValidationRule[] expectedValidationRules = new ModelClientValidationRule[]
{
new ModelClientValidationRule() { ValidationType = "ValidationRule1" },
new ModelClientValidationRule() { ValidationType = "ValidationRule2" }
};
Mock<ModelValidator> mockValidator = new Mock<ModelValidator>(ModelMetadata.FromStringExpression("", htmlHelper.ViewContext.ViewData), htmlHelper.ViewContext);
mockValidator.Setup(v => v.GetClientValidationRules())
.Returns(expectedValidationRules);
Mock<ModelValidatorProvider> mockValidatorProvider = new Mock<ModelValidatorProvider>();
mockValidatorProvider.Setup(vp => vp.GetValidators(It.IsAny<ModelMetadata>(), It.IsAny<ControllerContext>()))
.Returns(new[] { mockValidator.Object });
ModelValidatorProviders.Providers.Add(mockValidatorProvider.Object);
// Act
htmlHelper.Validate("baz");
// Assert
Assert.NotNull(formContext.GetValidationMetadataForField("baz"));
Assert.Equal(expectedValidationRules, formContext.FieldValidators["baz"].ValidationRules.ToArray());
}
finally
{
ModelValidatorProviders.Providers.Clear();
foreach (var provider in originalProviders)
{
ModelValidatorProviders.Providers.Add(provider);
}
}
}
示例3: GetValidationMetadataForField_NoCreate_ReturnsMetadataIfFound
public void GetValidationMetadataForField_NoCreate_ReturnsMetadataIfFound()
{
// Arrange
FormContext context = new FormContext();
FieldValidationMetadata metadata = new FieldValidationMetadata();
context.FieldValidators["fieldName"] = metadata;
// Act
FieldValidationMetadata result = context.GetValidationMetadataForField("fieldName");
// Assert
Assert.NotNull(result);
Assert.Equal(metadata, result);
}
示例4: GetValidationMetadataForFieldThrowsIfFieldNameIsNull
public void GetValidationMetadataForFieldThrowsIfFieldNameIsNull()
{
// Arrange
FormContext context = new FormContext();
// Act & assert
Assert.ThrowsArgumentNullOrEmpty(
delegate { context.GetValidationMetadataForField(null); }, "fieldName");
}
示例5: GetValidationMetadataForField_NoCreate_ReturnsNullIfNotFound
public void GetValidationMetadataForField_NoCreate_ReturnsNullIfNotFound()
{
// Arrange
FormContext context = new FormContext();
// Act
FieldValidationMetadata result = context.GetValidationMetadataForField("fieldName");
// Assert
Assert.Null(result);
}
示例6: ValidationMessageWithClientValidation_ExplicitMessage_Valid
public void ValidationMessageWithClientValidation_ExplicitMessage_Valid()
{
var originalProviders = ModelValidatorProviders.Providers.ToArray();
ModelValidatorProviders.Providers.Clear();
try
{
// Arrange
HtmlHelper htmlHelper = MvcHelper.GetHtmlHelper(GetViewDataWithModelErrors());
FormContext formContext = new FormContext();
htmlHelper.ViewContext.ClientValidationEnabled = true;
htmlHelper.ViewContext.FormContext = formContext;
ModelClientValidationRule[] expectedValidationRules = new ModelClientValidationRule[]
{
new ModelClientValidationRule() { ValidationType = "ValidationRule1" },
new ModelClientValidationRule() { ValidationType = "ValidationRule2" }
};
Mock<ModelValidator> mockValidator = new Mock<ModelValidator>(ModelMetadata.FromStringExpression("", htmlHelper.ViewContext.ViewData), htmlHelper.ViewContext);
mockValidator.Setup(v => v.GetClientValidationRules())
.Returns(expectedValidationRules);
Mock<ModelValidatorProvider> mockValidatorProvider = new Mock<ModelValidatorProvider>();
mockValidatorProvider.Setup(vp => vp.GetValidators(It.IsAny<ModelMetadata>(), It.IsAny<ControllerContext>()))
.Returns(new[] { mockValidator.Object });
ModelValidatorProviders.Providers.Add(mockValidatorProvider.Object);
// Act
MvcHtmlString html = htmlHelper.ValidationMessage("baz", "some explicit message"); // 'baz' is valid
// Assert
Assert.Equal("<span class=\"field-validation-valid\" id=\"baz_validationMessage\">some explicit message</span>", html.ToHtmlString());
Assert.NotNull(formContext.GetValidationMetadataForField("baz"));
Assert.Equal("baz_validationMessage", formContext.FieldValidators["baz"].ValidationMessageId);
Assert.False(formContext.FieldValidators["baz"].ReplaceValidationMessageContents);
Assert.Equal(expectedValidationRules, formContext.FieldValidators["baz"].ValidationRules.ToArray());
}
finally
{
ModelValidatorProviders.Providers.Clear();
foreach (var provider in originalProviders)
{
ModelValidatorProviders.Providers.Add(provider);
}
}
}
示例7: ValidationMessageForWithClientValidation
public void ValidationMessageForWithClientValidation() {
var originalProviders = ModelValidatorProviders.Providers.ToArray();
ModelValidatorProviders.Providers.Clear();
try {
// Arrange
HtmlHelper<ValidationModel> htmlHelper = MvcHelper.GetHtmlHelper(GetViewDataWithModelErrors());
FormContext formContext = new FormContext();
htmlHelper.ViewContext.ClientValidationEnabled = true;
htmlHelper.ViewContext.FormContext = formContext;
ModelClientValidationRule[] expectedValidationRules = new ModelClientValidationRule[] {
new ModelClientValidationRule() { ValidationType = "ValidationRule1" },
new ModelClientValidationRule() { ValidationType = "ValidationRule2" }
};
Mock<ModelValidator> mockValidator = new Mock<ModelValidator>(ModelMetadata.FromStringExpression("", htmlHelper.ViewContext.ViewData), htmlHelper.ViewContext);
mockValidator.Setup(v => v.GetClientValidationRules())
.Returns(expectedValidationRules);
Mock<ModelValidatorProvider> mockValidatorProvider = new Mock<ModelValidatorProvider>();
mockValidatorProvider.Setup(vp => vp.GetValidators(It.IsAny<ModelMetadata>(), It.IsAny<ControllerContext>()))
.Returns(new[] { mockValidator.Object });
ModelValidatorProviders.Providers.Add(mockValidatorProvider.Object);
// Act
MvcHtmlString html = htmlHelper.ValidationMessageFor(m => m.baz);
// Assert
Assert.AreEqual(@"<span class=""field-validation-valid"" id=""baz_validationMessage""></span>", html.ToHtmlString(),
"ValidationMessageFor() should always return something if client validation is enabled.");
Assert.IsNotNull(formContext.GetValidationMetadataForField("baz"));
Assert.AreEqual("baz_validationMessage", formContext.FieldValidators["baz"].ValidationMessageId);
CollectionAssert.AreEqual(expectedValidationRules, formContext.FieldValidators["baz"].ValidationRules.ToArray());
}
finally {
ModelValidatorProviders.Providers.Clear();
foreach (var provider in originalProviders) {
ModelValidatorProviders.Providers.Add(provider);
}
}
}
示例8: ValidationMessageWithClientValidation
public void ValidationMessageWithClientValidation() {
var originalProvider = ModelValidatorProviders.Current;
try {
// Arrange
HtmlHelper htmlHelper = HtmlHelperTest.GetHtmlHelper(GetViewDataWithModelErrors());
FormContext formContext = new FormContext() {
ClientValidationEnabled = true,
FormId = "form_id"
};
htmlHelper.ViewContext.FormContext = formContext;
ModelClientValidationRule[] expectedValidationRules = new ModelClientValidationRule[] {
new ModelClientValidationRule() { ValidationType = "ValidationRule1" },
new ModelClientValidationRule() { ValidationType = "ValidationRule2" }
};
Mock<ModelValidator> mockValidator = new Mock<ModelValidator>(ModelMetadata.FromStringExpression("", htmlHelper.ViewContext.ViewData), htmlHelper.ViewContext);
mockValidator.Expect(v => v.GetClientValidationRules())
.Returns(expectedValidationRules);
Mock<ModelValidatorProvider> mockValidatorProvider = new Mock<ModelValidatorProvider>();
mockValidatorProvider.Expect(vp => vp.GetValidators(It.IsAny<ModelMetadata>(), It.IsAny<ControllerContext>()))
.Returns(new[] { mockValidator.Object });
ModelValidatorProviders.Current = mockValidatorProvider.Object;
// Act
MvcHtmlString html = htmlHelper.ValidationMessage("baz");
// Assert
Assert.AreEqual(@"<span class=""field-validation-error"" id=""form_id_baz_validator""></span>", html.ToHtmlString(),
"ValidationMessage() should always return something if client validation is enabled.");
Assert.IsNotNull(formContext.GetValidationMetadataForField("baz"));
Assert.AreEqual("form_id_baz_validator", formContext.FieldValidators["baz"].ValidatorId);
CollectionAssert.AreEqual(expectedValidationRules, formContext.FieldValidators["baz"].ValidationRules.ToArray());
}
finally {
ModelValidatorProviders.Current = originalProvider;
}
}