.NET Framework Class Library for Silverlight
ValidationAttribute.IsValid Method
Determines whether the specified object is valid.
Namespace: System.ComponentModel.DataAnnotations
Assembly: System.ComponentModel.DataAnnotations (in System.ComponentModel.DataAnnotations.dll)
Syntax
Visual Basic (Declaration)
Protected Overridable Function IsValid ( _ value As Object, _ validationContext As ValidationContext _ ) As ValidationResult
C#
protected virtual ValidationResult IsValid( Object value, ValidationContext validationContext )
Parameters
- value
- Type: System.Object
The object to validate.
- validationContext
- Type: System.ComponentModel.DataAnnotations.ValidationContext
An object that contains information about the validation request.
Return Value
Type: System.ComponentModel.DataAnnotations.ValidationResulttrue if value is valid; otherwise, false.
Exceptions
| Exception | Condition |
|---|---|
| NotImplementedException |
The IsValid method is not overridden in the derived class. |
Remarks
You override the IsValid method to provide the logic for your customized validation check. The GetValidationResult and Validate methods call the IsValid method to determine if the value is valid.
Examples
The following example shows a customized validation attribute that derives from ValidationAttribute. The class overrides the IsValid method to test whether the value is valid. By default, the attribute displays an error message that is included as a resource in the application.
Visual Basic
Public Class EvenNumberAttribute Inherits ValidationAttribute Public Sub New() MyBase.New(Function() My.Resources.Resource1.EvenNumberError) End Sub Public Sub New(ByVal errorMessage As String) MyBase.New(Function() errorMessage) End Sub Protected Overrides Function IsValid(ByVal value As Object, _ ByVal validationContext As ValidationContext) As ValidationResult If (value = Nothing) Then Return ValidationResult.Success End If Dim convertedValue As Integer Try convertedValue = Convert.ToInt32(value) Catch ex As FormatException Return New ValidationResult(My.Resources.Resource1.ConversionError) End Try If (convertedValue Mod 2 = 0) Then Return ValidationResult.Success Else Return New ValidationResult(FormatErrorMessage(validationContext.DisplayName)) End If End Function End Class
C#
public class EvenNumberAttribute : ValidationAttribute { public EvenNumberAttribute() : base(() => Resource1.EvenNumberError) { } public EvenNumberAttribute(string errorMessage) : base(() => errorMessage) { } protected override ValidationResult IsValid(object value, ValidationContext validationContext) { if (value == null) { return ValidationResult.Success; } int convertedValue; try { convertedValue = Convert.ToInt32(value); } catch (FormatException) { return new ValidationResult(Resource1.ConversionError); } if (convertedValue % 2 == 0) { return ValidationResult.Success; } else { return new ValidationResult(FormatErrorMessage(validationContext.DisplayName)); } } }
Version Information
Silverlight
Supported in: 5, 4, 3Platforms
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.
See Also
Reference
Other Resources
Community Content
msollicito
Could not get this to work
What am I doing wrong?
I use this code exactly as is. I am compiling against .Net 4. It compiles fine. But when I get into the method, the validation context is null.
I have tried overriding a number of validation attributes and get the same thing every time - what am I doing wrong?
I use this code exactly as is. I am compiling against .Net 4. It compiles fine. But when I get into the method, the validation context is null.
I have tried overriding a number of validation attributes and get the same thing every time - what am I doing wrong?