locked
Validating Multiple Forms on one page RRS feed

  • Question

  • User1771308999 posted

    Once again, previous forum's solution does not appear to apply to Razor Syntax as it's slightly different.

    I have multiple forms on one page and if a user submit an empty field, the validation summary display ALL the error including the forms that have not yet been submitted.

    How can I get the Validation to differentiate between forms that's being submitted??

    Many thanks in advance!

    Sample:

    Validation.RequiredField("firstname","Must have first name");
    Validation.RequiredField("lastname", "Must have last name");
    
    Validation.RequiredField("email", "Must have email");
    Validation.RequiredField("phonenumber", "Must have phone number");

    This is not my actual code but for clarity's sake: first and last name input is the first form, it has to be submitted before moving on to second form that ask for email and phone number.

    If a user left first and last name field empty, the Validation call the error for first and last name AND email and phone number. I want Validation to display error only on first and last name since that was submitted, not email and phone number.

    Thursday, August 22, 2013 2:12 PM

Answers

  • User-821857111 posted

    You can't very easily use the new Validation helpers for this. You need to manually populate the ModelState dictionary with the individual form errors relevant to the form being submitted.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 22, 2013 5:24 PM

All replies

  • User-1496281956 posted

    This should help you: http://blog.thecodingguys.net/article/36/2013/august/asp-net-webpages-multiple-submit-buttons

    You'd have to put validation in the if statements for the button submited:

    if (IsPost)
    {
      if (!Request[“AddRole”].IsEmpty())
      {
       //Code for AddRole.
    //Validation
      }
    }

    I have not tried it but it should work.

    Thursday, August 22, 2013 3:17 PM
  • User-821857111 posted

    You can't very easily use the new Validation helpers for this. You need to manually populate the ModelState dictionary with the individual form errors relevant to the form being submitted.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 22, 2013 5:24 PM