ant-design-blazor/components/form/Internal/Validate/ValidationAttribute/NumberMaxAttribute.cs
笨木头 ec45abc4d7 feat(module: form): support set validation rules on FormItem (#1516)
* feat(module: form): support set validation rules on FormItem(not complete yet)

* refactor(module: form): optimized code

* feat(module: form): support Whitespace rule

* feat(module: form): support [message、pattern、transform、validator、whitespace] rules

* feat(module: form): support type rule

* feat(module: form): support [defaultField、oneOf、fields、type] rules

* feat(module: form): support custome validate messages(without test)

* test(module: form): complete RuleValidate_ValidateMessages test

* feat(module: form): complete DynamicRule demo, refactor code, fix bugs

* feat(module: form): support ValidateMessages param

* doc(module: form): add "Rules" and "ValidateMessages" docs

* fix: Rule.Max's errorMessage is wrong

* refactor: rename Rule to FormValidationRule

* test: rename Rule to FormValidationRule

* chore: refactor code and rename classes

* Update ValidateMode.razor

* Update FormValidateErrorMessages.cs

* Update FormValidateHelper.cs

Co-authored-by: James Yeung <shunjiey@hotmail.com>
2021-05-28 09:08:42 +00:00

49 lines
1.7 KiB
C#

using System;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
namespace AntDesign.Internal.Form.Validate
{
internal class NumberMaxAttribute : ValidationAttribute
{
internal decimal Max { get; }
internal NumberMaxAttribute(decimal max)
{
Max = max;
}
public override string FormatErrorMessage(string name)
{
return string.Format(CultureInfo.CurrentCulture, ErrorMessageString, name, Max);
}
public override bool IsValid(object value)
{
var type = value.GetType();
var typeMinValue = GetMinValueString(type);
var attribute = new RangeAttribute(type, typeMinValue, Max.ToString());
return attribute.IsValid(value);
}
private static string GetMinValueString(Type type)
{
if (type == typeof(byte)) return byte.MinValue.ToString();
if (type == typeof(short)) return short.MinValue.ToString();
if (type == typeof(int)) return int.MinValue.ToString();
if (type == typeof(long)) return long.MinValue.ToString();
if (type == typeof(float)) return float.MinValue.ToString();
if (type == typeof(double)) return double.MinValue.ToString();
if (type == typeof(sbyte)) return sbyte.MinValue.ToString();
if (type == typeof(ushort)) return ushort.MinValue.ToString();
if (type == typeof(uint)) return uint.MinValue.ToString();
if (type == typeof(ulong)) return ulong.MinValue.ToString();
if (type == typeof(decimal)) return decimal.MinValue.ToString();
return null;
}
}
}