コード例 #1
0
            public CompareAttributeWrapper(DataAnnotationsCompareAttribute attribute, ModelMetadata metadata)
                : base(attribute.OtherProperty)
            {
                _otherPropertyDisplayName = attribute.OtherProperty;
                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;
                }
            }
コード例 #2
0
 /// <summary>
 /// Creates the validator.
 /// </summary>
 /// <param name="modelMetadata">The model metadata.</param>
 /// <param name="context">The context.</param>
 /// <returns></returns>
 protected override ModelValidator CreateValidatorCore(ExtendedModelMetadata modelMetadata, ControllerContext context)
 {
     var attribute = new CompareAttribute(OtherProperty);
     PopulateErrorMessage(attribute);
     return new DataAnnotationsModelValidator<CompareAttribute>(modelMetadata, context, attribute);
 }