public void ClientRulesWithCompareAttribute_ErrorMessageUsesResourceOverride() { // Arrange ModelMetadata metadata = ModelMetadataProviders.Current.GetMetadataForProperty( () => null, typeof(PropertyNameModel), "MyProperty" ); ControllerContext context = new ControllerContext(); AnnotationsCompareAttribute attribute = new AnnotationsCompareAttribute("OtherProperty") { ErrorMessageResourceName = "CompareAttributeTestResource", ErrorMessageResourceType = typeof(MyResources), }; ModelValidator adapter = new CompareAttributeAdapter(metadata, context, attribute); // Act ModelClientValidationRule[] rules = adapter .GetClientValidationRules() .OrderBy(r => r.ValidationType) .ToArray(); // Assert ModelClientValidationRule rule = Assert.Single(rules); Assert.Equal("Hello 'MyProperty', goodbye 'OtherProperty'.", rule.ErrorMessage); }
public CompareAttributeWrapper(DataAnnotationsCompareAttribute attribute, ModelMetadata metadata) : base(attribute.OtherProperty) { _otherPropertyDisplayName = attribute.OtherPropertyDisplayName; if (_otherPropertyDisplayName == null && metadata.ContainerType != null) { _otherPropertyDisplayName = ModelMetadataProviders.Current.GetMetadataForProperty(() => metadata.Model, metadata.ContainerType, attribute.OtherProperty).GetDisplayName(); } if (_otherPropertyDisplayName == null) { _otherPropertyDisplayName = attribute.OtherProperty; } // Copy settable properties from wrapped attribute. Don't reset default message accessor (set as // CompareAttribute constructor calls ValidationAttribute constructor) when all properties are null to // preserve default error message. Reset the message accessor when just ErrorMessageResourceType is // non-null to ensure correct InvalidOperationException. if (!String.IsNullOrEmpty(attribute.ErrorMessage) || !String.IsNullOrEmpty(attribute.ErrorMessageResourceName) || attribute.ErrorMessageResourceType != null) { ErrorMessage = attribute.ErrorMessage; ErrorMessageResourceName = attribute.ErrorMessageResourceName; ErrorMessageResourceType = attribute.ErrorMessageResourceType; } }
public CompareAttributeWrapper(DataAnnotationsCompareAttribute attribute, ModelMetadata metadata) : base(attribute.OtherProperty) { _otherPropertyDisplayName = attribute.OtherPropertyDisplayName; if (_otherPropertyDisplayName == null && metadata.ContainerType != null) { _otherPropertyDisplayName = ModelMetadataProviders.Current.GetMetadataForProperty(() => metadata.Model, metadata.ContainerType, attribute.OtherProperty).GetDisplayName(); } }
public IEnumerable <ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context) { string name = ""; if (metadata.ContainerType != null && this.OtherPropertyDisplayName == null) { CompareAttribute displayName = this; ModelMetadataProvider current = ModelMetadataProviders.Current; name = current.GetMetadataForProperty(() => metadata.Model, metadata.ContainerType, this.OtherProperty).GetDisplayName(); } ILocalizationService _LocalizationService = DependencyResolver.Current.GetService <ILocalizationService>(); yield return(new ModelClientValidationEqualToRule(String.Format(CultureInfo.CurrentCulture, _LocalizationService.LocalizeString(ErrorMessageString, CultureInfo.CurrentCulture.Name), name, OtherPropertyDisplayName ?? OtherProperty), FormatPropertyForClientValidation(this.OtherProperty))); }
public void ClientRulesWithCompareAttribute_ErrorMessageUsesPropertyName() { // Arrange var metadata = ModelMetadataProviders.Current.GetMetadataForProperty(() => null, typeof(PropertyNameModel), "MyProperty"); var context = new ControllerContext(); var attribute = new System.ComponentModel.DataAnnotations.CompareAttribute("OtherProperty"); var adapter = new CompareAttributeAdapter(metadata, context, attribute); // Act var rules = adapter.GetClientValidationRules() .OrderBy(r => r.ValidationType) .ToArray(); // Assert ModelClientValidationRule rule = Assert.Single(rules); Assert.Equal("'MyProperty' and 'OtherProperty' do not match.", rule.ErrorMessage); }
public void ClientRulesWithCompareAttribute_ErrorMessageUsesPropertyName() { // Arrange var metadata = ModelMetadataProviders.Current.GetMetadataForProperty(() => null, typeof(PropertyNameModel), "MyProperty"); var context = new ControllerContext(); var attribute = new AnnotationsCompareAttribute("OtherProperty"); var adapter = new CompareAttributeAdapter(metadata, context, attribute); // Act var rules = adapter.GetClientValidationRules() .OrderBy(r => r.ValidationType) .ToArray(); // Assert ModelClientValidationRule rule = Assert.Single(rules); Assert.Equal("'MyProperty' and 'OtherProperty' do not match.", rule.ErrorMessage); }
public void ClientRulesWithCompareAttribute_ErrorMessageUsesOverride() { // Arrange ModelMetadata metadata = ModelMetadataProviders.Current.GetMetadataForProperty(() => null, typeof(PropertyNameModel), "MyProperty"); ControllerContext context = new ControllerContext(); AnnotationsCompareAttribute attribute = new AnnotationsCompareAttribute("OtherProperty") { ErrorMessage = "Hello '{0}', goodbye '{1}'.", }; ModelValidator adapter = new CompareAttributeAdapter(metadata, context, attribute); // Act ModelClientValidationRule[] rules = adapter.GetClientValidationRules() .OrderBy(r => r.ValidationType) .ToArray(); // Assert ModelClientValidationRule rule = Assert.Single(rules); Assert.Equal("Hello 'MyProperty', goodbye 'OtherProperty'.", rule.ErrorMessage); }