Bug 2076 - StringLengthAttribute behaviour insconsistent with Microsoft.NET
Summary: StringLengthAttribute behaviour insconsistent with Microsoft.NET
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: System ()
Version: 2.10.x
Hardware: All All
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2011-11-17 01:26 UTC by Eddy Zavaleta
Modified: 2012-03-12 23:04 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Proposed patch (870 bytes, application/octet-stream)
2011-11-17 01:26 UTC, Eddy Zavaleta
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Eddy Zavaleta 2011-11-17 01:26:45 UTC
Created attachment 901 [details]
Proposed patch

I have found that mono's StringLengthAttribute class (System.ComponentModel.DataAnnotations) have an inconsistency for localized error messages. In MS .NET the formatting function (FormatErrorMessage) supports 3 indexed placeholders (field name, maximum length, minimum length) for error message string and mono only supports the first two.


Steps to reproduce the problem:
1. Create an asp.net app with a model using StringLength validation.
2. Use "{2}" inside the ErrorMessage string.
3. Test the validation.

Code snippet:
public class TestModel
{
    public TestModel()
    {
    }

    [StringLength(10, MinimumLength = 3, ErrorMessage = "{0} must be between {2} and {1} characters.")]
    public string TestProperty { get; set; }
}

Actual Results:
System.FormatException: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

Expected Results:
Correct formatted message: "TestProperty must be between 3 and 10 characters."

I have attach a patch that fix the problem.
Comment 1 Anton Heryanto 2011-12-05 05:44:52 UTC
voted this bug to fixed
Comment 2 Miguel de Icaza [MSFT] 2012-03-12 23:04:19 UTC
This is supported if you are using the 4.0 API