dotnet / dotnet/dotnet-api-docs

Composite string placeholders in validation attributes

Open
#2,047 2 comments 0 reactions 0 assignees View on GitHub
area-System.ComponentModel.DataAnnotations help wanted Pri3
Dominant language
C#
Stars
949
Forks
1.7k
Avg merge
3d 27m
Merged PRs (30d)
49

Description

Many validation attributes compose their error messages using `String.Format` with one or more arguments. You can use placeholders like {0}, {1}, {2}, etc. in the error message string passed to the attribute. For example:
```
[StringLength(4, ErrorMessage = "The {0} value cannot exceed {1} characters. ")]
```
If the property name is "Name" and the maximum length is 25, the error message would be "The Name value cannot exceed 25 characters."

The composite string capability has not been documented for most of the validation attributes. The first one (`StringLength`) has been done, and that PR #2011 can be used as a template to follow when doing the others.

To find out which parameters are passed to `String.Format` for a particular attribute's error message, see the [DataAnnotations source code](https://github.com/dotnet/corefx/tree/master/src/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations).

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.