{%Pragmatic Coding%} Tips and tricks for software developers.

4Feb/120

Validation Controls in SharePoint

Recently I had to compare 2 E-mail fields in the form with SharePoint field controls. And this article will describe the usage of SharePoint validation controls.

Introduction

The Microsoft.SharePoint.WebControls namespace of the Microsoft.SharePoint.dll contains a number of controls such as validation controls that can be used on any application pages to validate user entry in the SharePoint controls.
To use SharePoint control all your classes in project namespace require that a page directive like the following be included in the ASPX page containing the control:

You can obtain the PublicKeyToken value for the current Windows SharePoint Services deployment from the default.aspx file in the %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\SiteTemplates\STS folder, or from information provided for the Microsoft.SharePoint assembly atLocal_Drive:\%WINDOWS%\assembly in Windows Explorer.

Today we'll review following SharePoint validation controls:

By the way, all validator controls are inherited from the standard ASP.NET Validation Controls.
Let's start our review.

The InputFormCompareValidator control

Properties of the control:
  • ControlToValidate: This property is required and must contain the id of the control to which this validator control applies.
  • ControlToCompare: This property is required and must contain the id of the control against which the first control must be validated.
  • Type: indicates the type of data to validate. If this property is omitted, the default is String.
  • Operator:  This property specifies the comparison operation. Possible values are Equal, NotEqual, GreaterThan, GreaterThanEqual, LessThan, LessThanEqual, DataTypeCheck.
  • ErrorMessage: This property contains the error message that will be displayed when the validation fails.
  • SetFocusOnError: When this property is set to true, the focus is set on the control that caused the validation to fail.
  • EnableClientScript: Setting this property to false will bypass client-side validation.

Plus here is a few important properties of the InputFormCompareValidator control:

  • Display: This property influences the postion of the subsequent controls. If set to Static, the necessary space for the error message will be foreseen. Possible values are None, Dynamic and Static.
  • ErrorImageUrl: If this property is filled out, the image will be displayed together with the error message in case a validation error occurs.
  • BreakAfter: When this property is set to true, a linke break is inserted after the error message.
  • BreakBefore: When this property is set to true, a line break is inserted before the error message.

The InputFormRequiredFieldValidator control

Properties of the control:
  • ControlToValidate: This property is required and must contain the id of the control to which this validator control applies.
  • ErrorMessage: This property contains the error message that will be displayed when the validation fails.
  • SetFocusOnError: When this property is set to true, the focus is set on the control that caused the validation to fail.
  • EnableClientScript: Setting this property to false will bypass client-side validation.

Important properties of the SharePoint InputFormRequiredFieldValidator control:

  • Display: This property influences the postion of the subsequent controls. If set to Static, the necessary space for the error message will be foreseen. Possible values are None, Dynamic and Static.
  • ErrorImageUrl: If this property is filled out, the image will be displayed together with the error message in case a validation error occurs.
  • BreakAfter: When this property is set to true, a linke break is inserted after the error message.
  • BreakBefore: When this property is set to true, a line break is inserted before the error message.

The InputFormRangeValidator control

Properties of the control:
  • ControlToValidate: This property is required and must contain the id of the control to which this validator control applies.
  • Type: indicates the type of data to validate. If this property is omitted, the default is String.
  • MinimumValue: the lower boundary. The minimum value itself is considered as a valid entry.
  • MaximumValue: the upper boundary. The maximum value itself is considered as a valid entry.
  • ErrorMessage: This property contains the error message that will be displayed when the validation fails.
  • SetFocusOnError: When this property is set to true, the focus is set on the control that caused the validation to fail.
  • EnableClientScript: Setting this property to false will bypass client-side validation.

Plus here is a few important properties of the InputFormRangeValidator control:

  • Display: This property influences the postion of the subsequent controls. If set to Static, the necessary space for the error message will be foreseen. Possible values are None, Dynamic and Static.
  • ErrorImageUrl: If this property is filled out, the image will be displayed together with the error message in case a validation error occurs.
  • BreakAfter: When this property is set to true, a linke break is inserted after the error message.
  • BreakBefore: When this property is set to true, a line break is inserted before the error message.

The InputFormRegularExpressionValidator control

Properties of the control:
  • ControlToValidate: This property is required and must contain the id of the control to which this validator control applies.
  • ValidationExpression: contains the regular expression against which the user entry must be validated.
  • ErrorMessage: This property contains the error message that will be displayed when the validation fails.
  • SetFocusOnError: When this property is set to true, the focus is set on the control that caused the validation to fail.
  • EnableClientScript: Setting this property to false will bypass client-side validation.

Plus here is a few important properties of the InputFormRegularExpressionValidator control:

  • Display: This property influences the postion of the subsequent controls. If set to Static, the necessary space for the error message will be foreseen. Possible values are None, Dynamic and Static.
  • ErrorImageUrl: If this property is filled out, the image will be displayed together with the error message in case a validation error occurs.
  • BreakAfter: When this property is set to true, a linke break is inserted after the error message.
  • BreakBefore: When this property is set to true, a line break is inserted before the error message.

The InputFormCheckBoxListValidator control

This validator control can only be used against an InputFormCheckBoxList control (and not against an InputFormCheckBox control). It works as a required field validator: if no options are selected when clicking the OK button, the specified error message will appear.

If you try to use it in combination with a InputFormCheckBox control, you will get an unclear error message (not informing you about the real problem) but when looking with Reflector into the code, it learns you that the control referenced in the ControlToValidate property is casted to an InputFormCheckBoxList control.

The InputFormCustomValidator control

If one of other validation controls does not suit your needs, you can define a custom validation function. You have the choice to define a client-side validation function or a server-side validation function, or combine both.
An example of server-side validation:

In your code-behind you define the controls as protected class-level variables:

And you add the server-side validation method. This method accepts 2 incoming arguments: a source object being the control to validate, and a args object having properties as Value and IsValid. The Value property contains the value to validate. Set the IsValid property to true if the validation is successful or set the IsValid property to false if the validation fails.

Properties of the control:
  • ControlToValidate: This property is required and must contain the id of the control to which this validator control applies.
  • ClientValidationFunction: contains the name of the javascript function that needs to be executed.
  • OnServerValidate: contains the name of the server-side function that needs to be executed.
  • ErrorMessage: This property contains the error message that will be displayed when the validation fails.
  • SetFocusOnError: When this property is set to true, the focus is set on the control that caused the validation to fail.
  • EnableClientScript: Setting this property to false will bypass client-side validation.

A lot of people combine a custom validator control with a RequiredFieldValidator but this is not necessary because the ASP.NET CustomValidator control (and thus also the InputFormCustomValidator control) has a ValidateEmptyText property which can be set to true or false. If set to false, a blank entry is validated as a valid entry. If set to true, a blank entry will be considered as erroneous, thus excluding the need for a RequiredFieldValidator control.
If you want to use client-side validation, you have to set the ClientValidationFunction property. The value must be the name of a javascript function. See an example:

The javascript function must reside in the < asp:Content > placeholder with ContentPlaceHolderID PlaceHolderMain (where most of your controls reside). The function needs 2 arguments: a source object, being the control that needs validation; and an arguments object having properties like Value and IsValid. If you want the validation to succeed, you have to set args.IsValid to true; if you want the validation to fail, you have to set args.IsValid to false.

Important properties of the InputFormCustomValidator control:

  • Display: This property influences the postion of the subsequent controls. If set to Static, the necessary space for the error message will be foreseen. Possible values are None, Dynamic and Static.
  • ErrorImageUrl: If this property is filled out, the image will be displayed together with the error message in case a validation error occurs.
  • BreakAfter: When this property is set to true, a linke break is inserted after the error message.
  • BreakBefore: When this property is set to true, a line break is inserted before the error message.

ValidationSummary control

You can combine any SharePoint validation control with the ASP.NET ValidationSummary control. There is no counterpart for it in SharePoint.

When you use no ValidationSummary control, all error messages appear next to or right under the control that fails validation. A ValidationSummary control presents the user with a list of validation errors at the bottom of the page.
You add a ValidationSummary control to your application page or web part as follows:

The different properties on the ValidationSummary control are:
  • DisplayMode: this property defines the appearance of the summary. Possible values are List, BulletList and SingleParagraph.
  • HeaderText: set this property if you want to display a header text at the top of the summary.
  • ShowSummary: set this property to show the error summary on the page. In that case the ShowMessageBox property must be set to false.
  • ShowMessageBox: this property indicates whether the validation summary is displayed in a message box or on the page. In that case also theEnableClientScript property should be set to true.

Normally you should also be able to set the Text property of the SharePoint validation controls to f.e. * to display an asterisk next to the control that failed validation but after a look inside the Microsoft.SharePoint.dll with Reflector it show that the Text property is overridden and looses its original meaning.

Share on social network

Share to Facebook
Share to Google Plus
Share to LiveJournal
Share to MyWorld
Share to Odnoklassniki
Share to Yandex
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

No trackbacks yet.