asp.net mvc razor Ajax form custom validation message using result from 3rd party ajax

Viewed 14

I have an ajax form like this:

   @using (Ajax.BeginForm("Update", "Jobs", new AjaxOptions { HttpMethod = "Post", OnBegin = "updateFormSubmitBegin" }, new { enctype = "multipart/form-data"}))
            {
                <div>
                    @Html.HiddenFor(m => m.Id)
                    <table>
                        <tr>
                            <td valign="top" width="55%">
                                <div class="divSeperator">
                                    @Html.LabelFor(m => m.Name) <br />
                                    @Html.Kendo().TextBoxFor(m => m.Name).HtmlAttributes(new { style = "width: 300px;" })
                                    @Html.ValidationMessageFor(m => m.Name, String.Empty, new { @style = "color:red !important;" })
                                </div>
                                <div class="divSeperator">
                                    @Html.LabelFor(m => m.Description)<br />
                                    @Html.TextAreaFor(m => m.Description, new { style = "width: 280px; height: 200px; min-height: 100px; max-height: 500px; resize: vertical; vertical-align: top;" })
    
                                </div>
                            </td>
                            // many more fields - textbox and dropdowns
                <br />
                <div class="divSeperator">
                    <div style="display: inline-block; margin-left: 10px;">
                        <input type="submit" value="Update" name="submit" id="Submit" class="k-button k-primary" style="width: 150px;" />
                    </div>
                </div>
            }

There are validators defined in the model and those are working fine.

Now the problem is: AFTER all the fields are filled out, and BEFORE the form info is submitted for processing, I need to call a third party api to validate the details, and if the Job Id -fyi, this is the field m.Id- is not unique, I need to trigger a custom validation message for Id field

The validation is done like this

<script>
   function updateFormSubmitBegin() {
            var result=false;

            $.ajax({
                type: "POST",
                url: '/third-party/validate',
                data: { ...multiple field values... },                
                success: function (data) {
                    result = data;//returns true is everything is ok
                },
                error: function (data) {
                    DisplayAjaxError("An unknown Error has Occured.");
                    result = false;
                        }
             });

            return result;//when false, I want to trigger model error with custom message
   }
</script>

Now, I know that I can just insert a label and show the error message.

However my question is about how to use validators to achieve the same only from client side.

I searched a lot in stackoverflow, but I see custom validation with server side validators. In my case that is not possible.

0 Answers
Related