PrimalForms 2011: Validating the Form – Part 2

September 9th, 2011 by David Corrales
Last updated on September 12th, 2011

 

In Part 1 we discussed the import events and properties that are needed for validation. Now we will discuss how to validate a control. 

How do I validate a control?

As it turns out, validating controls depends on what type of control and the format of the data you wish to validate. Typically you will be validating text that is provided by a TextBox, but it could also come from a ComboBox where the user can type their selection. As for validating the text, it can range from simply ensuring there is value (i.e., not an empty string) or more complex task such as validating the format of an email address. The bad news is that you have to script your validation and it can change depending on the data type and format.

Tip: Use functions and code snippets. Never write the same validation function twice! Once you have written a validation function for a specific format, convert it into a snippet, so the next time you need to validate similar data, you will save time by simply inserting your snippet.

Validation Example 1: Checking if a TextBox Field is Empty

For the first example, we will create a simple function that will ensure the field is empty as well as making sure it doesn’t only contain blank spaces.

function Validate-IsEmptyTrim ([string] $field)
{
    if($field -eq $null -or $field.Trim().Length -eq 0)
    {
        return $true    
    }
    
    return $false
}

Next add a Validating event to the TextBox:

$textboxName_Validating=[System.ComponentModel.CancelEventHandler]{
#Event Argument: $_ = [System.ComponentModel.CancelEventArgs]
    
    $_.Cancel = Validate-IsEmptyTrim $textboxName.Text
}

In this example, we cancel the validation, when the field is empty or has only whitespaces. Since we are canceling, the focus will remain on the TextBox until the user enters the correct format.

How do I let the user know there is a validation failure?

You can use various techniques to notify a user. You can have a label to display the error message or change the foreground color of the TextBox or use a ToolTip control to display a popup message. A good solution is to use the ErrorProvider control. It displays an icon next to the control indicating an error as well as displaying an error message when the user hovers the mouse over the icon. For more details on the ErrorProvider control, refer to the PrimalForms 2011: Spotlight on the ErrorProvider blog article.

Let’s update Example 1 to display an Error Message using an ErrorProvider Control:

$textboxName_Validating=[System.ComponentModel.CancelEventHandler]{

    #Check if the Name field is empty
    $_.Cancel = Validate-IsEmptyTrim $textboxName.Text    
    if($_.Cancel)
    {
        #Display an error message
        $errorprovider1.SetError($textboxName, "Please enter your name.");
    }
}

Next add a Validated event to clear in the ErrorProvider’s message. Since our event will only clear the error message, it can be used as a generic event shared by all validating controls. This is done by passing $this variable to the ErrorProvider’s SetError method. The $this variable represents the control that is calling the event.

$control_Validated={
    #Pass the calling control and clear error message
    $errorprovider1.SetError($this, "");    
}

Reminder: The Validated event will not be called if $_.Cancel was set to $True in the Validating event.

If you don’t want to use the $_.Cancel property in the Validating event (because users may not like it when they cannot select another field when the validation fails), then it is recommended to remove the Validated event and update the Validating event as follows:

$textboxName_Validating=[System.ComponentModel.CancelEventHandler]{

    #Check if the Name field is empty
    $result = Validate-IsEmptyTrim $textboxName.Text    
    if($result -eq $true)
    {
        #Display an error message
        $errorprovider1.SetError($textboxName, "Please enter your name.");
    }
    else
    {
        #Clear the error message
        $errorprovider1.SetError($textboxName, "");
    }
}

 

Next: Part 3 – Other Validating Techniques

 

 
[Google+]   [Facebook]   [LinkedIn]   [StumbleUpon]   [Digg]   [Reddit]   [Google Bookmark]  

Tags: , , , ,