Format BrokenRule.Description

Format BrokenRule.Description

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


Michael Hildner posted on Tuesday, September 19, 2006

I could swear this has been discussed lately, but I couldn't find.

Is there a recommended approach to formatting what the description is for a broken rule? For example, I have something like

ValidationRules.AddRule(CommonRules.StringRequired, "SerialNumber");

Of course the .Description is "SerialNumber required". I'd like to have it say "Serial Number is required" or similar.

Thanks,

Mike

P.S.

I recently subclassed BusinessBase and have a public get property that will return all my broken rules as a string. With data binding and all that, when I drag from the Data Sources pane, this property is automatically there - cool! Even better, when a broken rule is fixed, this text box is automagically updated - way cool! I think this is better than the ErrorProvider. Really neat UI stuff. I can see having a ListView or similar that has the rules, and a red X or green check mark depending on broken rule status. This way the user doesn't have to mouse over the error provider icon to see what the issue is. That's my end goal.

Henrik replied on Wednesday, September 20, 2006

Michael

I've subclassed the Validation.RuleArgs class into an ExtRuleArgs which has an overloaded constructor that can take a description string:

Public Class ExtRuleArgs

   Inherits RuleArgs

 

   Public Sub New(ByVal propertyName As String, ByVal description As String)

       MyBase.New(propertyName)

       MyBase.Description = description

   End Sub

End Class

 

I’ve also created my own ExtCommonRules module (which I’ve attached to this post), where I’ve rewritten the common rules, so they only return true or false and doesn’t set the description.

 

This allows me to pass my own description into the rule like this:
(These are the validation rules for an address)

 

Protected Overrides Sub AddBusinessRules()

   With ValidationRules

       'AddressTypeId (Byte)

       .AddRule(AddressOf ExtCommonRules.NotEmpty(Of Byte), _

New EmptyValueRuleArgs(Of Byte)("AddressTypeId", 0, _
My.Resources.AddressTypeRequired))

 

       'Street (AnsiString)

       .AddRule(AddressOf ExtCommonRules.StringRequired, _

       New ExtRuleArgs("Street", My.Resources.StreetRequired))

       .AddRule(AddressOf ExtCommonRules.StringMaxLength, _

       New ExtMaxLengthRuleArgs("Street", 180, My.Resources.StreetTooLong))

 

       'PostalCode (AnsiString)

       .AddRule(AddressOf ExtCommonRules.StringMaxLength, _

       New ExtMaxLengthRuleArgs("PostalCode", 11, My.Resources.PostalCodeTooLong))

 

       'City (AnsiString)

       .AddRule(AddressOf ExtCommonRules.StringMaxLength, _

       New ExtMaxLengthRuleArgs("City", 35, My.Resources.CityTooLong))

 

       'Area (AnsiString)

       .AddRule(AddressOf ExtCommonRules.StringMaxLength, _

       New ExtMaxLengthRuleArgs("Area", 50, My.Resources.AreaTooLong))

   End With

End Sub

 

The first rule (NotEmpty) is a generic rule, that can check whether a field of type T is empty. The second parameter of the EmptyValueRuleArgs defines what an empty value for this type is.

 

Hope this helps

/Henrik

 

Michael Hildner replied on Wednesday, September 20, 2006

Henrik,

Nice. Thank you for posting the code.

Regards,

Mike

JoeFallon1 replied on Wednesday, September 20, 2006

I made virtually the exact same extension (just a different class name). The Description is an Optional Parameter. If I do not pass in a Description then I do a lookup in a dictionary for the property name and return the extended description from that. Loading the dictionary only occurs once.

=============================================================

All of my rules use code like this:

    e.Description = GetDescription(target, propertyName, propertyDescription) & " the rest of the phrase for this rule."

=============================================================

Public Function GetDescription(ByVal target As Object, ByVal propertyName As String, ByVal propertyDescription As String) As String
      Dim result As String = ""

      If propertyDescription <> "" Then
        result = propertyDescription
      Else
        'since no propertyDescription was passed in, look up the default value 

        If TranslationList.ContainsKey(propertyName) Then
          result = TranslationList.Item(propertyName)
        Else
          result = propertyName
        End If
      End If

      Return result
  End Function

=============================================================

 Private mTranslationList As Generic.Dictionary(Of String, String)

=============================================================

 Private ReadOnly Property TranslationList() As Generic.Dictionary(Of String, String)
      Get
        If mTranslationList IsNot Nothing Then
          Return mTranslationList
        Else
          mTranslationList = New Generic.Dictionary(Of String, String)

          'load the list the first time it is asked for. Subsequent requests will return the fully loaded list.
          mTranslationList.Add("Acctcode", "Account code")
          mTranslationList.Add("Acctdesc", "Account description")

          'TODO: add more fields here in alphabetical order.

          'OR: create a database table and pull the values from there instead.
          'no re-compile required just to change the list.

          Return mTranslationList
        End If
      End Get
    End Property

 

Joe

William replied on Wednesday, October 11, 2006

Rocky,
 
Do you think it is a good idea to include this "standard" capability as part of the common rule library in CSLA?
 
Regards,
William

RockfordLhotka replied on Thursday, October 12, 2006

Yes, a solution, of some sort, to this issue is on my list-of-things-to-consider for future versions.

Obviously any solution I implement in CommonRules would need to preserve backward compatibility, and so that complicates things a bit. But it is solvable, and so this is a likely change for a future version.

Copyright (c) Marimer LLC