Home > ASP.NET > CodeFluent Entities and ASP.NET Web Forms: Validation

CodeFluent Entities and ASP.NET Web Forms: Validation

In this post we’ll see how to implement validation in a form, data bound to a CodeFluent Entities generated class. We’re going to start from the page created in the Inserting Data post, and we’ll replace the DetailsView with a simple validation-enabled ASP.NET FormView.

Client Validation

First, we’ll start by adding ASP.NET field validators to avoid unnecessary server calls. Here’s the code:

<asp:ObjectDataSource runat="server" ID="ContactDataSource" TypeName="ContactAdmin.Contact"
    DataObjectTypeName="ContactAdmin.Contact" SelectMethod="LoadByEntityKey" InsertMethod="Insert">
        <asp:QueryStringParameter Name="key" QueryStringField="key" DefaultValue="-1" />
<asp:FormView runat="server" DefaultMode="Insert" OnItemInserted="OnContactInserted"
    DataKeyNames="Id, RowVersion" DataSourceID="ContactDataSource">
            <asp:Label runat="server" Text="First Name" /><br />
            <asp:TextBox ID="FirstNameBox" runat="server" AutoCompleteType="FirstName" Text='<%# Bind("FirstName") %>' />
            <asp:RequiredFieldValidator runat="server" Display="Dynamic" ErrorMessage="Field required."
                ControlToValidate="FirstNameBox" ValidationGroup="InsertContact" />
            <br />
            <br />
            <asp:Label runat="server" Text="Last Name" /><br />
            <asp:TextBox ID="LastNameBox" runat="server" AutoCompleteType="LastName" Text='<%# Bind("LastName") %>' />
            <asp:RequiredFieldValidator runat="server" Display="Dynamic" ErrorMessage="Field required."
                ControlToValidate="LastNameBox" ValidationGroup="InsertContact" />
        <asp:LinkButton ID="InsertButton" runat="server" Text="Create" CommandName="Insert"
            ValidationGroup="InsertContact" />
    <asp:Label ID="errorText" runat="server" ForeColor="Red" />

As you can see in the code sample above, we added two RequiredFieldValidator controls which ensure that each of our text boxes (FirstName and LastName) are filled when submitting the form. This way, if the form is incomplete, they’ll display the “Field required.” error message.

Here’s the result in action, if you click create with at least one empty field, no server call will be issued, and an error is shown to the user:



Server Validation

You can also set-up server-side validation. To do so in CodeFluent Entities, you can add one or several validation rules to your entity, on properties, or also extend the automatically generated Validate method with your own custom code using the OnBeforeValidate and/or OnAfterValidate licycle event rules.

As an example, we added a simple String Validation rule on the FirstName and LastName properties indicating that those properties cannot be null or empty, and cannot contain the ‘_’ character:


If you generate over your model, your Business Object Model will now enforce the following rule, and if ever you send a value with an invalid character to the server, it’ll now throw a CodeFluentValidationException. You’ll now need to handle it in your UI, and this is exactly what we’re doing by subscribing to the OnItemInserted event of the FormView. Here’s the event handler:

<script runat="server">    
    private void OnContactInserted(object sender, FormViewInsertedEventArgs e)
        if (e.Exception == null || e.Exception.InnerException == null)

        CodeFluent.Runtime.CodeFluentValidationException vex = e.Exception.InnerException as CodeFluent.Runtime.CodeFluentValidationException;
        if (vex == null)
            return; // Do nothing

        errorText.Text = vex.MessageWithoutCode;
        e.ExceptionHandled = true;
        e.KeepInInsertMode = true;

And here’s the result:


Note: the validation message can be localized and customized using messages in your model.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s