2

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

1 Answer 1

10

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Can't believe I didn't notice this! ty

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.