Validate Dependent Fields with Custom Validators
In general, validation in an Iron Speed Designer application is done exactly the same as any .NET application.
- Pooja Daga, Technical Support Engineer, Iron Speed, Inc.

April 12, 2006
Iron Speed Designer V3.2

Procedure
Many times, your application will need additional, customized data validation for certain fields or certain types of fields. You might, for example, find it useful to:
  • Validate related fields where values must be specified in one field or the other.
  • Validate dependent fields where the value of one field is dependent on the value of another field.
  • Perform page-level validation.
In a .NET application, the only validator that checks for the presence of data is the Required Field Validator. Thus, Custom Validators in .NET are only called if a value is specified. This complicates validating related fields or dependent fields if there is no value specified for the field. How can this be done? In an Iron Speed Designer generates a RecordControlCustomValidator for each field. When enabled, it calls the RecordControl.ValidateData function. ValidateData returns False when the data is invalid and sets an error message. You can have ValidateData called for as many fields as you want, although generally once is enough to check the values of all fields on a page.

The example below validates fields on an Add Orders Page for the Orders table in the Southwind database. This particular code validates the ShipPostalCode and ShipCountry fields to ensure one or the other field is specified. The RecordControlCustomValidator for the ShipPostalCode field is enabled in the Init event for the OrdersRecordControl class as shown below.

For .NET Framework 1.1, add the code below in AddOrdersPage.aspx.cs, located in

...\<Application Folder>\Orders\AddOrdersPage.aspx.cs

For .NET Framework 2.0, add the code below in AddOrdersPage.Controls.cs, located in

...\<Application Folder>\<App_Code>\Orders\AddOrdersPage.Controls.cs

C#:

public OrdersRecordControl()
{
    this.Init += new EventHandler(RecordControl_Init);
}
 
private void RecordControl_Init(object sender, EventArgs e)
{
    this.ShipPostalCodeRecordControlCustomValidator.Visible=true;
    this.ShipPostalCodeRecordControlCustomValidator.Enabled=true;
}

Once the RecordControlCustomValidator for ShipPostalCode is enabled, you can override the ValidateData() method in the OrdersRecordControl class.

protected override bool ValidateData(RecordControlCustomValidator v)
{
    if(this.ShipPostalCode.Text=="" && this.ShipCountry.Text=="")
    {
        v.ErrorMessage="One of Postal Code or Country must be specified.";
        return false;
    }
    return true;
}

Visual Basic .NET:

Private Sub MyPage_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Init
    If (Not Me.Page.IsPostBack) Then
        Me.ShipPostalCodeRecordControlCustomValidator.Visible = True
        Me.ShipPostalCodeRecordControlCustomValidator.Enabled = True
    End If
End Sub

Once the RecordControlCustomValidator for ShipPostalCode is enabled, you can override the ValidateData() method in the OrdersRecordControl class.

Protected Overrides Function ValidateData(ByVal v As BaseClasses.Web.UI.WebControls.RecordControlCustomValidator) As Boolean
 
    If Me.ShipPostalCode.Text = "" And Me.ShipCountry.Text = "" Then
        v.ErrorMessage = "One of Postal Code or Country must be specified."
        Return False
    End If
    Return True
End function

Additional Resources

In general, validation in an Iron Speed Designer application is done exactly the same as any .NET application. Here are resources you might find helpful:

How to validate (using a custom validator) a field according to an option selected in a listbox:

http://searchvb.techtarget.com/vsnetATEAnswers/0,293820,sid8_gci912327_tax293672,00.html

The above example also shows how to do validation in JavaScript. You can place the JavaScript in the HTML layout page and it will be passed unmodified to the ASPX page generated by Iron Speed Designer.

The following example shows how to create a custom RequiredIfValidator. This might be helpful if you are doing this often.

https://secure.codeproject.com/cs/miscctrl/RequiredIfValidator.asp

The following example shows how to use the RequiredFieldValidator with other validation controls to handle blank entries.

http://support.microsoft.com/default.aspx?scid=kb;en-us;313044

About the Author
Pooja Daga
Technical Support Engineer, Iron Speed, Inc.

Pooja has experience developing Web applications using .NET technology. She started her career with PCS, a leading software services company headquartered in India and has been with Iron Speed since 2005.

Pooja holds a M.S. degree in Computer Application and a B.S. degree in Electrical engineering from Maharaja Sayajirao University in India.



  Privacy Statement