How do I combine resource strings for validation attribute error messages?

Sinaesthetic

If I were to have error messages on a validation attribute like:

  • First Name is required
  • Last Name is required

and then a validation attribute like this:

[Required(ErrorMessageResourceName = "Error_FirstNameRequired", ErrorMessageResourceType = typeof(Strings)]
public string FirstName {get;set;}

I don't want to have a translation done for every instance of this. Is there a way to combine resource strings in a formatter, for example:

[Required(ErrorMessage = string.Format("{0} {1}", Strings.Label_FirstName, Strings.Error_IsRequired))]
public string FirstName {get;set;}

Of course this doesn't work because it needs to be a compile time constant. But is there away to achieve this so that I can build localized strings and reuse those that already exist? I thought of just creating custom attributes that allow for extra properties to be set and override the default output message, but that would be way too much refactoring and kind of kludgy imo.

Any ideas?

Silvermind

You can use formatting in the strings defined in your resources. When you use {0}, as shown in FieldRequired, the display name will be inserted when possible. Otherwise it will fall back to the property name as shown for MiddleName.

Example:

Resources:

Strings.resx
String.resx

Strings.nl.resx
Strings.nl.resx

Implementation:

public class MyClass
{

    [Display(ResourceType = typeof(Strings), Name = "FirstName")]
    [Required(ErrorMessageResourceName = "FieldRequired",
              ErrorMessageResourceType = typeof(Strings))]
    public string FirstName { get; set; }

    [Required(ErrorMessageResourceName = "FieldRequired",
              ErrorMessageResourceType = typeof(Strings))]
    public string MiddleName { get; set; }

    [Display(ResourceType = typeof(Strings), Name = "LastName")]
    [Required(ErrorMessageResourceName = "FieldRequired",
              ErrorMessageResourceType = typeof(Strings))]
    public string LastName { get; set; }

    // Validation errors for culture [en] would be:
    //             "First name is a required field."
    //             "MiddleName is a required field."
    //             "Last name is a required field."
    //
    // Validation errors for culture [nl] would be:
    //             "Voornaam is een benodigd veld."
    //             "MiddleName is een benodigd veld."
    //             "Achternaam is een benodigd veld."
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I enforce the order which error messages are displayed when using validation annotations?

From Dev

How do I determine the password attribute value for the Chef "user" resource?

From Dev

How do I use Unity IoC in an MVC Validation Attribute?

From Dev

How do I fix this rstrip attribute error?

From Dev

How do I create a custom validation directive (i.e. attribute for validation) in Angular 2.0?

From Dev

How to use different error codes for validation messages?

From Dev

Java - How to setup validation with different error messages

From Dev

How do I suppress error messages on file.delete?

From Dev

How do I catch exceptions that have specific error messages in Python?

From Dev

How do I deal with placeholders for Win32 error messages?

From Dev

How do I copy error messages in Visual Studio

From Dev

How do I see error messages generated in PowerShell event handlers?

From Dev

How do I interpret PostgreSQL error messages from within Go?

From Dev

How do I deal with these error messages in Processing using the UnfoldingMaps library

From Dev

Rails 5 throw abort : how do I setup error messages?

From Dev

How do I test Moose subtype error messages?

From Dev

How do I suppress console/cmd error messages in python

From Dev

How do I display the error messages in signup form?

From Dev

How do I ignore warnings, but keep error messages from 'find'

From Dev

How do I suppress error messages from cp?

From Dev

How do I debug Resource #6 error in MSSQL?

From Dev

Laravel 5.1 How to translate attribute values in the form validation messages?

From Dev

Laravel 5.1 How to translate attribute values in the form validation messages?

From Dev

How do I Intentionally cause a database validation error on insert

From Dev

How to customize validation attribute error message?

From Dev

How do I combine COUNTIF with OR

From Dev

How do I combine COUNTIF with OR

From Java

How do I use Join-Path to combine more than two strings into a file path?

From Dev

Error messages in nested attribute

Related Related

  1. 1

    How do I enforce the order which error messages are displayed when using validation annotations?

  2. 2

    How do I determine the password attribute value for the Chef "user" resource?

  3. 3

    How do I use Unity IoC in an MVC Validation Attribute?

  4. 4

    How do I fix this rstrip attribute error?

  5. 5

    How do I create a custom validation directive (i.e. attribute for validation) in Angular 2.0?

  6. 6

    How to use different error codes for validation messages?

  7. 7

    Java - How to setup validation with different error messages

  8. 8

    How do I suppress error messages on file.delete?

  9. 9

    How do I catch exceptions that have specific error messages in Python?

  10. 10

    How do I deal with placeholders for Win32 error messages?

  11. 11

    How do I copy error messages in Visual Studio

  12. 12

    How do I see error messages generated in PowerShell event handlers?

  13. 13

    How do I interpret PostgreSQL error messages from within Go?

  14. 14

    How do I deal with these error messages in Processing using the UnfoldingMaps library

  15. 15

    Rails 5 throw abort : how do I setup error messages?

  16. 16

    How do I test Moose subtype error messages?

  17. 17

    How do I suppress console/cmd error messages in python

  18. 18

    How do I display the error messages in signup form?

  19. 19

    How do I ignore warnings, but keep error messages from 'find'

  20. 20

    How do I suppress error messages from cp?

  21. 21

    How do I debug Resource #6 error in MSSQL?

  22. 22

    Laravel 5.1 How to translate attribute values in the form validation messages?

  23. 23

    Laravel 5.1 How to translate attribute values in the form validation messages?

  24. 24

    How do I Intentionally cause a database validation error on insert

  25. 25

    How to customize validation attribute error message?

  26. 26

    How do I combine COUNTIF with OR

  27. 27

    How do I combine COUNTIF with OR

  28. 28

    How do I use Join-Path to combine more than two strings into a file path?

  29. 29

    Error messages in nested attribute

HotTag

Archive