Partilhar via


BaseFieldControl.Validate Method

When overridden in a derived class, verifies that the value of Value meets all restrictions on field content such as length, format, and data type.

Namespace:  Microsoft.SharePoint.WebControls
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: No

Syntax

'Declaration
Public Overridable Sub Validate
'Usage
Dim instance As BaseFieldControl

instance.Validate()
public virtual void Validate()

Implements

IValidator.Validate()

Remarks

There is no default implementation for Validate.

Notes to Inheritors

You should consider implementing this method in your derived classes. It could be used, for example, check Required property and verify that the field has a value whenever Required is true. See also the CreateChildControls property and Custom Field Data Validation for more about validation.

Value is the value of the control in the UI, not the underlying value (which is ItemFieldValue) of the SPField object that has the BaseFieldControl as its FieldRenderingControl property. Use Validate only when you need validation of the UI value. To validate ItemFieldValue, use the GetValidatedString().

An override of Validate should set the IsValid property to false and set ErrorMessage to an appropriate message.

Examples

The following is an example of an override of the Validate method. It first checks to see if the current control mode is Display, in which case it does not matter if the field is invalid because it cannot be changed anyway. The method also looks to see if the Value property is already known to be invalid by checking the IsValid property. If either of those checks is true, it does nothing. If neither is true, the method calls the base Validate which will set IsValid to false if it finds anything wrong with the Value property and set an appropriate ErrorMessage. (Since the BaseFieldControl.Validate method does nothing, if the class whose Validate method is being overridden is derived directly from BaseFieldControl, the call to the base Validate method can be omitted.) Finally, the method checks the value of Required and enforces that value.

public override void Validate()
{
    if (ControlMode == SPControlMode.Display || !IsValid)
    {
        return;
    }

    base.Validate();

    if (Field.Required && 
        (Value == null || Value.ToString().Length == 0))
    {
        this.ErrorMessage = Field.Title + " must have a value."
        IsValid = false;
        return;
    }
}
Public Overrides Sub Validate()
    If ControlMode = SPControlMode.Display OrElse (Not IsValid) Then
        Return
    End If

    MyBase.Validate()

    If Field.Required AndAlso (Value Is Nothing OrElse Value.ToString().Length = 0) Then
        Me.ErrorMessage = Field.Title & " must have a value."
        IsValid = False
        Return
    End If
End Sub

See Also

Reference

BaseFieldControl Class

BaseFieldControl Members

Microsoft.SharePoint.WebControls Namespace

RenderValidationMessage

IsValid

ErrorMessage

Other Resources

How to: Create a Custom Field Type

Custom Field Types

Walkthrough: Creating a Custom Field Type

Custom Field Data Validation