Skip to content

Instantly share code, notes, and snippets.

@carlhoerberg
Created August 27, 2010 08:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlhoerberg/553027 to your computer and use it in GitHub Desktop.
Save carlhoerberg/553027 to your computer and use it in GitHub Desktop.
public class VarCharLengthValidatorProvider : AssociatedValidatorProvider
{
protected override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context, IEnumerable<Attribute> attributes)
{
var columnAttribute = attributes.OfType<System.Data.Linq.Mapping.ColumnAttribute>().FirstOrDefault();
if (columnAttribute == null || !columnAttribute.DbType.ToLower().Contains("varchar"))
yield break;
var match = Regex.Match(columnAttribute.DbType, @"(\d+)", RegexOptions.IgnoreCase);
if (match.Success)
{
var maxLength = int.Parse(match.Value);
yield return new StringLengthAttributeAdapter(metadata, context, new StringLengthAttribute(maxLength));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment