So what I like to do is:
[NotLike(Value = "Forbidden value")]
public string Title { get; set; }
Is it possible? I've read the docs from Microsoft and could not find anything like this.
You should be using ValidationAttribute
and inherit from it as follows:
public class NotLikeAttribute : ValidationAttribute
{
private string _NotLikeStr = "";
public NotLikeAttribute(string notLikeStr)
{
this._NotLikeStr = notLikeStr;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (value != null)
{
if (!((string)value).Contains(_NotLikeStr))
{
var memberName = validationContext.MemberName;
var errorMsg = "Your Message";
return new ValidationResult(errorMsg);
}
}
return null;
}
}
and decorate your property as follows:
[NotLike("Forbidden value")]
public string Title { get; set; }
of course instead of using line below
if (!((string)value).Contains(_NotLikeStr))
you can split string to multiple words
or use Regular expression
or anything that meets your requirements .