locked
Post + Validation Summary RRS feed

  • Question

  • User-2051275383 posted

    Hey guys,

    I have a form that when you press post goes through some RequireField validation.. and it all works fine.

    However I want users to press post, get those errors WITHOUT loosing the fields that are correct? What can be done to modify my code to enable this functionality?

    Cheers~!


    Sunday, December 14, 2014 8:53 PM

Answers

  • User-821857111 posted

    Or I guess this doesn't matter as the field has content in it?

    Correct. Either the database value will persist or the incorrect amended value will.

    Another way to ensure that the values persist is to prevent the form being submitted if there are validation errors. You can do that by enabling unobtrusive client side validation. The easiest way to do that is to convert your inputs into HTML helpers so that 

    <p>Name:<input type="text" name="formName" value="@Request.Form["formName"]"/></p>

    becomes

    @Html.Textbox("Name: ", "formName", Request["formName"]) @Html.ValidationMessage("formName")

    Then you just need to add jQuery, jQuery.validate and jQuery.validate.unobtrusive to your page and it works out of the box.

    http://www.mikesdotnetting.com/article/191/validation-in-razor-web-pages-2

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 15, 2014 4:28 AM

All replies

  • User379720387 posted

    We can merely speculate what your code is, could you narrow things down for us?

    Sunday, December 14, 2014 11:01 PM
  • User-2051275383 posted

    Here is an example:

    @{
         var Name=""; 
         var Details="";
    
         if(IsPost){
             
             Validation.RequireField("formName", "You must enter a name");
             Name=Request["formName"];
    
             Validation.RequireField("formDetails", "You must details");
             Details=Request["formName"];
        
         if(Validation.IsValid()){ 
    
             var SQLINSERT = "INSERT INTO Things (name, details) VALUES (@0, @1)";
    
             var db = Database.Open("MyDatabase");
             
             db.Execute(SQLINSERT, Name, Details);
            
            Response.Redirect(Href("AdminPage"));
           }
         }
      }
    
    <div>   
    
    @Html.ValidationSummary()
         
     <form action="" method="post">
     <p>Name:<input type="text" name="formName" /></p>
     <p>Details:<input type="text" name="formDetails" /></p>
     <p><input type="submit" value="Add" /></p>
     </form>
    
    </div>

    Should I be looking at coding the validation differently? - For example:

    @{
         var Name="";
         var Details="";
    
      if (IsPost) {
            Name = Request.Form["formName"];
    
            if (Name.IsEmpty()) {
                emailErrorMessage = "You must specify a name!";
                isValid = false;
            }
    
            Name = Request.Form["formDetails"];
    
            if (Details.IsEmpty()) {
                emailErrorMessage = "You must specify details";
                isValid = false;
            }
          
         
         if (isValid) {  
    
             var SQLINSERT = "INSERT INTO Things (name, details) VALUES (@0, @1)";
    
             var db = Database.Open("MyDatabase");
             
             db.Execute(SQLINSERT, Name, Details);
            
            Response.Redirect(Href("AdminPage"));
           }
         }
      }
    
    <div>   
    
    @Html.ValidationSummary()
         
     <form action="" method="post">
     <p>Name:<input type="text" name="formName" /></p>
     <p>Details:<input type="text" name="formDetails" /></p>
     <p><input type="submit" value="Add" /></p>
     </form>
    
    </div>

    Open to suggestions that will avoid losing the data fields that ARE correct, displaying the error and enabling users to only resolve the fields that require it - instead of having to fully re-do the form?






    Monday, December 15, 2014 2:19 AM
  • User-735851359 posted

    Hello,

    use value parameter:

     <p>Name:<input type="text" name="formName" value="@Request.Form["formName"]"/></p>

    Monday, December 15, 2014 2:55 AM
  • User-2051275383 posted

    Hello,

    use value parameter:

     <p>Name:<input type="text" name="formName" value="@Request.Form["formName"]"/></p>



    What if I used this code on an 'Edit' page and I use the value field to return information from the database? Or I guess this doesn't matter as the field has content in it?

    Monday, December 15, 2014 3:53 AM
  • User-821857111 posted

    Or I guess this doesn't matter as the field has content in it?

    Correct. Either the database value will persist or the incorrect amended value will.

    Another way to ensure that the values persist is to prevent the form being submitted if there are validation errors. You can do that by enabling unobtrusive client side validation. The easiest way to do that is to convert your inputs into HTML helpers so that 

    <p>Name:<input type="text" name="formName" value="@Request.Form["formName"]"/></p>

    becomes

    @Html.Textbox("Name: ", "formName", Request["formName"]) @Html.ValidationMessage("formName")

    Then you just need to add jQuery, jQuery.validate and jQuery.validate.unobtrusive to your page and it works out of the box.

    http://www.mikesdotnetting.com/article/191/validation-in-razor-web-pages-2

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 15, 2014 4:28 AM
  • User-2051275383 posted

    a-rad

    Or I guess this doesn't matter as the field has content in it?

    Correct. Either the database value will persist or the incorrect amended value will.

    Another way to ensure that the values persist is to prevent the form being submitted if there are validation errors. You can do that by enabling unobtrusive client side validation. The easiest way to do that is to convert your inputs into HTML helpers so that 

    <p>Name:<input type="text" name="formName" value="@Request.Form["formName"]"/></p>

    becomes

    @Html.Textbox("Name: ", "formName", Request["formName"]) @Html.ValidationMessage("formName")

    Then you just need to add jQuery, jQuery.validate and jQuery.validate.unobtrusive to your page and it works out of the box.

    http://www.mikesdotnetting.com/article/191/validation-in-razor-web-pages-2



    Thanks Mike - Looks like going down the Unobstrusive path might be the way to go!

    In terms of using @Html.Textbox - Does that have the ability to have a class added to it (referenced from a CSS)?

    Cheers!

    Monday, December 15, 2014 4:50 AM
  • User-1980594115 posted
    @Html.TextBox("frmUserName","this is default value", 
    new { maxlength = 50, size=10, @class = myClass, @something="blahblah" } )
    

    The above is an example using class within @Html.TextBox helper.

    Monday, December 15, 2014 4:46 PM
  • User-821857111 posted

    @something="blahblah"
    You don't need the @ sign before "something". You only need it before "class" because class is a C# key word, and you have to escape those if you use them as property names (or other identifiers).

    http://msdn.microsoft.com/en-gb/library/aa664670%28v=vs.71%29.aspx

    Monday, December 15, 2014 5:34 PM
  • User-2051275383 posted

    Thanks again Mike!

    I was going to take a look at your sample that you point to on your article - however I am getting an error: https://github.com/mikebrind/ValidationWebPages2

    This is the error on Default.cshtml :

    Compiler Error Message: CS0117: 'System.Web.Helpers.Validation' does not contain a definition for 'Add'


    Source Error:

    Line 1:  @{
    Line 2:      var result = "";
    Line 3:      Validation.Add("firstname",

    Cheers!

    Tuesday, December 16, 2014 3:25 AM
  • User-821857111 posted

    You get that error when you try to run Web Pages 2 code but your version of Web Pages is 1.

    Tuesday, December 16, 2014 3:43 AM
  • User-2051275383 posted

    I copied the code into a 'Blank Site' template - works fine! :D

    Cheers Mike!

    Tuesday, December 16, 2014 3:54 AM