Message Resources?

Topics: Validation Application Block
May 15, 2008 at 8:38 AM
Hi, I have just started to use the Validation Application Block (with ASP.NET), and was wondering how to configure the messages which are written in the event of validation failure.

In our web-application we have some backend classes which take care of all sorts of localisation tasks - like finding texts in different languages. Is it possible in the VAB to configure a custom class/method which should be used to deliver a message text to display in the event of validation failure?

I have seen the configuration items "messageTemplateResourceName" and "messageTemplateResourceType" - can these be used to identify a class and method which delivers the error text?

Thanks,
Peter
May 15, 2008 at 1:12 PM
Hi Peter,

The resource types and names are keys for the standard .NET resource lookup; there is no invocation through reflection, even if the resource name is a property in the resource type.

If you're willing to modify the source code, you can change the implementation for the GetMessageTemplate() method in ValidatorData and BaseValidatorAttribute so they invoke your resource manager.

Fernando


xdzgor wrote:
Hi, I have just started to use the Validation Application Block (with ASP.NET), and was wondering how to configure the messages which are written in the event of validation failure.

In our web-application we have some backend classes which take care of all sorts of localisation tasks - like finding texts in different languages. Is it possible in the VAB to configure a custom class/method which should be used to deliver a message text to display in the event of validation failure?

I have seen the configuration items "messageTemplateResourceName" and "messageTemplateResourceType" - can these be used to identify a class and method which delivers the error text?

Thanks,
Peter


May 16, 2008 at 8:49 AM

Hi - thanks for your reply and suggestion, but I don't really want to change the code in the enterprise-library, so I am looking for another solution. We don't use "resources" in our applications; most of the stuff we make uses some sort of content-management-system, and we utilise the localisation functions of these systems to deliver texts in different languages. I would really like to be able to specify in my config, or in my ascx, where the texts should be obtained, eg from a class/method (instantiated via reflection) - and then it is this class' problem to find the correct text to deliver.

Is this sort of thing not at all possible? Doesn't anyone else want this sort of functionality? Could a "custom validator" be used to solve this for me? I am not really sure how the error text is obtained - does it come from the StringLengthValidator for instance, or does something else deliver the text to the page?


Thanks,
Peter

 


fsimonazzi wrote:
Hi Peter,

The resource types and names are keys for the standard .NET resource lookup; there is no invocation through reflection, even if the resource name is a property in the resource type.

If you're willing to modify the source code, you can change the implementation for the GetMessageTemplate() method in ValidatorData and BaseValidatorAttribute so they invoke your resource manager.

Fernando


xdzgor wrote:
Hi, I have just started to use the Validation Application Block (with ASP.NET), and was wondering how to configure the messages which are written in the event of validation failure.

In our web-application we have some backend classes which take care of all sorts of localisation tasks - like finding texts in different languages. Is it possible in the VAB to configure a custom class/method which should be used to deliver a message text to display in the event of validation failure?

I have seen the configuration items "messageTemplateResourceName" and "messageTemplateResourceType" - can these be used to identify a class and method which delivers the error text?

Thanks,
Peter





May 16, 2008 at 2:22 PM
Hi,

Text for the different validators is determined by the creators of validators, usually attributes and configuration objects. There's logic in the GetMessageTemplate methods I mentioned below that will provide the literal message or a resource based one if configured. Validators get the messages when they are created and use them.

[Disclaimer: I haven't tried this and there might be unforeseen issues that make this approach impractical]
With the current version and since you don't want to update the code, you could try to use some kind of proxy validator that would use its messages as keys for lookups on your CMS, and override the proxied validator's messages with the result. Of course, this would only work with configuration-based validation because you can't wrap another attribute. If you want to give this a go here are a few tips:
  • The runtime aspect of config would be somewhat tricky: you want to have a subclass of validator data that nests another validator data instance, but you need to support the ability to nest any possible validator data subclass. This support is already available in EntLib for collections but not for nested references, so you would either have to implement this support for references (not trivial) or keep the configuration object for the proxied validator to be in a collection with a single element (somewhat hackish). The implementation for DoCreateValidator in this proxy config configuration object would ask the nested configuration object to create its validator and continue from there. You want to make sure serialization and deserialization work properly for this configuration object.
  • The design-time support will require some work. You can use the AndCompositeValidatorNode as an example. In this case however you'll need to add commands for all other validators so they can be added as children of the proxy node.

Regards,
Fernando



xdzgor wrote:

Hi - thanks for your reply and suggestion, but I don't really want to change the code in the enterprise-library, so I am looking for another solution. We don't use "resources" in our applications; most of the stuff we make uses some sort of content-management-system, and we utilise the localisation functions of these systems to deliver texts in different languages. I would really like to be able to specify in my config, or in my ascx, where the texts should be obtained, eg from a class/method (instantiated via reflection) - and then it is this class' problem to find the correct text to deliver.

Is this sort of thing not at all possible? Doesn't anyone else want this sort of functionality? Could a "custom validator" be used to solve this for me? I am not really sure how the error text is obtained - does it come from the StringLengthValidator for instance, or does something else deliver the text to the page?


Thanks,
Peter

 


fsimonazzi wrote:
Hi Peter,

The resource types and names are keys for the standard .NET resource lookup; there is no invocation through reflection, even if the resource name is a property in the resource type.

If you're willing to modify the source code, you can change the implementation for the GetMessageTemplate() method in ValidatorData and BaseValidatorAttribute so they invoke your resource manager.

Fernando


xdzgor wrote:
Hi, I have just started to use the Validation Application Block (with ASP.NET), and was wondering how to configure the messages which are written in the event of validation failure.

In our web-application we have some backend classes which take care of all sorts of localisation tasks - like finding texts in different languages. Is it possible in the VAB to configure a custom class/method which should be used to deliver a message text to display in the event of validation failure?

I have seen the configuration items "messageTemplateResourceName" and "messageTemplateResourceType" - can these be used to identify a class and method which delivers the error text?

Thanks,
Peter








May 19, 2008 at 8:03 AM
Hi
thanks for all your help.
I ended up writing my own "custom validator" - basically a severely cut-down version of the StringLengthValidator, where I override the GetMessage method to obtain the texts from my CMS.
This works fine for me at the moment, as I only need a string-length validator for my current project.

Thanks,
Peter



fsimonazzi wrote:
Hi,

Text for the different validators is determined by the creators of validators, usually attributes and configuration objects. There's logic in the GetMessageTemplate methods I mentioned below that will provide the literal message or a resource based one if configured. Validators get the messages when they are created and use them.

[Disclaimer: I haven't tried this and there might be unforeseen issues that make this approach impractical]
With the current version and since you don't want to update the code, you could try to use some kind of proxy validator that would use its messages as keys for lookups on your CMS, and override the proxied validator's messages with the result. Of course, this would only work with configuration-based validation because you can't wrap another attribute. If you want to give this a go here are a few tips:
  • The runtime aspect of config would be somewhat tricky: you want to have a subclass of validator data that nests another validator data instance, but you need to support the ability to nest any possible validator data subclass. This support is already available in EntLib for collections but not for nested references, so you would either have to implement this support for references (not trivial) or keep the configuration object for the proxied validator to be in a collection with a single element (somewhat hackish). The implementation for DoCreateValidator in this proxy config configuration object would ask the nested configuration object to create its validator and continue from there. You want to make sure serialization and deserialization work properly for this configuration object.
  • The design-time support will require some work. You can use the AndCompositeValidatorNode as an example. In this case however you'll need to add commands for all other validators so they can be added as children of the proxy node.

Regards,
Fernando