grails jquary ajax,json - Grails JQuery Ajax Form Validation - Stack Overflow

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 = $("

  • ");

for (field in errors) {

errorList.append("

" + errors[field] + "")

$('input[name=' + field + ']').addClass('error');

}

if (!element) {

$(".errors").html("").append(errorList).show(500);

} else {

$(element).html("").append(errorList).show(500);

}

}

Hope that helps.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值