19

I am having trouble using the StringLengthAttribute when validating my model using Entity Framework and ASP.NET MVC3.

My model is based on an Entity Framework entity which has a partial class that uses the MetadataType attribute to tell the MVC which type to use when searching for metadata. This is shown in the code below:

[MetadataType(typeof(PartMetadata))]
public partial class Part { }

class PartMetadata
{
    [DisplayName("Part number")]
    [Required(ErrorMessage="* Required")]
    [StringLength(50, MinimumLength = 3, ErrorMessage = "* Part numbers must be between 3 and 50 character in length.")]
    public string Number { get; set; }

    [StringLength(255, MinimumLength=3,
        ErrorMessage="* Part descriptions must be between 3 and 255 characters in length.")]
    public string Description { get; set; }

    [DisplayName("Drawing required?")]
    public bool DrawingRequired { get; set; }
}

The problem I am having is that the description field is not being validated properly. Using the code below my model is validated as OK and I am redirected to the Index page of my controller even when the description field is left blank.

if (ModelState.IsValid)
{
    return RedirectToAction("Index");
 }
 else
 {
     return View();
  }

If I add a RequiredAttribute to the description field then my model is classed as being in an invalid state and my form is reloaded showing the default error message of the required attribute. If I subsequently edit the description field then it shows the validation error message I have set in the string length attribute.

Is this how the attribute should behave? It isn't a problem decorating the properties with the required attribute but seems counterintuitive as the field isn't required I just want to ensure that if the user does type something then it falls within the range of the string length attribute.

Benjamin Gale
  • 12,977
  • 6
  • 62
  • 100
  • 1
    As I understand, this is exactly working as you expect it to. It is just validating the string length ***if** the user does type something*. If you *really* want him/her to type something, [Required] is needed, just as you said. What's the behavior you are searching for? – Joao Oct 19 '11 at 12:04
  • 1
    Lefting the field blank is considered **null** and that's when [Required] attribute triggers. – Joao Oct 19 '11 at 12:07

4 Answers4

26

Yes, that is the correct behavior. StringLength verifies that a string is a certain length, but does not REQUIRE that the string be entered. Decorate Description with [Required], so that you have both a requirement for the string, and StringLength will provide the constraints on the string length.

counsellorben
  • 10,924
  • 3
  • 40
  • 38
  • 1
    Thanks. This actually makes perfect sense and it was the null string that was throwing me. I have verified that this works. Thank you. – Benjamin Gale Oct 19 '11 at 12:47
1

Keyword as follows:

 class PartMetadata
 {
     // required keyword forces user to enter input
     [Required] 
     [DisplayName("Part number")]
     // or [Required(ErrorMessage="* Required")]

     // ErrorMessage in string only enforces when data is entered
     [StringLength(50, MinimumLength = 3, ErrorMessage = "* Part numbers must be between 3 and 50 character in length.")]
     public string Number { get; set; }
Joseph Poirier
  • 386
  • 2
  • 17
  • 1
    Why doesn't [Required(ErrorMessage="* Required")] ensure that the field is Required and we need to also put a [Required] tag before it? – Spyros_Spy Feb 11 '19 at 08:01
  • 1
    I believe the top one just turns the DisplayName red if the data is missing & the latter one displays the error message. in theory you could print any error message U choose. The stringLength requirement, also fails to show up when the item is missing, even though there's a low length > 0. Initially, I thought that would do the trick, then went on trying various items. Also consider, these property items can & will differ sometimes between versions. – Joseph Poirier Feb 12 '19 at 04:39
  • 1
    I just did an example and it marked the second [Required] field that had the error message inside, as duplicate. – Spyros_Spy Feb 13 '19 at 09:15
  • 1
    Your version or update of your VS edition has the bug fix correcting the issue. I sometimes use beta's & report things that I feel are lacking common sense. I think VS 2012 did not throw the error, but at least one beta required both for some reason, which I thought was inefficient. I don't believe anything beyond 2013 requires both, only one or the other. – Joseph Poirier Feb 14 '19 at 16:19
  • 1
    Also consider the original question was MVC 3.0, we are now on MVC 4.0 or above in most cases – Joseph Poirier Feb 14 '19 at 16:27
0

MSDN: You can use composite formatting placeholders in the error message: {0} is the name of the property; {1} is the maximum length; and {2} is the minimum length. The placeholders correspond to arguments that are passed to the String.Format method at runtime.

So, what you can do

[StringLength(50, MinimumLength = 3, ErrorMessage = "Part {0} must be between {2} and {1} character(s) in length.")]
public string Number { get; set; }

Note - 1 is for Max, 2 - is for Min

^^^ This is also part of the answer for "How does the StringLengthAttribute work?"

T.S.
  • 18,195
  • 11
  • 58
  • 78
  • Can you explain why you're able to put ErrorMessage property in the constructor of StringLength? I am not able to find the reason behind this – Duke3e33 Oct 01 '22 at 10:25
  • 1
    @Duke3e33 https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/attributes/ Also see this https://stackoverflow.com/a/40061119/1704458 – T.S. Oct 03 '22 at 04:51
0

10 years passed from the marked answer, and this, StringLength validation attribute still doesn't work as other validation attributes do. The work around is this (for fixed string length validation). Change the minimum value for "range" validation. When you set a value through StringLength constructor (e. g. 36), this will set the maximum value, and there is no need to set an error message for validating the maximum value. This error message will never shown, because you can't add more than your entered number (e.g. 36). On the other hand; when you set the minimum value, then the error message will tell you so...

[Required(ErrorMessage = "Please enter an account Id")]
[MinLength(36, ErrorMessage = "The account Id must be {1} characters long.")]
[StringLength(36)]
[Display(Name = "Account Id")]
public string AccountId { get; set; }
Sam Saarian
  • 992
  • 10
  • 13