简体   繁体   English

Asp.net MVC 4动态服务器和客户端验证

[英]Asp.net MVC 4 dynamic server and client side validation

My company is developing a multitenancy application that has to be fully customizable through the database. 我公司正在开发必须通过数据库完全自定义的多租户应用程序。 This includes validation, so i created a couple of tables containing information about jquery validation methods and the way they are to be assigned to properties. 这包括验证,因此我创建了几个表,这些表包含有关jquery验证方法及其分配给属性的方式的信息。

I have disabled automatic property validation generation, and marked each relevant property with an attribute that correctly generates multiple client side validation rules. 我已禁用自动属性验证生成,并使用正确生成多个客户端验证规则的属性标记了每个相关属性。 However, the problem arises when i have to validate the model on the server side. 但是,当我必须在服务器端验证模型时,就会出现问题。 I have implemented validations for required, digits and number, however i am not satisfied with that solution. 我已经对必填项,数字和数字进行了验证,但是我对该解决方案不满意。

Is there a way to dynamically link my custom validation attribute with built in attributes? 有没有办法将我的自定义验证属性与内置属性动态链接? The general idea is that my custom validator would recieve a set of required validation methods from the database, and would propagate the validation check to the built in MVC validation method so I don't have to implement it. 通常的想法是,我的自定义验证器将从数据库中接收一组必需的验证方法,并将验证检查传播到内置的MVC验证方法中,因此我不必实现它。

Sorry for not posting the code, but I'm not allowed to do it. 抱歉,没有发布代码,但我不允许这样做。 I would still appreciate a general guideline if this is doable in MVC. 如果这在MVC中可行,我仍然希望您有一个通用的指导原则。

Thank you 谢谢


Update: 更新:

My attribute extends ValidationAttribute and IClientValidatable. 我的属性扩展了ValidationAttribute和IClientValidatable。

Client side validation is not a problem. 客户端验证不是问题。 IClientValidatable returns an enumeration of ModelClientValidationRule that MVC flawlessly translates into proper jquery validation rules. IClientValidatable返回ModelClientValidationRule的枚举,该枚举可将MVC完美地转换为适当的jquery验证规则。 Database stores jquery validation method names, such as 'digits', 'number', etc, along with method parameters. 数据库存储jquery验证方法的名称,例如“数字”,“数字”等以及方法参数。

My problem is with the "public override bool IsValid" method. 我的问题是与“公共替代布尔IsValid”方法。 I can get the list of jquery validations to perform, but i have to manually implement validation methods. 我可以获取要执行的jquery验证的列表,但是我必须手动实现验证方法。 Basically, I want to check the validation rule and if it equals 'required', I want MVC's RequiredAttribute validation to be performed. 基本上,我想检查验证规则,如果它等于“ required”,则希望执行MVC的RequiredAttribute验证。

我之前做过,创建您自己的验证属性,该属性将依赖于数据库信息来验证数据,还创建自定义帮助程序以根据该属性值呈现正确的客户端验证

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM