Validation Application block in web.config

Topics: Validation Application Block
Mar 16, 2009 at 6:23 AM
Hi,
I am a beginner to EntLib. I have installed Enterprise libraray 4.1. I am using a validation application block and validating some properties of my Business Entities like EmpName,EmpID,Salary etc through web.config in my web application . My Business Entity is a C# Class Library.
I have created a Ranga Validator for length range between 2-8 characters and also a String Length Validator. But it validates even when I enter values between the specified Range.
this is the code: 

string

[] split = b1.ToString().Split(new char[] { ',' });

 

 

//b1 is a string builder whcih gets the values of all the textboxes
int
c1;

 

 

for (c1 = 0; c1 < split.Length; c1++)

 

{

objBE.EmpID =

int.Parse(split[0].ToString());

 

objBE.Name = split[1].ToString();

objBE.Exp = split[2].ToString();

objBE.Grade = split[3].ToString();

objBE.Salary= split[4].ToString();

objBE.EmpDept = split[5].ToString();

c1 = split.Length;

 

break;
}

 

ValidationResults

results = Validation.Validate<EmployeeBE>(objBE, "Default");

 

 

if (!results.IsValid)

 

{

 

foreach (ValidationResult re in results)

 

{

Response.Write(re.Message);

 

}

}

 

else

 

 

 

{

 

//here i am saving the data in a table.

 

}

Please tell me why it is validating even if I enter the correct Range or length of the string.
Thanks in advance.

Mar 16, 2009 at 6:41 AM
Edited Mar 16, 2009 at 6:42 AM
Did you specified the default rule set of the class?  You can check this by selecting the class in the validation section, look for the Default Ruleset property.


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com
Mar 16, 2009 at 6:51 AM
Yes, I have specified the rule set as a Default.
Mar 16, 2009 at 7:12 AM
I failed to clarify first your problem.  Are you getting a validationresult of false even when you entered the correct range and length or are you always getting a validation result of true even when entering invalid range and length of string?  Can you tell me exactly the validators for each property you are having problem with?


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com
Mar 16, 2009 at 7:35 AM
Edited Mar 16, 2009 at 8:25 AM
For Employee Name I have given a property as public string Name { get; set; } in my BE. In Enterprise Lib Configuration I have opened my web.config. And I have given a String Length Validator LowerBound=2,  LowerBoundType=Inclusive, UpperBound=8 , UpperBoundType=Inclusive. Negated=false.
this is the code in web.config:

<

validation>

 

<

 

type defaultRuleset="Default" assemblyName="BL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"

 

 

 

name="BE.EmployeeBE">
<
ruleset name="Default">
<
properties>
<
property name="EmpID" />
<
property name="Name">
<
validator lowerBound="2" lowerBoundType="Inclusive" upperBound="8" upperBoundType="Inclusive" negated="false" messageTemplate="Enter Name between 2-8 letters" messageTemplateResourceName="" messageTemplateResourceType=""tag=""  type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.StringLengthValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
name="String Length Validator" />
</
property>
<
property name="Salary" /> 
<
property name="EmpDept" /> 
 <
property name="Grade" /> 
<
property name="Exp" /> 
</
properties>
 </
ruleset>
</
type
</
validation>


When I enter a single letter in the textbox for the EmpName, it should display the error message. but it is not. It is returning True in if (!results.IsValid)

 

 

 

Mar 16, 2009 at 7:56 AM
Were you able to verify that the value that gets assigned to the Name property has indeed a length of 1?  No extra empty spaces?  (!results.IsValid) should really return true since results.IsValid is false.  


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com
Mar 16, 2009 at 8:37 AM

foreach

(ValidationResult re in results){Response.Write(re.Message);}
In the above statement, the property IsValid in "results" is "TRUE" and validationResults = Count = 0.

 

objBE.Name = split[1].ToString();
Here, objBE.Name is returning 'q'.

Mar 16, 2009 at 9:07 AM
Just to make sure I tried to repro your case but I got it working fine.  There are only 2 causes I can think of why validation wouldn't work in your case, the first one is the absence of default ruleset which you have already verified is present.  The second one is the configuration file where the validation section is defined.  But from your post, I could see that you placed it in the executable project.  Could you send me your solution?


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com