views:

11

answers:

0

How to put a resource (multiple languages in client) message into a CustomValidation?

Example: using any of ValidationAttribute, let'say StringLength, I simple use ErrorMessageResourceName and ErrorMessageResourceType, but it did not work for CustomValidation.

Simple Code:


public class UserInfo
{
 [Required]
 public string Name { get; set; }

 [Required]
 [RegularExpression("^.*[^a-zA-Z0-9]*$",
  ErrorMessageResourceName = "ValidationErrorBadPasswordStrength",
  ErrorMessageResourceType = typeof(Resources.ValidationErrorResources))
 ]
 public string Pass { get; set; }

 [Required]
 public string OldPass { get; set; }
}



public static class ValidateUserInfo
{
 public static ValidationResult ValidateOldPassword(UserInfo input, ValidationContext context)
 {
  // I compared passwords from database, but used
  // many code lines that is not the purpouse now
  if (input.OldPass == "123")
  {
   return ValidationResult.Success;
  }
  return new ValidationResult("BadPass", new string[] { "OldPass" });
 }
}



[CustomValidation(
 typeof(ValidateUserInfo),
 "ValidateOldPassword",
 ErrorMessage = null,
 ErrorMessageResourceName = "ValidationErrorPasswordMismatch",
 ErrorMessageResourceType = typeof(Resources.ValidationErrorResources))
]
public void UpdateUser(UserInfo input)
{
 ...
}

The "bad password strength" works fine with multi-language resources, but instead of "ValidationErrorPasswordMismatch", the "BadPass" is shown.

Yes, I tried to use "Resources.ValidationErrorResources.ValidationErrorPasswordMismatch" directly in the ValidationResult, but then only neutral language resource is used.

Ideas?

Thanks, Willian