Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6688801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:26:43+00:00 2026-05-26T05:26:43+00:00

I have a modal dialog where I load a form from a gsp and

  • 0

I have a modal dialog where I load a form from a gsp and I am struggling to find the right solution for an ajax based form with JSON validation incl. success message like a flash scope message:

$('#calendar_form').live('click', function () {
        $.modal({
            ajax: './form'
            , title: '${message(code:'calendar.main.addAppointment')}'
            , overlayClose: true
        });
    });

In that form I have the following JS:

$(document).ready(function() {
    $('#form1').submit(function() {

        $.ajax({
            type: 'POST',
            url: '${createLink(action:'post')}',
            data: $("#form1").serialize(),
            success: function(result) {
                alert(result);
            }
        });
    });
});

I am returning a JSON response if its successful and when an error occurs and I need Ajax otherwise my modal dialog is disapearing due to the new request.

So here are my questions:

  • How do I react on failures which happen during my validation in my controller?
  • Which error http codes do I need to use for validation errors in my controller?
  • How do I update the specific fields with error messages in my form?
  • How do I update the flash part of my page to render the success msg?
  • How is it possible to update other parts of the page after success?

Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:26:44+00:00Added an answer on May 26, 2026 at 5:26 am

    There are a lot of different ways to accomplish this and probably 1 or more plugins to get you going. However, I’ll show you how I generally deal with this. I have an object that looks like this…

    class AjaxPostResponse {
      boolean success
      String message
      String html
      def domainObject
      def errors = [:] 
    }
    

    This is the object I render as JSON. So if there are validation errors, success becomes false and I add all the errors to the errors map. I do this in a service and that method looks like this:

    def preparePostResponse(domainInstance) {
        def g = grailsApplication.mainContext.getBean('org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib')
        def postResponse = new AjaxPostResponse(domainObject: domainInstance)
        if (domainInstance.hasErrors()) {
          g.eachError(bean: domainInstance) {
            postResponse.errors."${it.field}" = g.message(error: it)
          }
          postResponse.success = false
          postResponse.message = "There was an error"
        } else {
          postResponse.success = true
          postResponse.message = "Success"
        }
        return postResponse
    }
    

    So my controller looks something like

    def save = {
      def someObjInstance = new SomeObj(params)
      someObjInstance.save(flush:true)
      render myService.preparePostResponse(someObjInstance) as JSON
    }
    

    In my client side code I do something like this (using the jQuery form plugin, but this would work with a generic $.ajax / $.post / $.get method as well…

    $(formElement).ajaxSubmit({
        dataType: 'json',
        success: function(jsonData) {
          if (jsonData.success) {
            // do good stuff
          } else {
            // bad stuff happened
            showErrors(jsonData.errors);
          }
        }
    });
    

    And my showErrors function

    function showErrors(errors, element) {
        var errorList = $("<ul>");
        for (field in errors) {
            errorList.append("<li>" + errors[field] + "</li>")
            $('input[name=' + field + ']').addClass('error');
        }
        if (!element) {
            $(".errors").html("").append(errorList).show(500);
        } else {
            $(element).html("").append(errorList).show(500);
        }
    }
    

    Hope that helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Anyone have any experience creating a modal dialog box using AJAX and ASP.NET MVC?
I have a Django form which I load dynamically to a jQuery dialog after
I add a link_to_remote to load a modal dialog box from a partial. But
I have a problem with Jquery UI modal dialogs. I have modal dialog (dialogA),
I have a modal dialog that is created with the following: procedure TFormCompose.createParams(var Params:
I have a modal dialog plugin written in jquery, that binds to the click
I have a modal dialog box presented in Yahoo UI. The user selects a
I like to have a popup modal dialog that need to show the list
We have implemented a popup window as a modal dialog using the IE method:
I have a page that opens a modal dialog. After the operations done on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.