2010-03-06 54 views
0

我创建了一个自定义的标签,看起来像这样:的Groovy/Grails的验证和hasFieldErrors问题

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:35329 [HTTP-8080-2] ERROR view.GroovyPageView - 错误处理GroovyPageView:错误执行标签:org.codehaus.groovy.grails .web.taglib.exceptions.GrailsTagException:执行标记时出错:groovy.lang.MissingPropertyException:没有这样的属性:类的客户端:com.personal.Client at/Users/dean/Projects/PersonalGrails/grails-app/views /登记/ index.gsp中:98在/Users/dean/Projects/PersonalGrails/grails-app/views/registration/index.gsp:145

问题是当hasFieldErrors被称为对bean。它通过应该是“workPhone”的“字段”。逐个调试器显示该字段完全是“workPhone”。但是,通过进一步检查字段变量,它显示字段的内部值为“client.workPhone”,偏移量= 7,计数= 9,哈希= 0.但是,如果调用toString(),则会返回“ workPhone“,就像你期望的那样。

我想知道Grails或者甚至Spring没有正确地使用这个字符串?它看起来像试图使用该字符串的真实值,而不是注意该字符串的偏移量/计数并找回目标。

有没有人看到我做错了什么?或者你知道解决方法吗?我可以给任何需要的信息,只要问......这让我疯狂!

回答

1

看起来你的标签的目的是减少渲染表单时所需的样板GSP代码的数量。你有没有考虑过使用bean-fields插件呢?

+0

谢谢!我是grails(来自rails)的新手,所以我不习惯一切都是插件,并且不认为在滚动自己的插件之前先看看插件。这绝对是我想要做的。 – intargc 2010-03-11 15:42:28

+0

呃,没关系...认为它不能与命令对象一起工作,但似乎我错误地使用了它。 – intargc 2010-03-11 16:27:52