gpt4 book ai didi

grails - Grails拒绝值错误和域错误验证

转载 作者:行者123 更新时间:2023-12-02 14:30:14 26 4
gpt4 key购买 nike

我正在尝试在grails中进行一些服务器端验证,并将我的错误作为json传递回前端,以便由angularjs处理。

错误条件

Department - required 
Department - unique
Description - foobar not allowed

我有以下代码。

Controller
def saveDepartment() {
def errors = []
def success = true
def department

try{
department = departmentService.save(request.JSON);

if(department.hasErrors()) {
success = false
errors = department.errors.fieldErrors;
}
} catch(Exception e){
e.printStackTrace()
errors = "Unknown"
success = false

if(log.errorEnabled){
log.error("save department encountered unknown error: ", e)
}

response.status = 500
} finally {
respond ([success:success, errors:errors, department:department]) as JSON;
}
}

服务
def save(jsonObj) {
def dept = new Department();
dept.setName(jsonObj.name);
dept.setDescription(jsonObj.description);

if(dept.description.equals('foobar')) {
dept.errors.rejectValue('description', 'foobar', 'Foobar is not allowed')
}

if (!dept.save()) {
dept.discard();
}

return dept;
}

服务方法尝试2,带有调试代码
def save(jsonObj) {
def dept = new Department();
dept.setName(jsonObj.name);
dept.setDescription(jsonObj.description);

if(dept.description.equals('foobar')) {
println 'rejected value '
dept.errors.rejectValue('description', 'foobar', 'Foobar is not allowed')
}

println 'dept errors ' + dept.errors.allErrors.size();

if (dept.errors.hasErrors()) {
dept.errors.allErrors.each {FieldError error ->
println error
}
}

if (!dept.save(true)) {
println 'dept errors 2 ' + dept.errors.allErrors.size();

if (dept.errors.hasErrors()) {
dept.errors.allErrors.each {FieldError error ->
println error
}
}
}

return dept;
}

输出量
..................rejected value 
dept errors 1
Field error in object 'org.hri.leaverequest.Department' on field 'description': rejected value [foobar]; codes [foobar.org.hri.leaverequest.Department.descripti
on,foobar.description,foobar.java.lang.String,foobar]; arguments []; default message [Foobar is not allowed]
dept errors 2 1
Field error in object 'org.hri.leaverequest.Department' on field 'name': rejected value [null]; codes [org.hri.leaverequest.Department.name.nullable.error.org.h
ri.leaverequest.Department.name,org.hri.leaverequest.Department.name.nullable.error.name,org.hri.leaverequest.Department.name.nullable.error.java.lang.String,or
g.hri.leaverequest.Department.name.nullable.error,department.name.nullable.error.org.hri.leaverequest.Department.name,department.name.nullable.error.name,depart
ment.name.nullable.error.java.lang.String,department.name.nullable.error,org.hri.leaverequest.Department.name.nullable.org.hri.leaverequest.Department.name,org.
hri.leaverequest.Department.name.nullable.name,org.hri.leaverequest.Department.name.nullable.java.lang.String,org.hri.leaverequest.Department.name.nullable,depa
rtment.name.nullable.org.hri.leaverequest.Department.name,department.name.nullable.name,department.name.nullable.java.lang.String,department.name.nullable,nulla
ble.org.hri.leaverequest.Department.name,nullable.name,nullable.java.lang.String,nullable]; arguments [name,class org.hri.leaverequest.Department]; default mess
age [Property [{0}] of class [{1}] cannot be null]

问题

如果部门为null,并且说明的foobar具有rejectValue,则仅返回一个错误“部门null”,而foobar不会出现在错误中。

如果部门包含现有值,并且描述包含foobar,则返回唯一约束,但foobar不会出现在错误中。

如果部门具有很好的值并且foobar仍然存在,则rejectValue不会阻止保存的发生,并且不会引发任何错误。现在,如果我在rejectValue之后输出dept.errors,我可以看到该错误实际上存在。

目标

我的目标是返回我所有的错误,如果存在错误,则不保存到数据库中,要实现该目标我缺少什么?

最佳答案

您可以这样操作:

dept.validate()

if(dept.description.equals('foobar')) {
dept.errors.rejectValue('description', 'foobar', 'Foobar is not allowed')
}

if(!dept.errors.hasErrors()) {
dept.save()
}

return dept

关于grails - Grails拒绝值错误和域错误验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36480281/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com