Re-fabrication

This commit is contained in:
2018-03-26 19:27:29 +02:00
parent e00bcbe275
commit 8fbe56c67e
499 changed files with 7510 additions and 12466 deletions

View File

@ -0,0 +1,17 @@
using System.Resources;
using Yavsc.Resources;
namespace Yavsc.Attributes.Validation
{
public class YaRegularExpression : System.ComponentModel.DataAnnotations.RegularExpressionAttribute {
public YaRegularExpression(string pattern): base (pattern)
{
this.ErrorMessage = pattern;
}
public override string FormatErrorMessage(string name)
{
return ResourcesHelpers.DefaultResourceManager.GetString(this.ErrorMessageResourceName);
}
}
}

View File

@ -0,0 +1,37 @@
using System;
namespace Yavsc.Attributes.Validation
{
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
public class YaRequiredAttribute : YaValidationAttribute
{
/// <summary>
/// Gets or sets a flag indicating whether the attribute should allow empty strings.
/// </summary>
public bool AllowEmptyStrings { get; set; }
public YaRequiredAttribute (string msg) : base()
{
ErrorMessage = msg;
}
public YaRequiredAttribute ()
{
this.ErrorMessage = ResourcesHelpers.DefaultResourceManager.GetString("RequiredField");
}
public override bool IsValid(object value) {
if (value == null) {
return false;
}
// only check string length if empty strings are not allowed
var stringValue = value as string;
if (stringValue != null && !AllowEmptyStrings) {
return stringValue.Trim().Length != 0;
}
return true;
}
}
}

View File

@ -0,0 +1,58 @@
namespace Yavsc.Attributes.Validation
{
public class YaStringLength: YaValidationAttribute
{
public long MinLen { get; set; } = -1;
private long maxLen;
public YaStringLength(long maxLen) : base(
()=>string.Format(
ResourcesHelpers.DefaultResourceManager.GetString("BadStringLength"),
maxLen))
{
this.maxLen = maxLen;
}
private long excedent;
private long manquant;
public override bool IsValid(object value) {
if (value == null) {
return false;
}
string stringValue = value as string;
if (stringValue==null) return false;
if (MinLen>=0)
{
if (stringValue.Length<MinLen)
{
manquant = MinLen-stringValue.Length;
}
return false;
}
if (maxLen>=0)
{
if (stringValue.Length>maxLen) {
excedent = stringValue.Length-maxLen;
return false;
}
}
return true;
}
public override string FormatErrorMessage(string name)
{
if (MinLen<0) {
// DetailledMaxStringLength
return string.Format(
ResourcesHelpers.DefaultResourceManager.GetString("DetailledMaxStringLength"),
maxLen,
excedent);
} else
return string.Format(
ResourcesHelpers.DefaultResourceManager.GetString("DetailledMinMaxStringLength"),
MinLen,
maxLen,
manquant,
excedent);
}
}
}

View File

@ -0,0 +1,22 @@
using System;
namespace Yavsc.Attributes.Validation
{
public class YaValidationAttribute : System.ComponentModel.DataAnnotations.ValidationAttribute
{
public YaValidationAttribute() : base(()=> ResourcesHelpers.DefaultResourceManager.GetString("validationError"))
{
}
public YaValidationAttribute(Func<string> acr): base(acr)
{
}
public override string FormatErrorMessage(string name)
{
return ResourcesHelpers.DefaultResourceManager.GetString(name);
}
}
}