-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4524 from abpframework/Cotur/dynamic-max-length
Introduced DynamicMaxLength
- Loading branch information
Showing
5 changed files
with
113 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
...spNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/DataAnnotations/DynamicMaxLengthAttributeAdapter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
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 DynamicMaxLengthAttributeAdapter : AttributeAdapterBase<DynamicMaxLengthAttribute> | ||
{ | ||
private readonly string _max; | ||
|
||
public DynamicMaxLengthAttributeAdapter( | ||
DynamicMaxLengthAttribute attribute, | ||
IStringLocalizer stringLocalizer) | ||
: base(attribute, stringLocalizer) | ||
{ | ||
_max = Attribute.Length.ToString(CultureInfo.InvariantCulture); | ||
} | ||
|
||
public override string GetErrorMessage(ModelValidationContextBase validationContext) | ||
{ | ||
Check.NotNull(validationContext, nameof(validationContext)); | ||
|
||
return GetErrorMessage( | ||
validationContext.ModelMetadata, | ||
validationContext.ModelMetadata.GetDisplayName(), | ||
Attribute.Length | ||
); | ||
} | ||
|
||
public override void AddValidation(ClientModelValidationContext context) | ||
{ | ||
Check.NotNull(context, nameof(context)); | ||
|
||
MergeAttribute(context.Attributes, "data-val", "true"); | ||
MergeAttribute(context.Attributes, "data-val-maxlength", GetErrorMessage(context)); | ||
MergeAttribute(context.Attributes, "data-val-maxlength-max", _max); | ||
|
||
if (Attribute.Length != int.MaxValue) | ||
{ | ||
MergeAttribute(context.Attributes, "data-val-maxlength-max", _max); | ||
} | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
framework/src/Volo.Abp.Core/Volo/Abp/Validation/DynamicMaxLengthAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Diagnostics; | ||
using System.Reflection; | ||
using JetBrains.Annotations; | ||
|
||
namespace Volo.Abp.Validation | ||
{ | ||
public class DynamicMaxLengthAttribute : MaxLengthAttribute | ||
{ | ||
private static readonly FieldInfo MaximumLengthField; | ||
|
||
static DynamicMaxLengthAttribute() | ||
{ | ||
MaximumLengthField = typeof(MaxLengthAttribute).GetField( | ||
"<Length>k__BackingField", | ||
BindingFlags.Instance | BindingFlags.NonPublic | ||
); | ||
Debug.Assert(MaximumLengthField != null, nameof(MaximumLengthField) + " != null"); | ||
} | ||
|
||
public DynamicMaxLengthAttribute( | ||
[NotNull] Type sourceType, | ||
[CanBeNull] string maximumLengthPropertyName) | ||
{ | ||
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)); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters