0

I'm currently working on an application which is being developed using C# and Asp.Net MVC. I have a form which is split into two parts. There are 3 submit buttons each if clicked, of course performs a client side validation and complains if one or more of the inputs are required.

The clicks of as follows:

  1. Submit - validation should not be first half of the form
  2. Save - no validation required
  3. Temporarily Add - validation on the bottom half of the form

My view model class looks something like

public class ViewModel
{
    public User User { get; set; } //used for first half of the form
    public Department Department { get; set; } //used for second half of the form
}

and my POCO classes look like

public class User
{
    public int Id { get; set; }

    [Required]
    public string Username { get; set; } 
    public DateTime Dob { get; set; }

    //more required properties
}

public class Department
{
    public int Id { get; set; }

    [Required]
    public string DepartmentName { get; set; }

    //more required properties
}

On Save click if I had class cancel then that seems to work and no validation is done. However I can seem to figure out how I can do it for the other two buttons. Is there a way or am I completely going off the rail here.

Izzy
  • 6,740
  • 7
  • 40
  • 84
  • you have to handle it manually. there is no any automated validation for this type of case. – K D Jan 19 '17 at 08:36
  • @KD would it be possible if you can provide a small example please. – Izzy Jan 19 '17 at 08:37
  • No clear what your wanting to achieve. Do you only want to validate the 2nd part based on some condition of the 1st part? Are you wanting to save the 1st part before the 2nd part –  Jan 19 '17 at 08:37
  • this might come handy: http://stackoverflow.com/questions/15154309/jquery-mvc-4-client-side-validation-wont-work – Masoud Andalibi Jan 19 '17 at 08:39
  • Option 1: As you are relying on Data Annotations, either you should disable client side validation and handle these clicks on server side. Option 2: On click of every button, add a JS function which will first remove all validation attributes from form elements where it is not needed and then try to submit the form. Let me know which stratagy you preffer – K D Jan 19 '17 at 08:40
  • @StephenMuecke apologies for not being clear enough. 1st or 2nd part can be saved independantly or at once together. That's why I have put them in the same form and not to separate forms – Izzy Jan 19 '17 at 08:40
  • @KD option one seems to be ideal because I've got so many required Annotations – Izzy Jan 19 '17 at 08:42
  • @Valkyriee Thanks for the link i'll have a look – Izzy Jan 19 '17 at 08:42
  • if you choose option 1, you can disable client validation using link Valkyriee provided, then you have to create a hidden field which will hold information about which button is clicked, and on the basis of that value posted, you can write your own logic on server side in controller. – K D Jan 19 '17 at 08:44
  • @KD I've just tried to disable the client validation from the link provided and now each button disables the validation for the entire form. Would I need to write it in a custom script or inside the `
    Html.EnableClientValidation(false); Html.EnableUnobtrusiveJavaScript(false);
    this will work?
    – Izzy Jan 19 '17 at 09:08
  • Well as you selected Option 1 there is no any client side validation hack you need to add. You just have to handle it on your controller as i mentioned in my previous comment. the question you asked in your recent comment points towards Option 2 :) – K D Jan 19 '17 at 09:14
  • @KD Right I got you. Option 2 sounds like the correct one because I won't need to do a post back to the server and the client is warned straight away. Could you provide an example of this – Izzy Jan 19 '17 at 09:19

1 Answers1

1

as you decided to use Option 2 in your comment, here is the solution for it.

Provided you have 3 different buttons...

<button type="button" id="save" class="submit">Save</button>
<button type="button" id="submit" class="submit">Submit</button>
<button type="button" id="tempAdd" class="submit">Temporarily Add</button>

you have to add one function and bind it with all above buttons.

$(function(){
 $("submit").click(function(){
   var ignoreValidationElements = [];
   if$(this).attr("id") == "save")
   {
     // add element ids which you want to exclude from validation
     ignoreValidationElements.push("FirstName"); 
     ignoreValidationElements.push("LastName");
   }
   else if$(this).attr("id") == "submit")
   {
     ignoreValidationElements.push("FirstName");
   }
   else if$(this).attr("id") == "tempAdd")
   {
     ignoreValidationElements.push("LastName");
   }

    // code to remove validation attributes
    for(i = 0; i < ignoreValidationElements.length;i++)
    {
       var element = $("#" + ignoreValidationElements[i]);
       // remove all validation related attributes from it
       var attributes = element[0].attributes;
       for(var j = 0; j <attributes.length; j++)
       { 
           if(attributes[j].name.indexOf("data-") >=0)
           {
               var attributItem = attributes[j];
               var attributeName = attributeItem.name;
               element.removeAttr(attributeName);

           }
       }

//submit the form
    }
  });

});
K D
  • 5,889
  • 1
  • 23
  • 35
  • you can enhance this logic as per your needs, but this solution will give you a whole idea about option 2 mentioned in my comments – K D Jan 19 '17 at 09:50
  • Thanks for this, this gives me a good idea. However I get an error saying `attributes[j].indexOf is not a function`. Also should'nt the form be submitted outside the outer `for` loop – Izzy Jan 19 '17 at 09:59
  • I've fixed that error. Thanks for this. Just quickly, as I mentioned should'nt the form be submitted out of the loop? – Izzy Jan 19 '17 at 10:41
  • No I still got the same issue on the required fields where I receive an error if its not filled in – Izzy Jan 19 '17 at 10:58
  • 1
    can you try to refer this link ? http://stackoverflow.com/questions/2853416/jquery-disable-rule-validation-on-a-single-field – K D Jan 19 '17 at 10:59
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/133536/discussion-between-code-and-k-d). – Izzy Jan 19 '17 at 11:03