Skip to content

Instantly share code, notes, and snippets.

@JeremySkinner
Created December 21, 2010 11:48
Show Gist options
  • Select an option

  • Save JeremySkinner/749837 to your computer and use it in GitHub Desktop.

Select an option

Save JeremySkinner/749837 to your computer and use it in GitHub Desktop.
// Should allow for this syntax:
// RuleFor(x => x.Surname).NotNull().WithLocalizedMessage(() => MyResources.SomeResource, x => "foo", x => "bar");
// ...where MyResources.SomeResource has format placeholders
public static IRuleBuilderOptions<T, TProperty> WithLocalizedMessage<T, TProperty>(this IRuleBuilderOptions<T, TProperty> rule, Expression<Func<string>> resourceSelector, params Func<T, object>[] funcs) {
return rule.Configure(config => {
config.CurrentValidator.ErrorMessageSource = LocalizedStringSource.CreateFromExpression(resourceSelector, new StaticResourceAccessorBuilder());
funcs
.Select(func => new Func<object, object>(x => func((T)x)))
.ForEach(config.CurrentValidator.CustomMessageFormatArguments.Add);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment