I have a form on a page which uses Model Validation, with
set on the page. This works fine.
Below this form I have a grid of items, and I want to add a "Delete" button to each line of the grid. In this case, there is no validation to be done, but I want the button to POST to the server, so each button looks something like this:
However, since EnableClientValidation is set for the page, I find that MVC adds a bit of JavaScript below each button, checking if there is anything to validate:
In this case, this extra script is not necessary, since there is nothing to validate.
So my question is whether there is any way to specify/restrict which Forms on a page will use client validation, to avoid this extra, unecessary script being included in the page?
Thanks in advance for any advice.
Appetere Web Solutions www.appetere.com is a web design & development company in Brighton & Hove, UK, using Microsoft's ASP.NET technology, with C#. Our product range includes a fully configurable & scalable Content Management System, based on Kentico CMS.
Steve, this should work. I’m taking a stab here, but I bet that your individual delete forms are nested instead another larger form that actually needs validation. Is that correct? If so, that’s what is causing your issue. Currently, that setup confuses
the MVC client validation and I haven’t been able to find a way around it.
Your options are to figure out a way to un-nest the forms. Or to change the inner delete forms to use links (GET). If want to keep the delete buttons using the POST verb, you could still use links (or standard buttons) and preform a POST by AJAX.
Good thought, but none of the forms were nested. It seems that if EnableClientValidation is set for the page, then the Html.BeginForm helper will automatically render the JavaScript, whether needed or not.
However, I have found a simple workaround, where you add the <form> element manually, and no JavaScript is added:
Appetere Web Solutions www.appetere.com is a web design & development company in Brighton & Hove, UK, using Microsoft's ASP.NET technology, with C#. Our product range includes a fully configurable & scalable Content Management System, based on Kentico CMS.
Ah sorry, I thought you meant that the forms were causing validation to happen. You just want to get rid of the unnecessary script.
Another option is to call Html.EnableClientValidation() below the delete forms. EnableClientValidation() will only add the JavaScript for the forms that are rendered after it has been called.
Steve, this should work. I’m taking a stab here, but I bet that your individual delete forms are nested instead another larger form that actually needs validation. Is that correct? If so, that’s what is causing your issue. Currently, that setup confuses
the MVC client validation and I haven’t been able to find a way around it.
Not really related to the original question, but nested <form> elements are illegal in HTML.
Call this before the form(s) for which you want to disable validation. If you want to re-enable later in the page, just call EnableClientValidation() again (or set that property to
true).
Marked as answer by SteveMoss on Apr 06, 2010 06:41 PM
Not really related to the original question, but nested <form> elements are illegal in HTML.
Yep, but I run into people trying to make it work all the time. Especially in the context of an editable-grid-form contained in a larger save-page-form.
I'm not advocating nested form elements or suggesting that MVC client validation should support them.
Call this before the form(s) for which you want to disable validation. If you want to re-enable later in the page, just call EnableClientValidation() again (or set that property to
true).
Appetere Web Solutions www.appetere.com is a web design & development company in Brighton & Hove, UK, using Microsoft's ASP.NET technology, with C#. Our product range includes a fully configurable & scalable Content Management System, based on Kentico CMS.
SteveMoss
Member
8 Points
17 Posts
EnableClientValidation for only some forms on a page
Apr 06, 2010 11:32 AM|LINK
I have a form on a page which uses Model Validation, with
However, since EnableClientValidation is set for the page, I find that MVC adds a bit of JavaScript below each button, checking if there is anything to validate:
</form> <script type="text/javascript"> //<![CDATA[ if (!window.mvcClientValidationMetadata) { window.mvcClientValidationMetadata = []; } window.mvcClientValidationMetadata.push({"Fields":[],"FormId":"form0","ReplaceValidationSummary":false}); //]]> </script>In this case, this extra script is not necessary, since there is nothing to validate.
So my question is whether there is any way to specify/restrict which Forms on a page will use client validation, to avoid this extra, unecessary script being included in the page?
Thanks in advance for any advice.
Nick Riggs
Member
502 Points
81 Posts
Re: EnableClientValidation for only some forms on a page
Apr 06, 2010 01:09 PM|LINK
Steve, this should work. I’m taking a stab here, but I bet that your individual delete forms are nested instead another larger form that actually needs validation. Is that correct? If so, that’s what is causing your issue. Currently, that setup confuses the MVC client validation and I haven’t been able to find a way around it.
Your options are to figure out a way to un-nest the forms. Or to change the inner delete forms to use links (GET). If want to keep the delete buttons using the POST verb, you could still use links (or standard buttons) and preform a POST by AJAX.
SteveMoss
Member
8 Points
17 Posts
Re: EnableClientValidation for only some forms on a page
Apr 06, 2010 01:36 PM|LINK
Good thought, but none of the forms were nested. It seems that if EnableClientValidation is set for the page, then the Html.BeginForm helper will automatically render the JavaScript, whether needed or not.
However, I have found a simple workaround, where you add the <form> element manually, and no JavaScript is added:
<form action="<%= Url.Action("DeleteRelationship") %>" class="inlineForm" method="post"> <input type="submit" value="Remove" /> <input id="Id1" name="Id1" type="hidden" value="<%= Model.Id %>" /> <input id="Id2" name="Id2" type="hidden" value="<%= item.Id %>" /> </form>This then does what I need.
Nick Riggs
Member
502 Points
81 Posts
Re: EnableClientValidation for only some forms on a page
Apr 06, 2010 01:55 PM|LINK
Ah sorry, I thought you meant that the forms were causing validation to happen. You just want to get rid of the unnecessary script.
Another option is to call Html.EnableClientValidation() below the delete forms. EnableClientValidation() will only add the JavaScript for the forms that are rendered after it has been called.
bradwils
Contributor
5779 Points
691 Posts
Microsoft
Re: EnableClientValidation for only some forms on a page
Apr 06, 2010 02:00 PM|LINK
Not really related to the original question, but nested <form> elements are illegal in HTML.
levib
Star
7702 Points
1099 Posts
Microsoft
Re: EnableClientValidation for only some forms on a page
Apr 06, 2010 05:08 PM|LINK
<% ViewContext.ClientValidationEnabled = false; %>
Call this before the form(s) for which you want to disable validation. If you want to re-enable later in the page, just call EnableClientValidation() again (or set that property to true).
Nick Riggs
Member
502 Points
81 Posts
Re: EnableClientValidation for only some forms on a page
Apr 06, 2010 05:18 PM|LINK
Yep, but I run into people trying to make it work all the time. Especially in the context of an editable-grid-form contained in a larger save-page-form.
I'm not advocating nested form elements or suggesting that MVC client validation should support them.
Nick Riggs
Member
502 Points
81 Posts
Re: EnableClientValidation for only some forms on a page
Apr 06, 2010 05:19 PM|LINK
Nice! I didn't know that trick.
SteveMoss
Member
8 Points
17 Posts
Re: EnableClientValidation for only some forms on a page
Apr 06, 2010 07:07 PM|LINK
Thanks levib, that worked a treat.