自定义ValidationAttribute不显示错误

时间:2015-08-31 01:26:51

标签: javascript c# asp.net-mvc

我编写了一个自定义ValidationAttribute以检查Db中是否已存在电子邮件。但是,不显示错误消息,并在提交表单后进行验证。 HTML包含对javascript文件的引用以显示此消息,但仍然没有结果。然而,其他属性工作正常,并显示它们各自的消息。

public class NoDuplicateEmail : ValidationAttribute
{



     protected override ValidationResult IsValid(object value, ValidationContext validationContext)
     {
          var context = new MhotivoContext();

         var email = validationContext.ObjectInstance.GetType().GetProperties(
             ).FirstOrDefault(prop => IsDefined(prop, typeof(NoDuplicateEmail)));

         var emailValue = (string)email.GetValue(validationContext.ObjectInstance);

         if(context.Users.FirstOrDefault(x => x.Email == emailValue)!=null)
             return new ValidationResult("Email is in use!");

         return ValidationResult.Success;

     } 

}

    [Required(ErrorMessage = "Email is required")]
    [DataType(DataType.EmailAddress)]
    [Display(Name = "Email")]
    [NoDuplicateEmail]

1 个答案:

答案 0 :(得分:0)

object value方法的IsValid参数包含您的电子邮件属性的值,因此您不需要var emailvar emailValue行代码。你的代码应该是

public class NoDuplicateEmail : ValidationAttribute
{
  private const string _DefaultErrorMessage = "Email {0} is in use!"
  public NoDuplicateEmail() : : base(_DefaultErrorMessage)
  {
  }

  protected override ValidationResult IsValid(object value, ValidationContext validationContext)
  {
    var context = new MhotivoContext();
    if (value != null)
    {
      var email = (string)value;
      if(context.Users.FirstOrDefault(x => x.Email == emailValue) != null)
      {
        return new ValidationResult(FormatErrorMessage(email));
      }
    }
    return ValidationResult.Success;
  }
}

但是,您不应该在验证属性中访问数据库资源(这不是它的责任,您的代码不可能进行单元测试)。您应该使用[Remote]属性进行客户端验证并调用服务进行服务器端验证。

而是创建一个服务方法,该方法返回一个指示其有效的值(或在控制器中添加私有方法

private bool isEmailInUse(string email)
{
  return context.Users.Any(x => x.Email == email)
}

ActionResult用于客户端验证

public JsonResult IsUniqueEmail(string email) // the parameter name must match the property name
{
  bool isInUse = isEmailInUse(email);
  return Json(!isInUse, JsonRequestBehavior);
}

然后将RemoteAttribute应用到您的媒体资源

[Remote("IsUniqueEmail", yourControllerName, ErrorMessage = "The email is already in use")]
public string Email { get; set; }

最后,在POST方法中(如果是恶意用户,或者javascript已被禁用)

public ActionResult YourMethod (YourModel model)
{
  if (isEmailInUse(model.Email)
  {
    // add a ModelState error and return the view
  } 
}

或者只是在保存时捕获异常(假设数据库字段具有唯一约束)