mirror of https://github.com/abpframework/abp.git
7 changed files with 184 additions and 3 deletions
@ -0,0 +1,29 @@ |
|||
using System.ComponentModel.DataAnnotations; |
|||
using Microsoft.AspNetCore.Mvc.DataAnnotations; |
|||
using Microsoft.Extensions.Localization; |
|||
using Volo.Abp.Validation; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Mvc.DataAnnotations |
|||
{ |
|||
public class AbpValidationAttributeAdapterProvider : IValidationAttributeAdapterProvider |
|||
{ |
|||
private readonly ValidationAttributeAdapterProvider _defaultAdapter; |
|||
|
|||
public AbpValidationAttributeAdapterProvider(ValidationAttributeAdapterProvider defaultAdapter) |
|||
{ |
|||
_defaultAdapter = defaultAdapter; |
|||
} |
|||
|
|||
public virtual IAttributeAdapter GetAttributeAdapter(ValidationAttribute attribute, IStringLocalizer stringLocalizer) |
|||
{ |
|||
var type = attribute.GetType(); |
|||
|
|||
if (type == typeof(DynamicStringLengthAttribute)) |
|||
{ |
|||
return new DynamicStringLengthAttributeAdapter((DynamicStringLengthAttribute) attribute, stringLocalizer); |
|||
} |
|||
|
|||
return _defaultAdapter.GetAttributeAdapter(attribute, stringLocalizer); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,54 @@ |
|||
using System; |
|||
using System.Globalization; |
|||
using Microsoft.AspNetCore.Mvc.DataAnnotations; |
|||
using Microsoft.AspNetCore.Mvc.ModelBinding.Validation; |
|||
using Microsoft.Extensions.Localization; |
|||
using Volo.Abp.Validation; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Mvc.DataAnnotations |
|||
{ |
|||
public class DynamicStringLengthAttributeAdapter : AttributeAdapterBase<DynamicStringLengthAttribute> |
|||
{ |
|||
private readonly string _max; |
|||
private readonly string _min; |
|||
|
|||
public DynamicStringLengthAttributeAdapter( |
|||
DynamicStringLengthAttribute attribute, |
|||
IStringLocalizer stringLocalizer) |
|||
: base(attribute, stringLocalizer) |
|||
{ |
|||
_max = Attribute.MaximumLength.ToString(CultureInfo.InvariantCulture); |
|||
_min = Attribute.MinimumLength.ToString(CultureInfo.InvariantCulture); |
|||
} |
|||
|
|||
public override void AddValidation(ClientModelValidationContext context) |
|||
{ |
|||
Check.NotNull(context, nameof(context)); |
|||
|
|||
MergeAttribute(context.Attributes, "data-val", "true"); |
|||
MergeAttribute(context.Attributes, "data-val-length", GetErrorMessage(context)); |
|||
|
|||
if (Attribute.MaximumLength != int.MaxValue) |
|||
{ |
|||
MergeAttribute(context.Attributes, "data-val-length-max", _max); |
|||
} |
|||
|
|||
if (Attribute.MinimumLength != 0) |
|||
{ |
|||
MergeAttribute(context.Attributes, "data-val-length-min", _min); |
|||
} |
|||
} |
|||
|
|||
public override string GetErrorMessage(ModelValidationContextBase validationContext) |
|||
{ |
|||
Check.NotNull(validationContext, nameof(validationContext)); |
|||
|
|||
return GetErrorMessage( |
|||
validationContext.ModelMetadata, |
|||
validationContext.ModelMetadata.GetDisplayName(), |
|||
Attribute.MaximumLength, |
|||
Attribute.MinimumLength |
|||
); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,58 @@ |
|||
using System; |
|||
using System.ComponentModel.DataAnnotations; |
|||
using System.Diagnostics; |
|||
using System.Reflection; |
|||
using JetBrains.Annotations; |
|||
|
|||
namespace Volo.Abp.Validation |
|||
{ |
|||
/// <summary>
|
|||
/// Used to determine <see cref="StringLengthAttribute.MaximumLength"/> and <see cref="StringLengthAttribute.MinimumLength"/>
|
|||
/// properties on the runtime.
|
|||
/// </summary>
|
|||
public class DynamicStringLengthAttribute : StringLengthAttribute |
|||
{ |
|||
private static readonly FieldInfo MaximumLengthField; |
|||
|
|||
static DynamicStringLengthAttribute() |
|||
{ |
|||
MaximumLengthField = typeof(StringLengthAttribute).GetField( |
|||
"<MaximumLength>k__BackingField", |
|||
BindingFlags.Instance | BindingFlags.NonPublic |
|||
); |
|||
Debug.Assert(MaximumLengthField != null, nameof(MaximumLengthField) + " != null"); |
|||
} |
|||
|
|||
/// <param name="sourceType">A type to get the values of the properties</param>
|
|||
/// <param name="maximumLengthPropertyName">The name of the public static property for the <see cref="StringLengthAttribute.MaximumLength"/></param>
|
|||
/// <param name="minimumLengthPropertyName">The name of the public static property for the <see cref="StringLengthAttribute.MinimumLength"/></param>
|
|||
public DynamicStringLengthAttribute( |
|||
[NotNull] Type sourceType, |
|||
[CanBeNull] string maximumLengthPropertyName, |
|||
[CanBeNull] string minimumLengthPropertyName = null) |
|||
: base(0) |
|||
{ |
|||
Check.NotNull(sourceType, nameof(sourceType)); |
|||
|
|||
if (maximumLengthPropertyName != null) |
|||
{ |
|||
var maximumLengthProperty = sourceType.GetProperty( |
|||
maximumLengthPropertyName, |
|||
BindingFlags.Static | BindingFlags.Public |
|||
); |
|||
Debug.Assert(maximumLengthProperty != null, nameof(maximumLengthProperty) + " != null"); |
|||
MaximumLengthField.SetValue(this, (int) maximumLengthProperty.GetValue(null)); |
|||
} |
|||
|
|||
if (minimumLengthPropertyName != null) |
|||
{ |
|||
var minimumLengthProperty = sourceType.GetProperty( |
|||
minimumLengthPropertyName, |
|||
BindingFlags.Static | BindingFlags.Public |
|||
); |
|||
Debug.Assert(minimumLengthProperty != null, nameof(minimumLengthProperty) + " != null"); |
|||
MinimumLength = (int) minimumLengthProperty.GetValue(null); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue