Logo

NHibernate

The object-relational mapper for .NET

Validator

This page is converted from the old nhforge.org Wiki. Published by: Ricardo Peres on 10-09-2009

NHibernate Property Validator for ASP.NET

Suppose you want to validate an ASP.NET control based on some validation attributes from a property. You may know that the Enterprise Library Application Block's PropertyProxyValidator does just this, the problem is that it uses Enterprise Library's own validation attributes. If you want to use NHibernate Validator's, your on your own.

I have written a small validator control, based on Enterprise Library's, that may come in handy. Here is the code:

public class NHibernatePropertyValidatorBaseValidator

{

    private ValidationSummaryDisplayMode displayMode;

    private String propertyName;

    private String sourceTypeName;

    protected override void OnInit(EventArgs e)

    {

        if ((this.Enabled == true) && (this.Visible == true))

        {

            this.Page.RegisterRequiresControlState(this);

        }

        base.OnInit(e);

    }

    protected override void LoadControlState(Object savedState)

    {

        Object [] state = savedState as Object [];

        base.LoadControlState(state[0]);

        this.displayMode = (ValidationSummaryDisplayMode) state [ 1 ];

        this.PropertyName = (String) state [ 2 ];

        this.SourceTypeName = (String) state [ 3 ];

    }

    protected override Object SaveControlState()

    {

        Object [] state = new Object [ 4 ];

        state [ 0 ] = base.SaveControlState();

        state [ 1 ] = this.DisplayMode;

        state [ 2 ] = this.PropertyName;

        state [ 3 ] = this.SourceTypeName;        return (state);

    }

    protected override Boolean EvaluateIsValid()

    {

        Type type = Type.GetType(this.SourceTypeName, false);

        if (type != null)

        {

            PropertyInfo prop = type.GetProperty(this.PropertyName, BindingFlags.Instance |BindingFlags.Public | BindingFlags.GetProperty);

 

            if (prop != null)

            {

                IClassValidator validator = new ClassValidator(type);

                Object obj = Activator.CreateInstance(type);

                Object value = this.GetControlValidationValue(this.ControlToValidate);

                prop.SetValue(obj, Convert.ChangeType(value, prop.PropertyType), null);

                InvalidValue [] results = validator.GetInvalidValues(obj, this.PropertyName);

                base.ErrorMessage = this.formatErrorMessage(results);

                return (results.Length == 0);

            }

        }

        base.ErrorMessage = String.Empty;

 

        return (true);

    }

    private String formatErrorMessage(InvalidValue [] results)

    {

        String str = String.Empty;

        String str2 = String.Empty;

        String str3 = String.Empty;

        String str4 = String.Empty;

        StringBuilder builder = new StringBuilder();        switch (this.DisplayMode)

        {

            case ValidationSummaryDisplayMode.List:

            str3 = "<br/>";

            break;

            case ValidationSummaryDisplayMode.SingleParagraph:

            str3 = " ";

            str4 = "<br/>";

            break;

            default:

            str = "<ul>";

            str2 = "<li>";

            str3 = "</li>";

            str4 = "</ul>";            break;

        }

        if (results.Length != 0)

        {

            builder.Append(str);

 

            foreach (InvalidValue result in results)

            {

                builder.Append(str2);

                builder.Append(result.Message);

                builder.Append(str3);

            }

            builder.Append(str4);

        }

 

        return (builder.ToString());

    }

    [Browsable(true)]

    [DefaultValue(ValidationSummaryDisplayMode.List)]

    public ValidationSummaryDisplayMode DisplayMode

    {

        get

        {

            return (this.displayMode);

        }

        set

        {

            this.displayMode = value;

        }

    }

    [Browsable(
true)]

    [DefaultValue("")]

    public String PropertyName

    {

        get

        {

            return (this.propertyName);

        }

        set

        {

            this.propertyName = value ?? String.Empty;

        }

    }

    [
Browsable(true)]

    [DefaultValue("")]

    public String SourceTypeName

    {

        get

        {

            return (this.sourceTypeName);

        }

        set

        {

            this.sourceTypeName = value ?? String.Empty;

        }

    }

}

Basically, it uses the NHibernate Validator's ClassValidator to do the actual validation.

This is how you would use it:

<asp:TextBox runat="server" ID="email"/>

<xxx:NHibernatePropertyValidator runat="server" ControlToValidate="email" SourceTypeName="SomeClass, SomeAssembly" PropertyName="Email"/>

If you combine it with Web Client Software Factory's ServerSideValidationExtnder, you can even have client-side (AJAX) validation. Give it a try!

© NHibernate Community 2024