Enterprise Library 4.0 ValidationProvider Self Validation question
I am using the Validationprovider control for the 1st time. I've had limited success trying to mimic the functionality t hat I saw in the ValidationQuickStart solution example that comes with the Ent Lib installation.
While I was able to get form errorprovider icons to appear based on Business Object validations that decorate the BO property with validation attribute such as:
<NotNullValidator(MessageTemplate:="Gender must be specified.", RuleSet:="MyRule", Tag:="Gender")> _
Public Shadows Property Gender() As String
Get
Return MyBase.Gender
End Get
Set(ByVal value As String)
MyBase.Gender = value
End Set
End Property
However, if I wanted to add an edit to the "SelfValidation" method, how would I tie the Result to a form control?
(In Self Validation)
If Me.Attornet s Nothing Then
Dim result As New ValidationResult("Attorney selection is required", Me, Nothing, Nothing, Nothing)
results.AddResult(result)
End If
Or must I create by own **CUSTOM VALIDATOR?" and then use its type to decorate a Business Object property?
Edit:
If The ValidationProvider control is not intended for cross-control validation like this one:
if the user selects the "Yes" option in the radio button group corresponding to Business Option property Customer.DoYouSmokeC开发者_运维技巧rack As Boolean then the txtHowMuch control should be flagged with an ErrorProvider icon indicating that this field is conditionally required.
The way I am doing this now is to do this in a Self Validation block and, when I add a ValidationResult object to the collection, I set the Key property to "Explanation" so the GUI can use it to manually map to the offending screen control.
Is there a more standard/beter way to do this?
You can't use self validation (or any other cross property validation) using the ValidationProvider
. It won't work, because the ValidationProvider
works without the existence of an business object. What it does is loading all validations defined for a single property and check the value of the given control using those validations.
Cross property validations such as what custom validators and self validation do need a completely initialized business object to work.
If the validation is simple and validates a single property (without doing anything else as going to the database etc), you can write a custom validation attribute to do the validation. In all other cases, what you should do is create the business object after the user hit "Save" (what you will probably be doing anyway) and validate the object at that point. In other words, you delay the validation till the user commits his changes.
精彩评论