Email Common Validation Rule

Email Common Validation Rule

Old forum URL: forums.lhotka.net/forums/t/2565.aspx


gdk9am posted on Tuesday, March 20, 2007

If I have a BO with

ValidationRules.AddRule(CommonRules.RegExMatch, new CommonRules.RegExRuleArgs("ContactEmail", CommonRules.RegExPatterns.Email));

in it

It makes it impossible to save a record without an email address.

A blank email address is still valid.

Has anyone come across this before and do they have a solution ?

RockfordLhotka replied on Wednesday, March 21, 2007

I'm sure you could create a regular expression to allow that by taking the Email one in CommonRules and adding an option that says an empty string is also valid.

Or you could use rule short-circuiting, though that could be fragile.

Or you could create a custom rule that encapsulates the concept. Somewhat like:

  Private Shared Function CheckEmail(Of T As OrderInfo)( _
    ByVal target As T, ByVal e As Csla.Validation.RuleArgs) As Boolean

    If String.IsNullOrEmpty(target._customer) Then
      Return True

    Else
      Dim emailRule As New Csla.Validation.RuleHandler( _
        AddressOf Csla.Validation.CommonRules.RegExMatch)
      Dim args As New Csla.Validation.CommonRules.RegExRuleArgs( _
         e.PropertyName, Validation.CommonRules.RegExPatterns.Email)
      Dim result As Boolean = emailRule.Invoke(target, args)
      If Not result Then
        e.Description = args.Description
        e.Severity = args.Severity
        e.StopProcessing = args.StopProcessing
      End If
      Return result
    End If

  End Function

 

Bowman74 replied on Wednesday, March 21, 2007

Whoops misread that one.  I'd do a custom validation rule as Rocky indicated.

Thanks,

Kevin

Copyright (c) Marimer LLC