one model property validation info/rules are available in the other properties of same model object. i have to validate the property values using other properties values.
Model class:-
[FormatValidtion("Value", "Format", "MinLength", "MaxLength", ErrorMessage = "Please enter correct format")]
public class Sample
{
#region ModelProperties
public string Id { get; set; }
[Required(ErrorMessage = "Please Enter Value")]
public string Value { get; set; }
public string Format { get; set; }
public string MinLength { get; set; }
public string MaxLength { get; set; }
#endregion
}
Model object looks like above.Here i have to validate Value property using the
Format ( Fomart Validation like email,phone & Date)
MinLength,MaxLength (Range validation) properties.
I know we can do this using custom validations like
- Create Custom class keeping ValidationAttribute as base
- Pass all these properties( Value ,Format,MinLength & MaxLength)
- Write switch case with Format property.
- Validate Format using Regex and do manual range validation or with dynamic Regex validation.
EDIT :- Added Custom Validation Class Code Here
Custom Validation Class:
[AttributeUsage(AttributeTargets.Class)]
public class FormatValidtion : ValidationAttribute
{
public String Value { get; set; }
public String Format { get; set; }
public String MinLength { get; set; }
public String MaxLength { get; set; }
public FormatValidtion(String value, String format, String minLength, String maxLength)
{
Value = value;
Format = format;
MinLength = minLength;
MaxLength = maxLength;
}
public override Boolean IsValid(Object value)
{
Type objectType = value.GetType();
PropertyInfo[] neededProperties =
objectType.GetProperties()
.Where(p => p.Name == Value || p.Name == Format || p.Name == MinLength || p.Name == MaxLength)
.ToArray();
if (neededProperties.Count() != 4)
{
throw new ApplicationException("PropertiesMatchAttribute error on " + objectType.Name);
}
Boolean isMatched = true;
switch (Convert.ToString(neededProperties[1].GetValue(value, null)))
{
case "Alpha":
Regex regExAlpha = new Regex(@"/^[A-Za-z]+$/");
if (!regExAlpha.IsMatch(Convert.ToString(neededProperties[0].GetValue(value, null))))
{
isMatched = false;
}
break;
case "Number":
Regex regExNumber = new Regex(@"/^[0-9]+$/");
if (!regExNumber.IsMatch(Convert.ToString(neededProperties[0].GetValue(value, null))))
{
isMatched = false;
}
break;
case "AlphaNum":
Regex regExAlphaNum = new Regex(@"/^[a-zA-Z][a-zA-Z0-9]+$/");
if (!regExAlphaNum.IsMatch(Convert.ToString(neededProperties[0].GetValue(value, null))))
{
isMatched = false;
}
break;
default:
isMatched = false;
break;
}
return isMatched;
}
}
This is working correctly at class level and showing validation messages through validation summary. But I want to modify the above code to do the following things.
- Validation should happen at property level or some how need to show error message at property level( using validationMessage helper class).
- Need to provide the specific error messages for each validation other than common error message.
- Range validation should happen.
Can anybody provide some thoughts around these?