How to Validate Forms with ASP.NET MVC 2 Data Annotations: Listing 5.

WithinDaysValidator Class to Define the Custom Client Validation Rule

Public Class WithinDaysValidator
    Inherits DataAnnotationsModelValidator(Of WithinDaysAttribute)
    Private _priorDays As Integer
    Private _subsequentDays As Integer
    Private _message As String

    Public Sub New(ByVal metadata As ModelMetadata,
                   ByVal context As ControllerContext,
                   ByVal attribute As WithinDaysAttribute)

        MyBase.New(metadata, context, attribute)

        _message = attribute.ErrorMessage
        _priorDays = attribute.PriorDays
        _subsequentDays = attribute.SubsequentDays
    End Sub

    Public Overrides Function GetClientValidationRules() _
      As IEnumerable(Of System.Web.Mvc.ModelClientValidationRule)

        Dim Rule As New ModelClientValidationRule _
        With {.ErrorMessage = _message, .ValidationType = 
          "withindays"}

        Rule.ValidationParameters.Add("prior", _priorDays)
        Rule.ValidationParameters.Add("subsequent", _subsequentDays)

        Dim ModelClientValidationRule() = {Rule}
        Return ModelClientValidationRule
    End Function
End Class
comments powered by Disqus

Featured

  • Black White Wave IMage

    Neural Regression Using PyTorch: Training

    The goal of a regression problem is to predict a single numeric value, for example, predicting the annual revenue of a new restaurant based on variables such as menu prices, number of tables, location and so on.

  • Microsoft Ships Visual Studio 2019 v16.9 Servicing Baseline Release

    Microsoft is urging enterprises and professional coders to standardize on the new Visual Studio 2019 v16.9, a servicing baseline release that's guaranteed to receive official support for an extended period.

  • Microsoft Extends Low-Code Push with Power Fx Programming Language

    "Using formulas that are already familiar to hundreds of millions of users, Power Fx allows a broad range of people to bring skills they already know to low code solutions."

  • Microsoft's Tools to Fight Solorigate Attack Are Now Open Source

    Microsoft open sourced homegrown tools it used to check its systems for code related to the recent massive breach of supply chains that the company has named Solorigate.

Upcoming Events