Validation Result always true

Topics: Validation Application Block
Aug 15, 2007 at 6:47 PM
I am attempting to validate input from and ASPX where the rules are defined in the web.config and my result is always true which it should not be. I have had someone more familiar than myself look at this and he was unsuccessful as well with my code example. Can anyone tell me where I have gone wrong? I am using .NET 2.0, AJAX enabled web, and EL 3.1

Core ASPX:
Email Address: <asp:TextBox ID="EmialAddressTextBox" runat="server" Width="379px"></asp:TextBox><br />
<asp:Button ID="SubmitButton" runat="server" OnClick="SubmitButton_Click" Text="Go -->" /><br />
<asp:Label ID="ResultLabel" runat="server"></asp:Label>

Then the btn event:
Page.Validate();

if (Page.IsValid)
{
Person person = new Person();
person.EmailAddress = EmialAddressTextBox.Text;

StringBuilder sb = new StringBuilder();

Validator<Person> personValidator = ValidationFactory.CreateValidatorFromConfiguration<Person>("PersonRuleSet");
ValidationResults results = personValidator.Validate(person);

//ValidationResults results = Validation.ValidateFromConfiguration<Person>(person, "PersonRuleSet");
if (!results.IsValid)
{
foreach (ValidationResult result in results)
{
sb.Append("Key = " + result.Key);
sb.Append("Message = " + result.Message);
}
}
ResultLabel.Text = sb.ToString();
}

Finally the web.config:
<configSections>
<section name="validation" type="Microsoft.Practices.EnterpriseLibrary.Validation.Configuration.ValidationSettings, Microsoft.Practices.EnterpriseLibrary.Validation, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
<sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
<section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication" />
<sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
<section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="Everywhere" />
<section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication" />
<section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication" />
</sectionGroup>
</sectionGroup>
</sectionGroup>
</configSections>
<validation>
<type assemblyName="BLL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
name="BLL.Person">
<ruleset name="PersonRuleSet">
<properties>
<property name="UserName" />
<property name="EmailAddress">
<validator lowerBound="10" lowerBoundType="Ignore" upperBound="30"
upperBoundType="Inclusive" negated="false" messageTemplate="Email Address must be 10 to 30 characters!"
messageTemplateResourceName="" messageTemplateResourceType=""
tag="" type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.StringLengthValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
name="String Length Validator" />
</property>
<property name="PhoneNumber" />
<property name="State" />
</properties>
</ruleset>
</type>
</validation>

I do not have any problems placing the Validation within the class itself and using the PropertyProxyValidator only where I try the above. Ideas?
Aug 16, 2007 at 1:36 PM
Does anyone have any suggestions on this issue?
Aug 16, 2007 at 1:48 PM
Attach a debugger to your app, and inspect the validator you get. If it's "empty" then there is some issue with the configuration, otherwise there might be a problem with the value you're feeding to the validator.

Also make sure the "Person" type youre using as a generic parameter is the same "Person" type referenced by the configuration file.

Regards,
Fernando
Aug 17, 2007 at 12:15 AM
Edited Aug 17, 2007 at 12:26 AM
I found the issue but I am not sure why. Take the following:

Person person = new Person();
person.EmailAddress = txtBoxEmailAddress.Text;
person.UserName = txtBoxUserName.Text;

If I fire up the ASPX and do not enter any data in my form and submit the form, validation is returned as true. Now modify the above to:

person.EmailAddress = "";
person.UserName = "";

validation returns as true.

try:
person.EmailAddress = null;
person.UserName = null;

validation kicks in and provides the feedback to the ASPX. Why is this?

On top of all of this I just added a string length validator to emailaddress of (5,10) and no matter what I enter in the textbox this validation rule does not pick up.

I am getting more and more confused as to why all this is happening. Any further thoughts?
Aug 17, 2007 at 1:36 AM
According to the configuration file "" is a perfectly valid value because the lower bound is set to "Ignore".

Fernando
Sep 26, 2007 at 9:26 PM
How come it always displays message: "OK"

protected void Page_Load(object sender, EventArgs e)
{
SolutionUser user = new SolutionUser("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
Validator validator = ValidationFactory.CreateValidator<SolutionUser>();
ValidationResults result = validator.Validate(user);

if (!result.IsValid)
Response.Write("Not OK");
else
Response.Write("OK");
}

public class SolutionUser
{
StringLengthValidator(5)
private string _UserGuid;
public string UserGuid
{
get { return _UserGuid; }
set { _UserGuid = value; }
}

public SolutionUser(string userGuid)
{
UserGuid = userGuid;
}
}
Sep 28, 2007 at 3:49 PM
Hi,

You bound the validation attribute to the private field, not the public property.

Fernando