18
votes

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.

4
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
Lefting the field blank is considered null and that's when [Required] attribute triggers. - Joao

4 Answers

24
votes

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.

1
votes

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; }
0
votes

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?"

0
votes

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; }