Danny Cullen
Danny Cullen

Reputation: 1832

MVC 5 not validating StringLength attribute properly

I'm trying to validate the sortCode field in my PersonPaymentDetails model but my view is failing to validate the StringLength(6). If I submit the form with a value of length 1 it incorrectly validates successfully.

Am I doing something fundamentally wrong here?

/* [CONTROLLER] */
public class PersonController : Controller
{
  [HttpGet]
  [Route("person/paymentDetails/create/{personId?}")]
  public ActionResult PaymentDetailsCreate(int? personId)
  {
      if (personId == null)
      {
          return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
      }
      Person person = db.People.Find(personId);

      if (person == null)
      {
          return HttpNotFound();
      }

      PersonPaymentDetailsViewModel personPaymentDetailsVM = new PersonPaymentDetailsViewModel();
      personPaymentDetailsVM.SetPerson(person);

      return View(personPaymentDetailsVM);
  }

  [HttpPost]
  [Route("person/paymentDetails/create")]
  public ActionResult PaymentDetailsCreate(PersonPaymentDetailsViewModel personPaymentDetailsVM)
  {
      if (ModelState.IsValid)
      {
          /* should not be entering here with sortCode = 123, as not 6 characters in length */
          return Content("No errors: |" + personPaymentDetailsVM.SinglePaymentDetails.sortCode + "|");
      }
  }
}

/* [VIEW] */
@model X.ViewModels.PersonPaymentDetailsViewModel

@Html.ValidationSummary()
@using (Html.BeginForm("PaymentDetailsCreate", "Person", FormMethod.Post, new { @class = " form-horizontal" }))
{
    @Html.HiddenFor(m => m.Person.id, "default")

    <div class="form-group">
        <label for="bankSortCode" class="col-md-3 control-label">Sort Code</label>
        <div class="col-md-9">
            @Html.EditorFor(m => m.SinglePaymentDetails.sortCode, new { htmlAttributes = new { @class = "form-control" } })
        </div>
    </div>

    <div class="form-group">
        <label for="save" class="col-md-3 control-label">&nbsp;</label>
        <div class="col-md-9">
            <button type="submit" class="btn btn-primary">Save</button>
        </div>
    </div>
}

/* [MODEL] */
public partial class PersonPaymentDetails
{
    public int id { get; set; }

    [Required, StringLength(6)]
    public string sortCode { get; set; }
}

/* [ViewModel] */
public class PersonPaymentDetailsViewModel
{
    public Person Person { get; set; }
    public PersonPaymentDetails SinglePaymentDetails { get; set; }

    public void SetPerson(Person person)
    {
        this.Person = person;
        this.SinglePaymentDetails = new PersonPaymentDetails();
    }
}

Upvotes: 2

Views: 10529

Answers (1)

Andrei
Andrei

Reputation: 56688

You want

[Required, StringLength(6, MinimumLength = 6)]

Constructor of StringLength takes in the maximum length only, so as you currently have it it checks that string is not longer than 6 characters, and therefore string of length 1 passes validation successfully.

Upvotes: 10

Related Questions