Files
BeWoPlaner/ValidationLibrary/ValidationAttribute.cs

80 lines
2.7 KiB
C#
Raw Permalink Normal View History

2016-06-27 01:45:38 +02:00
using System;
using System.Collections;
using BS.Shared.Extensions;
using Config;
namespace ValidationLibrary
{
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class ValidationAttribute : Attribute
{
public ValidationAttribute()
{
ValidationRule = ValidationRules.AlwaysValid;
}
public ValidationRules ValidationRule { get; set; }
public bool Validate(object pValue)
{
var lResult = true;
switch (ValidationRule)
{
case ValidationRules.NotZero:
double lNumber;
var lIsNumber = Double.TryParse(pValue.ToStringNullCheck(), out lNumber);
if (lIsNumber)
lResult = !Equals(lNumber, 0d);
break;
case ValidationRules.GreaterThanZero:
if (pValue == null)
{
lResult = false;
}
else
{
double lNumber2;
var lIsNumber2 = Double.TryParse(pValue.ToStringNullCheck(), out lNumber2);
if (lIsNumber2)
lResult = lNumber2 > 0d;
}
break;
case ValidationRules.GreaterThanOrEqualZero:
if (pValue == null)
{
lResult = false;
}
else
{
double lNumber3;
bool lIsNumber3 = Double.TryParse(pValue.ToStringNullCheck(), out lNumber3);
if (lIsNumber3)
lResult = lNumber3 >= 0d;
}
break;
case ValidationRules.NotNullOrStringEmpty:
lResult = pValue is string ? ((string) pValue).Trim() != string.Empty : pValue != null;
break;
case ValidationRules.EmptyNoticeInNewServiceRecordsAllowed:
if (AppSettings.mIsServiceRecordNoticeMandatory)
{
lResult = pValue is string ? ((string) pValue).Trim() != string.Empty : pValue != null;
}
break;
case ValidationRules.CollectionNotEmpty:
if (pValue is ICollection)
lResult = (pValue as ICollection).Count > 0;
break;
}
return lResult;
}
}
}