Imported Upstream version 4.8.0.309

Former-commit-id: 5f9c6ae75f295e057a7d2971f3a6df4656fa8850
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2016-11-10 13:04:39 +00:00
parent ee1447783b
commit 94b2861243
4912 changed files with 390737 additions and 49310 deletions

View File

@@ -4,6 +4,7 @@
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web.Globalization;
public class DataAnnotationsModelMetadataProvider : AssociatedMetadataProvider {
@@ -11,7 +12,7 @@
List<Attribute> attributeList = new List<Attribute>(attributes);
DisplayColumnAttribute displayColumnAttribute = attributeList.OfType<DisplayColumnAttribute>().FirstOrDefault();
DataAnnotationsModelMetadata result = new DataAnnotationsModelMetadata(this, containerType, modelAccessor, modelType, propertyName, displayColumnAttribute);
#if UNDEF
// Do [HiddenInput] before [UIHint], so you can override the template hint
HiddenInputAttribute hiddenInputAttribute = attributeList.OfType<HiddenInputAttribute>().FirstOrDefault();
@@ -71,12 +72,13 @@
DisplayAttribute display = attributes.OfType<DisplayAttribute>().FirstOrDefault();
string name = null;
if (display != null) {
result.Description = display.GetDescription();
result.ShortDisplayName = display.GetShortName();
result.Watermark = display.GetPrompt();
result.Order = display.GetOrder() ?? ModelMetadata.DefaultOrder;
var displayAdapter = new DisplayAttributeAdapter(display);
result.Description = displayAdapter.GetDescription();
result.ShortDisplayName = displayAdapter.GetShortName();
result.Watermark = displayAdapter.GetPrompt();
result.Order = displayAdapter.GetOrder() ?? ModelMetadata.DefaultOrder;
name = display.GetName();
name = displayAdapter.GetName();
}
if (name != null) {

View File

@@ -2,8 +2,10 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Threading;
using System.Web.Globalization;
public class DataAnnotationsModelValidator : ModelValidator {
public class DataAnnotationsModelValidator : ModelValidator {
public DataAnnotationsModelValidator(ModelMetadata metadata, ModelBindingExecutionContext context, ValidationAttribute attribute)
: base(metadata, context) {
@@ -18,7 +20,24 @@
protected internal string ErrorMessage {
get {
return Attribute.FormatErrorMessage(Metadata.GetDisplayName());
if (UseStringLocalizerProvider) {
var errorMsg = GetLocalizedString(Attribute.ErrorMessage);
return errorMsg ?? Attribute.FormatErrorMessage(Metadata.GetDisplayName());
}
else {
return Attribute.FormatErrorMessage(Metadata.GetDisplayName());
}
}
}
protected string GetLocalizedString(string name, params object[] arguments) {
if (StringLocalizerProviders.DataAnnotationStringLocalizerProvider != null) {
return StringLocalizerProviders.DataAnnotationStringLocalizerProvider
.GetLocalizedString(Thread.CurrentThread.CurrentUICulture, name, arguments);
}
else {
return null;
}
}
@@ -55,9 +74,37 @@
ValidationResult result = Attribute.GetValidationResult(Metadata.Model, context);
if (result != ValidationResult.Success) {
yield return new ModelValidationResult {
Message = result.ErrorMessage
Message = GetValidationErrorMessage(result)
};
}
}
protected virtual string GetLocalizedErrorMessage(string errorMessage) {
return GetLocalizedString(errorMessage, Metadata.GetDisplayName());
}
private string GetValidationErrorMessage(ValidationResult result) {
string errorMsg;
if (UseStringLocalizerProvider) {
errorMsg = GetLocalizedErrorMessage(Attribute.ErrorMessage);
errorMsg = errorMsg ?? result.ErrorMessage;
}
else {
errorMsg = result.ErrorMessage;
}
return errorMsg;
}
private bool UseStringLocalizerProvider {
get {
// if developer already uses existing localization feature,
// then we don't opt in the new localization feature.
return (!string.IsNullOrEmpty(Attribute.ErrorMessage) &&
string.IsNullOrEmpty(Attribute.ErrorMessageResourceName) &&
Attribute.ErrorMessageResourceType == null);
}
}
}
}

View File

@@ -1,5 +1,5 @@
namespace System.Web.ModelBinding {
using System;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis;
@@ -7,6 +7,7 @@ using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Web.Globalization;
// A factory for validators based on ValidationAttribute
public delegate ModelValidator DataAnnotationsModelValidationFactory(ModelMetadata metadata, ModelBindingExecutionContext context, ValidationAttribute attribute);
@@ -26,7 +27,7 @@ using System.Threading;
public class DataAnnotationsModelValidatorProvider : AssociatedValidatorProvider {
private static bool _addImplicitRequiredAttributeForValueTypes = true;
private static ReaderWriterLockSlim _adaptersLock = new ReaderWriterLockSlim();
// Factories for validation attributes
internal static DataAnnotationsModelValidationFactory DefaultAttributeFactory =
@@ -49,6 +50,14 @@ using System.Threading;
typeof(StringLengthAttribute),
(metadata, context, attribute) => new StringLengthAttributeAdapter(metadata, context, (StringLengthAttribute)attribute)
},
{
typeof(MinLengthAttribute),
(metadata, context, attribute) => new MinLengthAttributeAdapter(metadata, context, (MinLengthAttribute)attribute)
},
{
typeof(MaxLengthAttribute),
(metadata, context, attribute) => new MaxLengthAttributeAdapter(metadata, context, (MaxLengthAttribute)attribute)
},
};
// Factories for IValidatableObject models
@@ -66,7 +75,7 @@ using System.Threading;
_addImplicitRequiredAttributeForValueTypes = value;
}
}
protected override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, ModelBindingExecutionContext context, IEnumerable<Attribute> attributes) {
_adaptersLock.EnterReadLock();

View File

@@ -6,6 +6,11 @@
: base(metadata, context, attribute) {
}
protected override string GetLocalizedErrorMessage(string errorMessage) {
return GetLocalizedString(errorMessage, Metadata.GetDisplayName(), Attribute.Minimum, Attribute.Maximum);
}
#if UNDEF
public override IEnumerable<ModelClientValidationRule> GetClientValidationRules() {
string errorMessage = ErrorMessage; // Per Dev10 Bug #923283, need to make sure ErrorMessage is called before Minimum/Maximum

View File

@@ -6,6 +6,10 @@
: base(metadata, context, attribute) {
}
protected override string GetLocalizedErrorMessage(string errorMessage) {
return GetLocalizedString(errorMessage, Metadata.GetDisplayName(), Attribute.Pattern);
}
#if UNDEF
public override IEnumerable<ModelClientValidationRule> GetClientValidationRules() {
return new[] { new ModelClientValidationRegexRule(ErrorMessage, Attribute.Pattern) };

View File

@@ -6,6 +6,10 @@
: base(metadata, context, attribute) {
}
protected override string GetLocalizedErrorMessage(string errorMessage) {
return GetLocalizedString(errorMessage, Metadata.GetDisplayName(), Attribute.MinimumLength, Attribute.MaximumLength);
}
#if UNDEF
public override IEnumerable<ModelClientValidationRule> GetClientValidationRules() {
return new[] { new ModelClientValidationStringLengthRule(ErrorMessage, Attribute.MinimumLength, Attribute.MaximumLength) };