Validation Application Block not reading specified ruleset from configuration

Nov 28, 2014 at 10:47 AM
Edited Nov 28, 2014 at 12:50 PM
I am using Enterprise Library 5.0 (Validation Application Block)

ValidatorFactory valFactory = EnterpriseLibraryContainer.Current.GetInstance<ValidatorFactory>();
Validator<QuantumSV> quantumValidator = valFactory.CreateValidator<SV>("Validation Ruleset");

SV objRule = new SV();

objRule.FirstName = txtFirstName.Text;
objRule.LastName = txtLastName.Text;
objRule.Email = txtEmail.Text;
objRule.AccountNumber = txtAccountNumber.Text;
objRule.CaseID =0;

objRule.CaseType = string.Empty;
ValidationResults valResults = quantumValidator.Validate(objRule);
Nov 30, 2014 at 6:54 AM
Is QuantumSV a typo? The posted code will not compile. Are you trying to validate using base classes? Are you trying to use one of the integration approaches (Winforms, WPF)?

The code looks OK and should work provided that the configuration is in the web/app.config.

Can you expand on the exact scenario along with configuration?

Thanks,

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Dec 1, 2014 at 8:08 AM
Edited Dec 1, 2014 at 8:10 AM
Yes, QuantumSV, Quantum has been removed when posting details over here.
Code is compiling at my end as its correct as far as syntax and name goes.

Kindly help me out as my code is executing but not giving me right result as it always return results.IsValid as true.
I am using with ASP.Net and also using PropertyProxyValidator to accomplish this.
Dec 1, 2014 at 2:56 PM
Hi Randy,
Thanks a lot for your kind reply, I am able to find the issue and resolved on my own as issue is when I am setting object property and when it execute objRule.FirstName = txtFirstName.Text then it didn't file ruleset against this attribute so I change my object of this class (set property).
This thing work fine

But I need to show error messages against the control using PropertyProxyValidator.
Is it possible to show error messages using PropertyProxyValidator just like we used to do using ASP.Net Validation controls.
If yes then hows as I tried to do that but its not working.
Dec 1, 2014 at 5:38 PM
I'm not sure I follow you. You said:
I am using with ASP.Net and also using PropertyProxyValidator to accomplish this.
But then ask:
Is it possible to show error messages using PropertyProxyValidator just like we used to do using ASP.Net Validation controls.
So, I'm a bit confused about what you are trying to achieve. You can use configuration and/or attributes for validation and they should hook into the PropertyProxyValidator.

For configuration, I would just make sure that the defaultRuleset in configuration is set to the ruleset name in the configuration. Unless you are explicitly setting the RulesetName attribute on the PropertyProxyValidator.
<asp:TextBox ID="FirstNameTextBox" runat="server" />
<cc1:PropertyProxyValidator ID="FirstNameValidator" runat="server" 
    ControlToValidate="FirstNameTextBox" PropertyName="FirstName" 
    SourceTypeName="ValidationHOL.BusinessLogic.Customer, ValidationHOL.BusinessLogic">
</cc1:PropertyProxyValidator>

 <validation>
  <type name="ValidationHOL.BusinessLogic.Customer" defaultRuleset="MyRuleset" <!-- set this -->
   assemblyName="ValidationHOL.BusinessLogic, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
   <ruleset name="MyRuleset"> <!-- Set this to match defaultRuleset above -->
    <properties>
      <property name="FirstName">
        <validator type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.StringLengthValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
         upperBound="10" lowerBound="5" lowerBoundType="Inclusive" upperBoundType="Ignore"
         name="String Length Validator" />
        <validator type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.NotNullValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
         name="Not Null Validator" />
      </property>
    </properties>
   </ruleset>
  </type>
 </validation>

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Dec 2, 2014 at 9:37 AM
Thanks a lot for your kind reply,
I am using with ASP.Net and also using PropertyProxyValidator to accomplish this.
Basic functionality working fine, I am able to do validation but using below code...
Note : For me PropertyProxyValidator is not showing error message as desired just like we used to do using asp.net validation controls.
Thats why I mentioned is it possible to display error messages using that control as its not working for me..
(ideally it should be working in that way) ..


if (!results.IsValid)
    {
        foreach (ValidationResult objResult in results)
        {
            Response.Write(objResult.Message);
            Response.Write("<br>");
        }
    }

// ***** ASPX ******** //
<form id="form1" runat="server"> <div> <table> <tr><td align="left"> <asp:Label ID="FirstName" runat="server" Text="First Name"></asp:Label> :</td> <td> <asp:TextBox id="txtFirstName" runat="server" /> <EntLibValidators:PropertyProxyValidator ID="FirstNameValidator" PropertyName="FirstName" ControlToValidate="txtFirstName" runat="server" SourceTypeName="QuantumLibrary.QuantumSV,QuantumLibrary" RulesetName="Validation Ruleset" EnableClientScript="true" Text="First Name should be valid" ErrorMessage="First Name should be valid" Display="Static"></EntLibValidators:PropertyProxyValidator> <br /> </td></tr> <tr><td align="left"> <asp:Label ID="LastName" runat="server" Text="Last Name"></asp:Label> :</td> <td> <asp:TextBox id="txtLastName" runat="server" /><br /> <EntLibValidators:PropertyProxyValidator ID="LastNameValidator" PropertyName="LastName" ControlToValidate="txtLastName" runat="server" SourceTypeName="QuantumLibrary.QuantumSV,QuantumLibrary" RulesetName="Validation Ruleset" EnableClientScript="true" Text="Last Name should be valid" ErrorMessage="Last Name should be valid" Display="Static"></EntLibValidators:PropertyProxyValidator> </td></tr> <tr><td align="left"> <asp:Label ID="AccountNumber" runat="server" Text="Account Number"></asp:Label> : </td> <td> <asp:TextBox id="txtAccountNumber" runat="server" /><br /> <EntLibValidators:PropertyProxyValidator ID="AccountNumberValidator" PropertyName="AccountNumber" ControlToValidate="txtAccountNumber" runat="server" SourceTypeName="QuantumLibrary.QuantumSV, QuantumLibrary" RulesetName="Validation Ruleset" EnableClientScript="true" Text="Account Number should be valid" ErrorMessage="Account Number should be valid" Display="Static"></EntLibValidators:PropertyProxyValidator> </td></tr> <tr><td align="left"> <asp:Label ID="PhoneNumber" runat="server" Text="Phone Number"></asp:Label> : </td><td> <asp:TextBox id="txtPhoneNumber" runat="server" /><br /> <EntLibValidators:PropertyProxyValidator ID="PhoneValidator" PropertyName="PhoneNumber" ControlToValidate="txtPhoneNumber" runat="server" SourceTypeName="QuantumLibrary.QuantumSV, QuantumLibrary" RulesetName="Validation Ruleset" EnableClientScript="true" Text="Phone Number should be valid" ErrorMessage="Phone Number should be valid" Display="Static"></EntLibValidators:PropertyProxyValidator> </td></tr> <tr><td align="left"> <asp:Label ID="Email" runat="server" Text="Email"></asp:Label> : </td><td> <asp:TextBox id="txtEmail" runat="server"/><br /> <EntLibValidators:PropertyProxyValidator ID="EmailValidator" PropertyName="Email" ControlToValidate="txtEmail" runat="server" SourceTypeName="QuantumLibrary.QuantumSV, QuantumLibrary" RulesetName="Validation Ruleset" EnableClientScript="true" Text="Email should be valid" ErrorMessage="Email should be valid" Display="Static"></EntLibValidators:PropertyProxyValidator> </td></tr> <tr><td align="left"> <asp:Label ID="PostCode" runat="server" Text="Post Code"></asp:Label> : </td><td> <asp:TextBox id="txtPostCode" runat="server" /> <%--<EntLibValidators:PropertyProxyValidator ID="PropertyProxyValidator4" runat="server" ControlToValidate="txtPostCode" PropertyName="ValidatePostCode" EnableViewState="false" EnableClientScript="true"></EntLibValidators:PropertyProxyValidator>--%> </td></tr> <tr><td colspan="2"> <asp:Button ID="btnShow" runat="server" Text="Validate" onclick="btnShow_Click" /></td></tr> </table> </div> </form> // ***** Code Behind ******** //
using QuantumLibrary;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Validation.Integration.AspNet;

public partial class _Default : System.Web.UI.Page
{
//private Validator<QuantumSV> quantumValidator;

protected void Page_Load(object sender, EventArgs e)
{
    //quantumValidator = ValidationFactory.CreateValidator<QuantumSV>();
}

protected void btnShow_Click(object sender, EventArgs e)
{
    QuantumSV objRule = new QuantumSV();

    objRule.FirstName = txtFirstName.Text;
    objRule.LastName = txtLastName.Text;
    objRule.Email = txtEmail.Text;
    objRule.AccountNumber = txtAccountNumber.Text;
    objRule.PhoneNumber = txtPhoneNumber.Text;

    ValidationResults results = Validation.Validate(objRule);

     FirstNameValidator.Validate();
    LastNameValidator.Validate();
    AccountNumberValidator.Validate();

    PhoneValidator.Validate();
    EmailValidator.Validate();

    if (!FirstNameValidator.IsValid)
    {
        FirstNameValidator.Text = FirstNameValidator.ErrorMessage;
    }

    Page.Validate();

    if (!results.IsValid)
    {
        foreach (ValidationResult objResult in results)
        {
            Response.Write(objResult.Message);
            Response.Write("<br>");
        }
    }
}
}

// *** Config ***** //
<configuration>
<configSections>
<section name="validation" type="Microsoft.Practices.EnterpriseLibrary.Validation.Configuration.ValidationSettings, Microsoft.Practices.EnterpriseLibrary.Validation, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
 </configSections>
<validation>
<type name="QuantumLibrary.QuantumSV" defaultRuleset="Validation Ruleset" assemblyName="QuantumLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
  <ruleset name="Validation Ruleset">
    <properties>
      <property name="LastName">
        <validator type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.NotNullValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
          messageTemplate="Last Name must be entered" name="Not Null Validator" />
        <validator type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.StringLengthValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
          upperBound="2" lowerBound="0" lowerBoundType="Ignore" messageTemplate="Last Name should not be null"
          name="String Length Validator" />
      </property>
      <property name="Email">
        <validator type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.NotNullValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
          messageTemplate="Email must be entered" name="Not Null Validator" />
        <validator type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.StringLengthValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
          lowerBoundType="Inclusive" messageTemplate="Email should not be null"
          name="String Length Validator" />
      </property>
      <property name="PhoneNumber">
        <validator type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.NotNullValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Versio
Dec 2, 2014 at 1:11 PM
Edited Dec 2, 2014 at 1:12 PM
Code looks OK to me. Here is a shortened version of the code you posted that is working (except I changed the namespaces/assembly name) :

Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ValidationHOL.Web._Default" %>

<%@ Register Assembly="Microsoft.Practices.EnterpriseLibrary.Validation.Integration.AspNet" Namespace="Microsoft.Practices.EnterpriseLibrary.Validation.Integration.AspNet" TagPrefix="entlibvalidators" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table>
                <tr>
                    <td align="left">
                        <asp:Label ID="LastName" runat="server" Text="Last Name"></asp:Label>
                        :</td>
                    <td>
                        <asp:TextBox ID="txtLastName" runat="server" /><br />
                        <entlibvalidators:PropertyProxyValidator ID="LastNameValidator" PropertyName="LastName" ControlToValidate="txtLastName" runat="server" SourceTypeName="ValidationHOL.BusinessLogic.QuantumSV, ValidationHOL.BusinessLogic" RulesetName="Validation Ruleset" EnableClientScript="true" Text="Last Name should be valid" ErrorMessage="Last Name should be valid" Display="Static"></entlibvalidators:PropertyProxyValidator>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <asp:Button ID="btnShow" runat="server" Text="Validate" OnClick="btnShow_Click" /></td>
                </tr>
            </table>
        </div>
    </form>

</body>
</html>
Default.aspx.cs:
using Microsoft.Practices.EnterpriseLibrary.Validation;
using System;
using ValidationHOL.BusinessLogic;

namespace ValidationHOL.Web
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void btnShow_Click(object sender, EventArgs e)
        {
            if (!this.IsValid)
                return;
        }
    }
}

QuantumSV:
namespace ValidationHOL.BusinessLogic
{
    public class QuantumSV
    {
        public string LastName { get; set; }
    }
}

web.config:
  <validation>
    <type name="ValidationHOL.BusinessLogic.QuantumSV" defaultRuleset="Validation Ruleset" assemblyName="ValidationHOL.BusinessLogic, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
      <ruleset name="Validation Ruleset">
        <properties>
          <property name="LastName">
            <validator type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.NotNullValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
              messageTemplate="Last Name must be entered" name="Not Null Validator" />
            <validator type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.StringLengthValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
              upperBound="2" lowerBound="0" lowerBoundType="Ignore" messageTemplate="Last Name should be less than 2 characters"
              name="String Length Validator" />
          </property>
        </properties>
      </ruleset>
    </type>
  </validation>

The Hands-On Labs for Microsoft Enterprise Library 5.0 has an exercise for ASP.NET integration using the PropertyProxyValidator (it's what I used as a starting point for testing). You might want to take a look at that .

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Dec 2, 2014 at 4:51 PM
I am receiving below error :

The Source Type Name cannot be NULL when executing due to Integration.AspNet DLL but don't know why its coming.
Dec 2, 2014 at 5:15 PM
One troubleshooting approach would be to remove the PropertyProxyValidators so that the page is working (without validation) and then add them back one at a time and see what is causing the issue.

You could also create a small sample project (and upload it) demonstrating the issue so that I can look at it.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Dec 3, 2014 at 9:23 AM
I tried using new web application project (small project) as I've read somewhere there proxyvalidatior work only in web application and not in web project.

Below is my sample code and it doesn't work due to proxyvalidator and this.IsValid condition return true (while it should not return true), so validation doesn't work..

But when commenting proxyvalidator and use without this.Isvalid, using result.IsValid, validation work fine.

What is wrong in my code.. I am not receiving any error but when using proxyvalidator, this.IsValid condition return true (which is wrong)

// Sample Code (code behind)
using QuantumLibrary;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Validation.Integration.AspNet;

namespace ELVABConfigProxyDemonstration
{
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (!this.IsValid)
        {
            return;
        }
}
}

// aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ELVABConfigProxyDemonstration._Default" %>
<%@ Register Assembly="Microsoft.Practices.EnterpriseLibrary.Validation.Integration.AspNet" Namespace="Microsoft.Practices.EnterpriseLibrary.Validation.Integration.AspNet"
TagPrefix="EntLibValidators" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server"> <title></title> </head> <body>
<form id="form1" runat="server">
<asp:Label ID="lblFirstName" runat="server" Text="First Name"></asp:Label>
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
<EntLibValidators:PropertyProxyValidator ID="FirstNameValidator" PropertyName="FirstName" ControlToValidate="txtFirstName" runat="server"
        SourceTypeName="QuantumLibrary.QuantumSV" RulesetName="Validation Ruleset" EnableClientScript="true" 
        Text="First Name should be valid" ErrorMessage="First Name should be valid" Display="Static"></EntLibValidators:PropertyProxyValidator>
        <asp:Button ID="btnSubmit" runat="server" Text="Validate" 
    onclick="btnSubmit_Click" />
<div>

</div>
</form>
</body>
</html>

// web.config
<configSections>
  <section name="validation" type="Microsoft.Practices.EnterpriseLibrary.Validation.Configuration.ValidationSettings, Microsoft.Practices.EnterpriseLibrary.Validation, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
  <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
    <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
      <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
        <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere" />
        <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
        <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
        <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
      </sectionGroup>
    </sectionGroup>
  </sectionGroup>
</configSections>  

<validation>
    <type name="QuantumLibrary.QuantumSV" defaultRuleset="Validation Ruleset"
        assemblyName="QuantumLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
        <ruleset name="Validation Ruleset">
            <properties>
                <property name="FirstName">
                    <validator type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.NotNullValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                        messageTemplate="First name can't be null" name="Not Null Validator" />
                </property>
            </properties>
        </ruleset>
    </type>
</validation>
Dec 3, 2014 at 11:02 AM
The Text property of a TextBox will not be null ('The default is an empty string ("").') so a NotNullValidator won't work. Instead use a StringLengthValidator:
<validator type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.StringLengthValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
lowerBound="1" lowerBoundType="Inclusive" upperBound="2" upperBoundType="Ignore" messageTemplate="First name can't be null" name="Not Null Validator" />
  
~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Dec 3, 2014 at 1:25 PM
Thanks Randy but it doesn't help as I said in my prior post as when using ProxyValidator this.IsValid condition return true (Which is incorrect)

And Otherwise (without ProxyValidator) it work fine even when I am using Null Validator and in my QuantumSV Class File (I am only setting checking value property when its Not Null).
Dec 5, 2014 at 10:55 AM
I tried using StringLengthValidator, Still i face same issue with ProxyValidator.
Dec 5, 2014 at 11:15 AM
The code I posted is working so it seems like there is else something going on. Can you upload a sample project (e.g. to OneDrive)?

Thanks,

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Dec 5, 2014 at 1:41 PM
My Company/client premises don't allow uploading project.
Dec 6, 2014 at 3:07 AM
Here's a sample based on the the Hands on Labs that is working: http://1drv.ms/1z5GW5o