Html.ValidationSummary Questionhttp://forums.asp.net/t/1797478.aspx/1?Html+ValidationSummary+QuestionMon, 30 Apr 2012 18:04:50 -040017974784953706http://forums.asp.net/p/1797478/4953706.aspx/1?Html+ValidationSummary+QuestionHtml.ValidationSummary Question <p>I have a validation like this:</p> <pre class="prettyprint">@{ ViewContext.FormContext.ValidationSummaryId = &quot;valSumId&quot;;} @Html.ValidationSummary(false, &quot;Please fix these errors.&quot;, new Dictionary&lt;string, object&gt; { { &quot;id&quot;, &quot;valSumId&quot; } })</pre> <p>I have a submit button on my form that fires a javascript function:</p> <pre class="prettyprint"> &#36;(function () { &#36;("#submit").click(function () { onSubmit(); }); }); function onSubmit() { &#36;('#loading').toggle(); &#36;('#loadingspinner').empty(); &#36;('#loadingspinner').append('&lt;h3 style="text-align: center;"&gt;Please Wait...&lt;/h3&gt;&lt;p /&gt;'); &#36;('#loadingspinner').append('&lt;img alt="Please wait" src="/images/spinner_big.gif" style="display: block;margin-left: auto; margin-right: auto;" /&gt;'); }</pre> <p>If the validation fails, the submit does not go to the controller's action. The validations that failed are displayed.</p> <p>I now have a spinner.gif being displayed and I want it to go away if validation fails. How do I accomplish this?</p> <p>I have a function that I can call to make the spinner go away but no event to call the function on.</p> <pre class="prettyprint"> function onSubmitCancel() { &#36;('#loading').toggle(); &#36;('#loadingspinner').empty(); }</pre> <p><br> <br> <br> </p> 2012-04-26T20:42:39-04:004953727http://forums.asp.net/p/1797478/4953727.aspx/1?Re+Html+ValidationSummary+QuestionRe: Html.ValidationSummary Question <p>You want to call onSubmit() during the submit function of the form (which should only fire when the form is about to be submitted) and not on the click event for the button (which fires every time).</p> <p>So instead of &#36;(&quot;#submit&quot;).click use &#36;(&quot;form&quot;).submit or similar.</p> <p></p> 2012-04-26T20:58:42-04:004953749http://forums.asp.net/p/1797478/4953749.aspx/1?Re+Html+ValidationSummary+QuestionRe: Html.ValidationSummary Question <p>That is not going to work. I want the spinner.gif to be visible when the submit goes through. What I am looking for is a way to make the spinner.gif be not visible when the submit fails and the Validation error are displayed.</p> 2012-04-26T21:09:42-04:004953751http://forums.asp.net/p/1797478/4953751.aspx/1?Re+Html+ValidationSummary+QuestionRe: Html.ValidationSummary Question <p>Are you saying that you are submitting the form via ajax? Because otherwise if you are doing a standard post back to the server the validation should trigger immediately and your submit() binding should never happen (so there's no need to &quot;hide&quot; your loading message), unlike your click() binding which will happen whether the form is valid or not.</p> 2012-04-26T21:13:03-04:004953764http://forums.asp.net/p/1797478/4953764.aspx/1?Re+Html+ValidationSummary+QuestionRe: Html.ValidationSummary Question <p>You are confusing the issue entirely. I am only concerned with calling my javascript function to disable the spinner.gif if validation fails and the validation errors are displayed. I believe that I described everything in my first post.</p> 2012-04-26T21:18:33-04:004953769http://forums.asp.net/p/1797478/4953769.aspx/1?Re+Html+ValidationSummary+QuestionRe: Html.ValidationSummary Question <p>Why would the spinner ever appear if validation failed? Wouldn't you just not show the spinner if until the form is submitted and validation was successful?</p> 2012-04-26T21:22:17-04:004953789http://forums.asp.net/p/1797478/4953789.aspx/1?Re+Html+ValidationSummary+QuestionRe: Html.ValidationSummary Question <p>I now understand where you are coming from and I agree with you, however I can't get the following to fire:</p> <pre class="prettyprint">$('#myform').submit(function (e) { alert(&quot;onSubmit&quot;); onSubmit(); });</pre> <p>Any ideas what I am doing wrong?</p> <p>Here is the submit button:</p> <pre class="prettyprint">&lt;input name="submit" id="submit" type="submit" value="Save" style="cursor:pointer" /&gt;</pre> <p></p> 2012-04-26T21:39:35-04:004953850http://forums.asp.net/p/1797478/4953850.aspx/1?Re+Html+ValidationSummary+QuestionRe: Html.ValidationSummary Question <p>Are you sure the selector for the form tag is right? Is the form id &quot;myform&quot;? Also make sure that the form is validating and that no other javascript on the page is preventing the submit event from bubbling up.</p> 2012-04-26T22:40:57-04:004955084http://forums.asp.net/p/1797478/4955084.aspx/1?Re+Html+ValidationSummary+QuestionRe: Html.ValidationSummary Question <p>I got it to work but not using the actual id of the Html.BeginForm, I used this and this works:</p> <pre class="prettyprint">$(function () { $(&quot;form&quot;).submit(function (e) { alert(&quot;onSubmit&quot;); onSubmit(); }); });</pre> <p>But this event fires even when the form does not validate. I either need to find a way for this event not to fire if the form does not validate or be able to check inside the event if the form validated. How do I do this?</p> <p>&nbsp;</p> 2012-04-27T14:08:59-04:004958361http://forums.asp.net/p/1797478/4958361.aspx/1?Re+Html+ValidationSummary+QuestionRe: Html.ValidationSummary Question <p>Unlike what I was told, the submit event is fired even if the validation fails. The submit does not reach the controller if the validation fails. The only way to not enable the spinner.gif that I am displaying is this:</p> <pre class="prettyprint">$(function () { $(&quot;form&quot;).submit(function (e) { if ($('.input-validation-error').size() == 0) { onSubmit(); } }); });</pre> <p></p> 2012-04-30T14:09:38-04:004958733http://forums.asp.net/p/1797478/4958733.aspx/1?Re+Html+ValidationSummary+QuestionRe: Html.ValidationSummary Question <p>Appologies, you are right the event will fire every time. However you can check the validation status using the valid() method on the form itself:</p> <pre class="lang-cs prettyprint"><pre class="prettyprint">$('form').submit(function () { if($(this).valid()) { alert('the form is valid'); } });</pre></pre></pre> 2012-04-30T18:04:50-04:00