mirror of https://github.com/abpframework/abp.git
5 changed files with 161 additions and 13 deletions
@ -0,0 +1,46 @@ |
|||||
|
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 DynamicRangeAttributeAdapter : AttributeAdapterBase<DynamicRangeAttribute> |
||||
|
{ |
||||
|
private readonly string _max; |
||||
|
private readonly string _min; |
||||
|
|
||||
|
public DynamicRangeAttributeAdapter( |
||||
|
DynamicRangeAttribute attribute, |
||||
|
IStringLocalizer stringLocalizer) |
||||
|
: base(attribute, stringLocalizer) |
||||
|
{ |
||||
|
_min = Attribute.Minimum.ToString()?.ToString(CultureInfo.InvariantCulture); |
||||
|
_max = Attribute.Maximum.ToString()?.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)); |
||||
|
MergeAttribute(context.Attributes, "data-val-length-min", _min); |
||||
|
MergeAttribute(context.Attributes, "data-val-length-max", _max); |
||||
|
} |
||||
|
|
||||
|
public override string GetErrorMessage(ModelValidationContextBase validationContext) |
||||
|
{ |
||||
|
Check.NotNull(validationContext, nameof(validationContext)); |
||||
|
|
||||
|
return GetErrorMessage( |
||||
|
validationContext.ModelMetadata, |
||||
|
validationContext.ModelMetadata.GetDisplayName(), |
||||
|
Attribute.Minimum, |
||||
|
Attribute.Maximum |
||||
|
); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,64 @@ |
|||||
|
using System; |
||||
|
using System.ComponentModel.DataAnnotations; |
||||
|
using System.Diagnostics; |
||||
|
using System.Reflection; |
||||
|
using JetBrains.Annotations; |
||||
|
|
||||
|
namespace Volo.Abp.Validation |
||||
|
{ |
||||
|
public class DynamicRangeAttribute : RangeAttribute |
||||
|
{ |
||||
|
private static readonly FieldInfo MaximumField; |
||||
|
private static readonly FieldInfo MinimumField; |
||||
|
|
||||
|
static DynamicRangeAttribute() |
||||
|
{ |
||||
|
MaximumField = typeof(RangeAttribute).GetField( |
||||
|
"<Maximum>k__BackingField", |
||||
|
BindingFlags.Instance | BindingFlags.NonPublic |
||||
|
); |
||||
|
Debug.Assert(MaximumField != null, nameof(MaximumField) + " != null"); |
||||
|
|
||||
|
MinimumField = typeof(RangeAttribute).GetField( |
||||
|
"<Minimum>k__BackingField", |
||||
|
BindingFlags.Instance | BindingFlags.NonPublic |
||||
|
); |
||||
|
Debug.Assert(MinimumField != null, nameof(MinimumField) + " != null"); |
||||
|
} |
||||
|
|
||||
|
/// <param name="sourceType">A type to get the values of the properties</param>
|
||||
|
/// <param name="minimumPropertyName">The name of the public static property for the <see cref="RangeAttribute.Minimum"/></param>
|
||||
|
/// <param name="maximumPropertyName">The name of the public static property for the <see cref="RangeAttribute.Maximum"/></param>
|
||||
|
/// <param name="operandType">The type of the range parameters. Must implement IComparable. <see cref="RangeAttribute.OperandType"/></param>
|
||||
|
public DynamicRangeAttribute( |
||||
|
[NotNull] Type sourceType, |
||||
|
[NotNull] Type operandType, |
||||
|
[CanBeNull] string minimumPropertyName, |
||||
|
[CanBeNull] string maximumPropertyName |
||||
|
) |
||||
|
: base(operandType, string.Empty, string.Empty) |
||||
|
{ |
||||
|
Check.NotNull(sourceType, nameof(sourceType)); |
||||
|
|
||||
|
if (minimumPropertyName != null) |
||||
|
{ |
||||
|
var minimumProperty = sourceType.GetProperty( |
||||
|
minimumPropertyName, |
||||
|
BindingFlags.Static | BindingFlags.Public |
||||
|
); |
||||
|
Debug.Assert(minimumProperty != null, nameof(minimumProperty) + " != null"); |
||||
|
MinimumField.SetValue(this, minimumProperty.GetValue(null)); |
||||
|
} |
||||
|
|
||||
|
if (maximumPropertyName != null) |
||||
|
{ |
||||
|
var maximumProperty = sourceType.GetProperty( |
||||
|
maximumPropertyName, |
||||
|
BindingFlags.Static | BindingFlags.Public |
||||
|
); |
||||
|
Debug.Assert(maximumProperty != null, nameof(maximumProperty) + " != null"); |
||||
|
MaximumField.SetValue(this, maximumProperty.GetValue(null)); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue