Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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
Posted
Updated 15-Jul-16 19:31pm
v2
Comments
F-ES Sitecore 15-Jul-16 8:22am    
Go through the instructions\tutorials\documentation for Enterprise Library, it is extensively documented. If you don't want to post confidential code then reproduce your issue using sample code.
Atmesh Sonawane 16-Jul-16 0:03am    
Here What I have tried please refer this

1 solution

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
 
Share this answer
 
v2
Comments
Atmesh Sonawane 16-Jul-16 5:00am    
Thank you, but How can I write custom validation using VAB like data annotations

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900