Placeholders doesn't work in ValidationFailure message

Viewed 676

I'm trying to make placeholders work with my ValidationFailure. The below example is a simplified version of what's going on.

public IActionResult Get()
{
    var errors = new List<ValidationFailure>();
    errors.Add(new ValidationFailure("PropertyName", "{PropertyValue} is not a valid value.", 42));

    var result = new ValidationResult(errors);
    result.AddToModelState(ModelState, null);

    return BadRequest(ModelState);
}

Actual result:

{
    "PropertyName": [
        "{PropertyValue} is not a valid value."
    ]
}

Expected result:

{
    "PropertyName": [
        "42 is not a valid value."
    ]
}

I can see these methods on the ValidationFailure object. So I'm thinking I just need to send it to the right service to make the magic happen :)

/// <summary>
/// Gets or sets the formatted message arguments.
/// These are values for custom formatted message in validator resource files
/// Same formatted message can be reused in UI and with same number of format placeholders
/// Like "Value {0} that you entered should be {1}"
/// </summary>
public object[] FormattedMessageArguments { get; set; }

/// <summary>
/// Gets or sets the formatted message placeholder values.
/// </summary>
public Dictionary<string, object> FormattedMessagePlaceholderValues { get; set; }

source

Any suggestions?

0 Answers
Related