1

When attempting to use a property in the WithMessage part of a fluent validation rule, the string property isn't used, and instead it just outputs true. I have used validation in other areas of the application using collections (which is less straight-forward), and I could perform this task without issues. The only difference here is that it's a single object with a base class.

Here is my validator:

public class MultiCulturalControlValidator : AbstractValidator<TitleMultiCulturalControlProperty>
{
    public MultiCulturalControlValidator()
    {
        RuleFor(x => x.EnglishValue).NotEmpty().WithMessage("test error {0}",  x => x.DisplayName);
    }
}

My viewmodel, with all the irrelevant properties stripped out:

[Validator(typeof(MultiCulturalControlValidator))]
[DataContract]
public class TitleMultiCulturalControlProperty : MultiCulturalControlProperty
{
    public TitleMultiCulturalControlProperty()
    {
    }

    /// <summary>
    /// Gets or sets the name of these culture table values.
    /// </summary>
    [DataMember]
    public string DisplayName { get; set; }

    /// <summary>
    /// Gets or sets the required english value.
    /// </summary>
    // ReSharper disable once LocalizableElement
    [Display(Name = "English")]
    [StringLength(255)]
    [DataMember]
    public override string EnglishValue { get; set; }
}

As you can see, the required English value is overridden. Is that the issue? The rule still runs correctly, though, and it's just the message that isn't correct.

The message that displays when the rule doesn't pass:

"test error true"

'true' should be the DisplayName string. I checked, and the name isn't null/empty when the data is posted. I've checked all over for help and I couldn't find anything covering this issue.

Thanks

6
  • Are you trying to access the value of the property string DisplayName or the value in the [Display] attribute? Commented Dec 10, 2014 at 0:40
  • The value of the property. Commented Dec 10, 2014 at 0:42
  • I'm not familiar with FluentValidation, but should it be .WithMessage(String.Format("test error {0}", x => x.DisplayName)); Commented Dec 10, 2014 at 0:44
  • Cannot use format with the form (string, lambda). No dice. Commented Dec 10, 2014 at 7:12
  • According to this documentation, what your doing should work Commented Dec 10, 2014 at 7:20

2 Answers 2

2

I know this is ancient, but I just found this question while searching for the solution to similar problem. Here is the syntax I ended up using (reworked for the original question).

public class MultiCulturalControlValidator : AbstractValidator<TitleMultiCulturalControlProperty>
{
    public MultiCulturalControlValidator()
    {
         RuleFor(x => x.EnglishValue).NotEmpty().WithMessage(x => string.Format("test error {0}",  x.DisplayName));
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Way too late of an answer, but I wonder if the fact you are inheriting from "MultiCulturalControlProperty" was the issue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.