gpt4 book ai didi

spring - Groovy/Grails验证和hasFieldErrors问题

转载 作者:行者123 更新时间:2023-12-02 15:39:43 25 4
gpt4 key购买 nike

我创建了一个自定义标签,如下所示:

def textField = { attrs ->
def field = attrs.name.split('\\.')[-1]
log.error("--------- Field is ${field}")
if (attrs.bean && attrs.bean.errors.hasFieldErrors(field)) {
def className = attrs.remove('class')
def classStr = 'errors '
if (className) {
classStr += className
}
attrs.put('class', classStr)
attrs.put('value', attrs.bean[field])
attrs.remove('bean')
}
out << g.textField(attrs)
}

我在GSP中这样称呼它:
<my:textField bean="${client}" name="client.firstName"/>
<my:textField bean="${client}" name="client.lastName"/>
...
<my:textField bean="${client}" name="client.workPhone"/>

这是我的域类
class Client {

String email
String address
String city
String state
String zip
String firstName
String lastName
String phone
String workPhone
String mobilePhone
String birthCountry
String citizenshipCountry
String emergencyContactName
String emergencyPhone
String disabilities
String experience

static constraints = {
email(email:true, unique:true, blank:false)
address(blank:false)
city(blank:false)
state(blank:false)
zip(blank:false)
firstName(blank:false)
lastName(blank:false)
phone(blank:false)
emergencyContactName(blank:false)
emergencyPhone(blank:false)

workPhone(blank:true, nullable:true)
mobilePhone(blank:true, nullable:true)
birthCountry(blank:true, nullable:true)
citizenshipCountry(blank:true, nullable:true)
disabilities(blank:true, nullable:true)
experience(blank:true, nullable:true)
}

static mapping = {
disabilities type: 'text'
experience type: 'text'
}

static hasMany = [courses:ClientCourseMap]
}

该页面加载正常,除非我实际上有一个“客户端” bean。它一直加载到最后一个标签“client.workPhone”。然后我得到以下异常:

2010-03-06 18:32:35,329 [http-8080-2] ERROR view.GroovyPageView - Error processing GroovyPageView: Error executing tag : org.codehaus.groovy.grails.web.taglib.exceptions.GrailsTagException: Error executing tag : groovy.lang.MissingPropertyException: No such property: client for class: com.personal.Client at /Users/dean/Projects/PersonalGrails/grails-app/views/registration/index.gsp:98 at /Users/dean/Projects/PersonalGrails/grails-app/views/registration/index.gsp:145



问题是在bean上调用hasFieldErrors时。它传入应该是“workPhone”的“field”。单步调试器显示该字段完全是“workPhone”。但是,进一步检查该字段变量,它表明该字段的内部值为“client.workPhone”,offset = 7,count = 9,hash =0。但是,如果调用toString(),则会返回“如您所愿。

我想知道Grails是不是甚至Spring都没有正确使用此字符串?看起来它正在尝试使用该字符串的实际值,而不是注意该字符串的偏移量/计数并找回预期的值。

有人看到我做错了吗?还是您知道解决方法?我可以提供所需的任何信息,只需问...这真让我发疯!

最佳答案

看起来您标签的意图是减少呈现表单时所需的样板GSP代码量。您是否考虑过使用bean-fields插件?

关于spring - Groovy/Grails验证和hasFieldErrors问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2394530/

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