I have something in my viewmodel like
[RegularExpression(...)]
public string PenguinGenome
{
get;set;
}
public int PenguinSpecies
{
get; set;
}
Is there a way to make … dynamic based on PenguinSpecies?
ie:
[RegularExpression(GetExpression(PenguinSpeciesId))]
public string PenguinGenome
{
get;set;
}
public int PenguinSpeciesId
{
get; set;
}
public string GetExpression(int speciesId)
{
if (.....)
return "[some regex]";
}
This won’t work because PenguinSpeciesId doesn’t exist in the context. What’s the best workaround?
This is an article for ASP.NET MVC2, but the process is pretty close to the same for ASP.NET MVC3.
Basically you will need to create your own custom validator that can look at the object, and then determine the proper way to validate.