2012-04-13 98 views
1

简单的自定义的验证,MVC3简单的自定义验证

我的模式和自定义的验证:

public class Registration 
{ 
    [Required(ErrorMessage = "Date of Birth is required")]   
    [AgeV(18,ErrorMessage="You are not old enough to register")] 
    public DateTime DateOfBirth { set; get; } 
} 

public class AgeVAttribute : ValidationAttribute 
{ 
    private int _maxAge; 

    public AgeVAttribute(int maxAge) 
    { 
     _maxAge = maxAge; 
    } 

    public override bool IsValid(object value) 
    { 
     return false;  <--- **this never gets executed.... what am I missing?** 
    } 
} 

(请参见上面的在线评论)

观点:

@using (Html.BeginForm()) { 
@Html.ValidationSummary("Errors") 
<fieldset> 
    <legend>Registration</legend> 
    <div class="editor-label"> 
     @Html.LabelFor(model => model.DateOfBirth) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.DateOfBirth)  
    </div> 

    <p> 
     <input type="submit" value="Create" /> 
    </p> 
</fieldset> 
} 
+1

接收模型的控制器是什么样的? – 2012-04-13 16:09:19

+1

我已经在一个空白的MVC项目中尝试了您的代码,并且发生了IsValid调用,因为它应该发生。 – Iridio 2012-04-13 16:10:30

+0

您是否会在使用“注册”类型的模型时提供“操作”方法?我测试了你的代码,它在服务器端工作。由于您的'AgeVAttribute'没有实现IClientValidatable,所以客户端验证关闭。 – Kibria 2012-04-13 16:33:04

回答

2

灿没有回报。

型号:

public class Registration 
{ 
    [Required(ErrorMessage = "Date of Birth is required")] 
    [AgeV(18, ErrorMessage = "You are not old enough to register")] 
    public DateTime DateOfBirth { set; get; } 
} 

public class AgeVAttribute : ValidationAttribute 
{ 
    private int _maxAge; 

    public AgeVAttribute(int maxAge) 
    { 
     _maxAge = maxAge; 
    } 

    public override bool IsValid(object value) 
    { 
     return false; 
    } 
} 

控制器:

public class HomeController : Controller 
{ 
    public ActionResult Index() 
    { 
     return View(new Registration 
     { 
      DateOfBirth = DateTime.Now.AddYears(-10) 
     }); 
    } 

    [HttpPost] 
    public ActionResult Index(Registration model) 
    { 
     return View(model); 
    } 
} 

查看:

@model Registration 

@using (Html.BeginForm()) 
{ 
    @Html.ValidationSummary("Errors") 
    <fieldset> 
     <legend>Registration</legend> 
     <div class="editor-label"> 
      @Html.LabelFor(model => model.DateOfBirth) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.DateOfBirth)  
     </div> 
     <p> 
      <input type="submit" value="Create" /> 
     </p> 
    </fieldset> 
} 

提交表单时,IsValid的方法总是击中。另外请注意,我没有启用客户端验证,因为我没有包含jquery.validate.jsjquery.validate.unobtrusive.js脚本。如果你已经包含了它们,那么错误的可能性是客户端验证会阻止你的表单被提交给服务器,在这种情况下,IsValid方法不会被调用是正常的。

+0

thx Darin,一旦我放置了[HttpPost] public ActionResult Index(注册模型) { return View(model); }运行IsValid方法。如果没有困难,请告诉我如何使用客户端验证进行检查,而不是使用serverside感谢您的帮助 – Ben 2012-04-13 16:21:01

+0

您可以让自定义验证属性实现'IClientValidatable'接口,然后注册一个自定义适配器这将代表一个JavaScript函数,您需要编写和复制服务器上的相同验证逻辑。这里是一个例子:http://stackoverflow.com/a/4747466/29407 – 2012-04-13 16:27:21

+0

嗨达林,我已经检查了你发布的例子,一切都很清楚,直到我到最后一部分(定义自定义适配器),请你给我看看你将如何实现年龄限制(自定义适配器),我看看我see.test()方法的例子,不知道在做什么......如果你能解释我,那真是太棒了再次感谢。 – Ben 2012-04-13 16:54:19