1
나는 ASP.NET MVC3 프로젝트에서 두 가지 조건을 충족해야하는 사용자 지정 ValidationAttribute를 보유하고 있습니다. 그것은 잘 작동하지만 사용자 정의 오류 메시지를 반환하여 어떤 유효성 검사 규칙이 깨진 지금 사용자 싶습니다. 나는이 초기화 된 후 나는 _defaultError 상수의 값을 변경할 수 없습니다 알고, 그래서 (기본 클래스에서 오류 메시지가 상속) 사용하고있는 방법으로 조건부 ValidationAttribute 오류 메시지는 어떻게 만듭니 까?
....어떻게 다른 반환 할 어떤 조건이 충족되지 않았는지에 따라 오류 메시지가 표시됩니까?
public class DateValidationAttribute :ValidationAttribute
{
public DateValidationAttribute()
: base(_defaultError)
{
}
private const string _defaultError = "{0} [here is my generic error message]";
public override bool IsValid(object value)
{
DateTime val = (DateTime)value;
if (val > Convert.ToDateTime("13:30:00 PM"))
{
//This is where I'd like to set the error message
//_defaultError = "{0} can not be after 1:30pm";
return false;
}
else if (DateTime.Now.AddHours(1).Ticks > val.Ticks)
{
//This is where I'd like to set the error message
//_defaultError = "{0} must be at least 1 hour from now";
return false;
}
else
{
return true;
}
}
}
+1 감사합니다. 그것은 트릭을했다. 두 개의 별도의 사용자 정의 유효성 검사 속성으로 나눠서 완벽하게 작동했습니다. SRP에 관해서도 훌륭한 점. – Dhaust