public void EqualToAttribute_SetsOtherPropertyName() { String actual = new EqualToAttribute("Other").OtherPropertyName; String expected = "Other"; Assert.Equal(expected, actual); }
public void EqualToAttribute_SetsOtherPropertyName() { String actual = new EqualToAttribute("Other").OtherPropertyName; String expected = "Other"; Assert.Equal(expected, actual); }
/// <summary> /// 验证输入是否和目标ID元素的值相等 /// </summary> /// <param name="box">验证框</param> /// <param name="targetID">目标元素ID</param> /// <param name="errorMessage">提示信息</param> /// <returns></returns> public static ValidBox EqualTo(this ValidBox box, string targetID, string errorMessage) { var newBox = new EqualToAttribute(targetID) { ErrorMessage = errorMessage }.ToValidBox(); return(ValidBox.Merge(box, newBox)); }
public void GetValidationResult_NoProperty_Error() { attribute = new EqualToAttribute("Temp"); ValidationContext context = new ValidationContext(new AdaptersModel()); String actual = attribute.GetValidationResult("Test", context).ErrorMessage; String expected = String.Format(Validations.EqualTo, context.DisplayName, "Temp"); Assert.Equal(expected, actual); }
public void ValidateDoesNotThrowWhenComparedObjectsAreEqual() { object otherObject = new CompareObject("test"); CompareObject currentObject = new CompareObject("test"); ValidationContext testContext = new ValidationContext(otherObject, null, null); EqualToAttribute attr = new EqualToAttribute("CompareProperty"); attr.Validate(currentObject.CompareProperty, testContext); }
public void GetValidationResult_NoProperty_Error() { attribute = new EqualToAttribute("Temp"); ValidationContext context = new ValidationContext(new AllTypesView()); String actual = attribute.GetValidationResult("Test", context).ErrorMessage; String expected = Validation.For("EqualTo", context.DisplayName, "Temp"); Assert.Equal(expected, actual); }
public void ValidateThrowsWhenPropertyNameIsUnknown() { CompareObject currentObject = new CompareObject("a"); object otherObject = new CompareObject("b"); ValidationContext testContext = new ValidationContext(otherObject, null, null); testContext.DisplayName = "CurrentProperty"; EqualToAttribute attr = new EqualToAttribute("UnknownPropertyName"); attr.Validate(currentObject.CompareProperty, testContext); }
public void GetClientValidationRules_ReturnsEqualToValidationRule() { ModelMetadata metadata = new DataAnnotationsModelMetadataProvider().GetMetadataForProperty(null, typeof(AdaptersModel), "EqualTo"); EqualToAdapter adapter = new EqualToAdapter(metadata, new ControllerContext(), new EqualToAttribute("StringLength")); String expectedMessage = new EqualToAttribute("StringLength").FormatErrorMessage(metadata.GetDisplayName()); ModelClientValidationRule actual = adapter.GetClientValidationRules().Single(); Assert.Equal("*.StringLength", actual.ValidationParameters["other"]); Assert.Equal(expectedMessage, actual.ErrorMessage); Assert.Equal("equalto", actual.ValidationType); Assert.Single(actual.ValidationParameters); }
public void GetClientValidationRules_SetsOtherPropertyDisplayName() { ModelMetadata metadata = new DataAnnotationsModelMetadataProvider().GetMetadataForProperty(null, typeof(AdaptersModel), "EqualTo"); EqualToAttribute attribute = new EqualToAttribute("StringLength"); attribute.OtherPropertyDisplayName = null; new EqualToAdapter(metadata, new ControllerContext(), attribute).GetClientValidationRules(); String expected = ResourceProvider.GetPropertyTitle(typeof(AdaptersModel), "EqualTo"); String actual = attribute.OtherPropertyDisplayName; Assert.Equal(expected, actual); }
public void GetClientValidationRules_ReturnsEqualToValidationRule() { IServiceProvider services = Substitute.For <IServiceProvider>(); IModelMetadataProvider provider = new EmptyModelMetadataProvider(); ModelMetadata metadata = provider.GetMetadataForProperty(typeof(AdaptersModel), "EqualTo"); EqualToAdapter adapter = new EqualToAdapter(new EqualToAttribute("StringLength")); ClientModelValidationContext context = new ClientModelValidationContext(metadata, provider, services); String expectedMessage = new EqualToAttribute("StringLength").FormatErrorMessage("EqualTo"); ModelClientValidationRule actual = adapter.GetClientValidationRules(context).Single(); Assert.Equal("*.StringLength", actual.ValidationParameters["other"]); Assert.Equal(expectedMessage, actual.ErrorMessage); Assert.Equal("equalto", actual.ValidationType); Assert.Single(actual.ValidationParameters); }
public void GetClientValidationRules_SetsOtherPropertyDisplayName() { IServiceProvider services = Substitute.For <IServiceProvider>(); IModelMetadataProvider provider = new EmptyModelMetadataProvider(); ModelMetadata metadata = provider.GetMetadataForProperty(typeof(AdaptersModel), "EqualTo"); EqualToAttribute attribute = new EqualToAttribute("EqualTo"); attribute.OtherPropertyDisplayName = null; ClientModelValidationContext context = new ClientModelValidationContext(metadata, provider, services); new EqualToAdapter(attribute).GetClientValidationRules(context); String expected = ResourceProvider.GetPropertyTitle(typeof(AdaptersModel), "EqualTo"); String actual = attribute.OtherPropertyDisplayName; Assert.Equal(expected, actual); }
public void ErrorMessageTest() { var currentObject = new CompareObject("a"); object otherObject = new CompareObject("b"); var testContext = new ValidationContext(otherObject, null, null); testContext.DisplayName = "CurrentProperty"; var attribute = new EqualToAttribute("CompareProperty") { ErrorMessage = "SampleErrorMessage" }; var result = attribute.GetValidationResult(currentObject, new ValidationContext(otherObject, null, null)); Assert.AreEqual("SampleErrorMessage", result.ErrorMessage); }
public void ErrorResourcesTest() { var currentObject = new CompareObject("a"); object otherObject = new CompareObject("b"); var testContext = new ValidationContext(otherObject, null, null); testContext.DisplayName = "CurrentProperty"; var attribute = new EqualToAttribute("CompareProperty") { ErrorMessageResourceName = "ErrorMessage", ErrorMessageResourceType = typeof(ErrorResources) }; var result = attribute.GetValidationResult(currentObject, new ValidationContext(otherObject, null, null)); Assert.AreEqual(ErrorResources.ErrorMessage, result.ErrorMessage); }
public void GlobalizedErrorResourcesTest() { System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("es-MX"); var currentObject = new CompareObject("a"); object otherObject = new CompareObject("b"); var testContext = new ValidationContext(otherObject, null, null); testContext.DisplayName = "CurrentProperty"; var attribute = new EqualToAttribute("CompareProperty") { ErrorMessageResourceName = "ErrorMessage", ErrorMessageResourceType = typeof(ErrorResources) }; var result = attribute.GetValidationResult(currentObject, new ValidationContext(otherObject, null, null)); Assert.AreEqual("mensaje de error", result.ErrorMessage); }
/// <summary> /// 验证输入是否和目标ID元素的值相等 /// </summary> /// <param name="box">验证框</param> /// <param name="targetID">目标元素ID</param> /// <returns></returns> public static ValidBox EqualTo(this ValidBox box, string targetID) { var newBox = new EqualToAttribute(targetID).ToValidBox(); return(ValidBox.Merge(box, newBox)); }
public EqualToAttributeTests() { attribute = new EqualToAttribute("Total"); }
public EqualToAttributeTests() { attribute = new EqualToAttribute("Total"); }
public EqualToAttributeTests() { attribute = new EqualToAttribute("EqualTo"); }
public EqualToAttributeTests() { attribute = new EqualToAttribute("StringField"); }
public EqualToAttributeTests() { attribute = new EqualToAttribute(nameof(AllTypesView.StringField)); }
private static string WriteExtenders(PropertyInfo p, out bool isValidatable) { isValidatable = false; StringBuilder sb = new StringBuilder(); sb.Append(".extend({ editState : false, disableValidation : false, empty : true })"); RequiredAttribute requiredAttribute = p.GetCustomAttribute <RequiredAttribute>(); if (requiredAttribute != null) { sb.Append($".extend({{ required: {{ message: \"{requiredAttribute.FormatErrorMessage(p.Name)}\", onlyIf: function() {{ return ko.utils.isPropertyValidatable(self, \"{ p.Name}\"); }} }} }})"); isValidatable = true; } MinLengthAttribute minLengthAttribute = p.GetCustomAttribute <MinLengthAttribute>(); if (minLengthAttribute != null) { sb.Append(string.Format(".extend({ minLength: { message: \"{0}\", params: {1}, onlyIf: function() { return ko.utils.isPropertyValidatable(self, \"{2}\"); } } })", minLengthAttribute.FormatErrorMessage(p.Name), minLengthAttribute.Length, p.Name)); isValidatable = true; } MaxLengthAttribute maxLengthAttribute = p.GetCustomAttribute <MaxLengthAttribute>(); if (maxLengthAttribute != null) { sb.Append(string.Format(".extend({ maxLength: { message: \"{0}\", params: {1}, onlyIf: function() { return ko.utils.isPropertyValidatable(self, \"{2}\"); } } })", maxLengthAttribute.FormatErrorMessage(p.Name), maxLengthAttribute.Length, p.Name)); isValidatable = true; } RegularExpressionAttribute regularExpressionAttribute = p.GetCustomAttribute <RegularExpressionAttribute>(); if (regularExpressionAttribute != null) { sb.Append(string.Format(".extend({ pattern: { message: \"{0}\", params: /{1}/, onlyIf: function() { return ko.utils.isPropertyValidatable(self, \"{2}\"); } } })", regularExpressionAttribute.FormatErrorMessage(p.Name), regularExpressionAttribute.Pattern, p.Name)); isValidatable = true; } UrlAttribute urlAttribute = p.GetCustomAttribute <UrlAttribute>(); if (urlAttribute != null) { sb.Append(string.Format(".extend({ pattern: { message: \"{0}\", params: /{1}/, onlyIf: function() { return ko.utils.isPropertyValidatable(self, \"{2}\"); } } })", urlAttribute.FormatErrorMessage(p.Name), urlAttribute, p.Name)); isValidatable = true; } DateAttribute dateAttribute = p.GetCustomAttribute <DateAttribute>(); if (dateAttribute != null) { sb.Append(string.Format(".extend({ date: { message: \"{0}\", onlyIf: function() { return ko.utils.isPropertyValidatable(self, \"{1}\"); } } })", dateAttribute.FormatErrorMessage(p.Name), p.Name)); isValidatable = true; } NumericAttribute numericAttribute = p.GetCustomAttribute <NumericAttribute>(); if (numericAttribute != null) { sb.Append( string.Format(".extend({ number: { message: \"{0}\", onlyIf: function() { return ko.utils.isPropertyValidatable(self, \"{1}\"); } } }).extend({ numeric: {2} })", numericAttribute.FormatErrorMessage(p.Name), p.Name, numericAttribute.Precision)); isValidatable = true; } EmailAttribute emailAttribute = p.GetCustomAttribute <EmailAttribute>(); if (emailAttribute != null) { sb.Append(string.Format(".extend({ email: { message: \"{0}\", onlyIf: function() { return ko.utils.isPropertyValidatable(self, \"{1}\"); } } })", emailAttribute.FormatErrorMessage(p.Name), p.Name)); isValidatable = true; } DigitsAttribute digitsAttribute = p.GetCustomAttribute <DigitsAttribute>(); if (digitsAttribute != null) { sb.Append(string.Format(".extend({ digit: { message: \"{0}\", onlyIf: function() { return ko.utils.isPropertyValidatable(self, \"{1}\"); } } }).extend({ numeric: 0 })", digitsAttribute.FormatErrorMessage(p.Name), p.Name)); isValidatable = true; } MinAttribute minAttribute = p.GetCustomAttribute <MinAttribute>(); if (minAttribute != null) { sb.Append(string.Format(".extend({ min: { message: \"{0}\", params: {1}, onlyIf: function() { return ko.utils.isPropertyValidatable(self, \"{2}\"); } } })", minAttribute.FormatErrorMessage(p.Name), minAttribute.Min, p.Name)); isValidatable = true; } MaxAttribute maxAttribute = p.GetCustomAttribute <MaxAttribute>(); if (maxAttribute != null) { sb.Append(string.Format(".extend({ max: { message: \"{0}\", params: {1}, onlyIf: function() { return ko.utils.isPropertyValidatable(self, \"{2}\"); } } })", maxAttribute.FormatErrorMessage(p.Name), maxAttribute.Max, p.Name)); isValidatable = true; } EqualToAttribute equalToAttribute = p.GetCustomAttribute <EqualToAttribute>(); if (equalToAttribute != null) { sb.Append(string.Format(".extend({ equal: { message: \"{0}\", params: {1}, onlyIf: function() { return ko.utils.isPropertyValidatable(self, \"{2}\"); } } })", equalToAttribute.FormatErrorMessage(p.Name), equalToAttribute.OtherProperty, p.Name)); isValidatable = true; } CompareAttribute compareAttribute = p.GetCustomAttribute <CompareAttribute>(); if (compareAttribute != null) { sb.Append(string.Format(".extend({ equal: { message: \"{0}\", params: \"{1}\", onlyIf: function() { return ko.utils.isPropertyValidatable(self, \"{2}\"); } } })", compareAttribute.FormatErrorMessage(p.Name), compareAttribute.OtherProperty, p.Name)); isValidatable = true; } FormatterAttribute formatterAttribute = p.GetCustomAttribute <FormatterAttribute>(); if (formatterAttribute != null) { sb.Append(string.Format(".formatted({0}, {1})", formatterAttribute.Formatter, JsonConvert.SerializeObject(formatterAttribute.Arguments))); } return(sb.ToString()); }
public void SetUp() { attribute = new EqualToAttribute("Total"); }