将变量传递给验证器

4
我正在尝试设置一个类似于示例中的远程验证。
然而,我的应用程序有一个变化,我的表单元素是动态生成的,因此这个标记:
[Remote("doesUserNameExist", "Account", HttpMethod = "POST", ErrorMessage = "User name already exists. Please enter a different user name.")]

这并不是铁板一块的,我需要变化ErrorMessage,最好也能改变操作方式。这是否可行?或者您建议采取更长的方式,也就是自己实现整个ajax验证。

欢迎提出任何建议。

2个回答

2
如果您需要动态错误消息,则可以从验证操作中返回此字符串:

如果您需要动态错误消息,则可以从验证操作中返回此字符串:

public ActionResult DoesUserNameExist(string username)
{
    if (Exists(uasername))
    {
        string errorMessage = "Some dynamic error message";
        return Json(errorMessage, JsonRequestBehavior.AllowGet);
    }

    return Json(true, JsonRequestBehavior.AllowGet);
}

如果您需要更多灵活性,比如调用动态动作,则最好自己编写自定义验证解决方案,而不是依赖内置的Remote属性。


对于我们的动态表单,我选择实现自定义验证,这似乎是最灵活的方式。 - RealityDysfunction

2
你可以继承 RemoteAttribute,并根据自己的逻辑从服务或工厂中获取所需值。以下是一个示例:
    [AttributeUsage(AttributeTargets.Property)]
    public class MyRemoteAttribute : RemoteAttribute
    {
        public MyRemoteAttribute(Type type, string propertyName)
            : base(MyRemoteAttributeDataProvider.GetAttributeData(type,propertyName).Action,               MyRemoteAttributeDataProvider.GetAttributeData(type,propertyName).Controller)
        {
            var data = MyRemoteAttributeDataProvider.GetAttributeData(type,propertyName);
            base.ErrorMessage = data.ErrorMessage;
            base.HttpMethod = data.HttpMethod;
        }
    }

    public static class MyRemoteAttributeDataProvider
    {
        public static RemoteAttributeData GetAttributeData(Type type
           , string propertyName)
        {
            //this is where you are going to implement your logic im just implementing                 as an example
            //you can pass in a different type to get your values. For example you can  pass in a service to get required values.

            //property specific logic here, again im going to implement to make this
            //specification by example
           var attrData = new RemoteAttributeData();            
           if(propertyName == "MyOtherProperty")
           {
                attrData.Action = "MyOtherPropertyRelatedAction";
                attrData.Controller = "MyOtherPropertyRelatedController";
                attrData.ErrorMessage = "MyOtherPropertyRelated Error Message";
                attrData.HttpMethod = "POST";
           }            
           else 
           {            
                attrData.Action = "UserNameExists";
                attrData.Controller = "AccountController";
                attrData.ErrorMessage = "Some Error Message";
                attrData.HttpMethod = "POST";
           }
           return attrData;
        }
    }

    public class RemoteAttributeData
    {
        public string Controller { get; set; }
        public string Action { get; set; }
        public string HttpMethod { get; set; }
        public string ErrorMessage { get; set; }
    }

以下是正确使用方法:

public class RegisterViewModel
{
    [Required]
    [Display(Name = "User name")]
    [MyRemote(typeof(RegisterViewModel),"UserName")]
    public string UserName { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [System.ComponentModel.DataAnnotations.Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }

    [Required]
    [MyRemote(typeof(RegisterViewModel),"MyOtherProperty")]
    public string MyOtherProperty { get; set; }
}

正如我在评论中提到的那样,您应该根据自己的需求选择专业的供应商。

希望这可以帮助您。

更新: 根据您的评论,我更新了实现方式,使其接受属性名称并执行一些特定于属性名称的连接。


然而,如果页面上有多个需要检查的字段,我该如何告诉验证器我正在引用哪个字段呢? - RealityDysfunction
@RealityDysfunction 根据您的评论,我更新了答案。 - Kemâl Gültekin
我的问题是:“你只需要装饰想要远程检查的属性”...但我做不到这一点,因为这些属性是动态创建的,我从不知道何时需要装饰,以及需要检查什么内容,这由数据库决定。 - RealityDysfunction
1
@RealityDysfunction 嗯...我们的属性都没用了 :) 也许你应该实现自己的自定义验证,可以从抽象类和一个抽象的验证方法派生这些实体。 - Kemâl Gültekin
谢谢你的帮助,我学到了一些关于这些属性的东西。 - RealityDysfunction
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接