如何使用自定义验证在MVC中实现企业库 [英] How to implement enterprise library in MVC with custom validations

查看:48
本文介绍了如何使用自定义验证在MVC中实现企业库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已尝试过一些代码,但我不能在这里分享,因为它是保密的,所以请帮助我



我尝试了什么:



示例代码



我的模特儿这里



类客户

{

隐藏复制代码

公共字符串名称{get; set;}

隐藏复制代码

公共字符串城市{get; set;}

[ELCustomValidation]

隐藏复制代码

公共字符串电子邮件{get; set;}

}



EnterpriseLibrary包含类



公共类ELCustomValidation:Validator

{

静态只读Regex Email =新正则表达式(Regex Expr here);



Public ELCustomValidation(string MessageTemplet,string Tag):base(MessageTemplet,Tag)

{

}



受保护的覆盖字符串DefaultMessageTemplate

{

get

{

抛出新的NotImplementedException;

}

}



protected override void DoValidate(string objectToValidate,object currenttrget,string key,ValidationResult validationresult )

{

if(string.IsNullorEmpty(objectToValidate))

{return;}

匹配匹配= Email.match(objectToValidate);



if(!match.success)

{

LogValidationResult(validationresult ,电子邮件ID无效,currenttrget,key)

}

}



}





我的AttributeClass这里



[AttributeUsage(Attributetargets.property | Attributetargets。字段)]

公共类ELCustomeValidationAttribute:ValidatorAttribute

{

受保护的覆盖Validatior DoCreateValidator(类型目标)

{

返回ELCustomValidation(string,Tag);

}



}





和错误



一个vali ELcustomValidationAttribute类型的dation属性不能用于验证值

I have tried some code but I cant share it here because it is confidential so please kindly help me

What I have tried:

Sample Code here

My Model Here

Class Customer
{
Hide Copy Code
Public string Name{get;set;}
Hide Copy Code
Public string City{get;set;}
[ELCustomValidation]
Hide Copy Code
Public string Email{get;set;}
}

EnterpriseLibrary Included Class

Public class ELCustomValidation : Validator
{
static readonly Regex Email=new regex("Regex Expr here");

Public ELCustomValidation(string MessageTemplet,string Tag):base(MessageTemplet,Tag)
{
}

protected override string DefaultMessageTemplate
{
get
{
throw new NotImplementedException ;
}
}

protected override void DoValidate(string objectToValidate,object currenttrget,string key,ValidationResult validationresult)
{
if(string.IsNullorEmpty(objectToValidate))
{return;}
Match match=Email.match(objectToValidate);

if(!match.success)
{
LogValidationResult(validationresult,"Email Id is not valid",currenttrget,key)
}
}

}


My AttributeClass here

[AttributeUsage(Attributetargets.property |Attributetargets.field)]
public class ELCustomeValidationAttribute : ValidatorAttribute
{
protected override Validatior DoCreateValidator(type target )
{
return ELCustomValidation("string",Tag);
}

}


And Error

A validation attribute of type ELcustomValidationAttribute can not be used to validate values

推荐答案

使用实现自定义验证的selfValidation方法。

这很简单更好的方法。

它自动合并你的内置验证和自定义验证的结果

参考 EntLib中的MSDN自我验证
Use selfValidation approach which implements custom validations.
It is quite simple and better approach.
It automatically merges results of your in built validations and custom validation
Refer MSDN Self Validation In EntLib


这篇关于如何使用自定义验证在MVC中实现企业库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆