jquery-ajax-unobtrusive icon indicating copy to clipboard operation
jquery-ajax-unobtrusive copied to clipboard

Modifying an unobtrusive ajax form post to pass to its handler a status of 'error…

Open AnthonyGilliam opened this issue 9 years ago • 4 comments
trafficstars

…' if the form has field validation errors or validation summary errors. This should not disturb per-existing code as the 'status' var passed back on an ajax success is currently always 'success'. I created these changes in another branch just in case. I've tested both files and they work.

AnthonyGilliam avatar Apr 27 '16 21:04 AnthonyGilliam

Hi @AnthonyGilliam, I'm your friendly neighborhood .NET Foundation Pull Request Bot (You can call me DNFBOT). Thanks for your contribution! In order for us to evaluate and accept your PR, we ask that you sign a contribution license agreement. It's all electronic and will take just minutes. I promise there's no faxing. https://cla2.dotnetfoundation.org.

TTYL, DNFBOT;

dnfclas avatar Apr 27 '16 21:04 dnfclas

@AnthonyGilliam, Thanks for signing the contribution license agreement so quickly! Actual humans will now validate the agreement and then evaluate the PR.
Thanks, DNFBOT;

dnfclas avatar Apr 27 '16 22:04 dnfclas

@AnthonyGilliam sorry for the delay in the response. Can you clarify what it is that this change helps achieve? I just want to make sure we understand it before going further. Thanks!

Eilon avatar May 18 '16 06:05 Eilon

Hi @Eilon, thanks for the response. This code change makes the OnSuccess handler function passed into the System.Web.Mvc.AjaxHelper<TModel>.BeginForm() method via AjaxOptions receive a status parameter equal to "error" if the form being posted has validation errors or errors in its validation summary.

Basically, if you build a handler function to respond to an ajax form post, the handler method will receive a status of "error" if the form has any validation errors. Without this change, the handler method currently receives a status of "success" from an ajax form post even if the form posted has validation errors.

Consider the following:

function onAjaxFormSuccessfullyPosted(data, status) {
    //Before my pull request, this status is always "success" if the ajax request returns a code of 200
    if (status && status === "success") {
        //Redirect to success page;
    } else if (status === "error") {  //Here's my change: 
        //The status will be 'error' if the form posted is invalid

        /*Highlight and display validation errors...*/
    }
}

AnthonyGilliam avatar May 18 '16 14:05 AnthonyGilliam