我正在构建适用于用户个人资料的此网络应用。用户编辑配置文件,如果输入错误数据,配置文件将显示红色的无效字段并显示该消息。非常简单的场景。但由于某些原因,它不像我假设的那样工作。我可能会遗漏某些地方的东西。这是我在控制器中做的事情。
正如您在下面的代码中看到的,我创建了一个新的配置文件,我使用包含列表将params绑定到它,仅绑定配置文件接受的那些参数,然后在配置文件实例上调用hasErrors。在调试模式下,我可以看到当用户输入错误时myProfile有错误,但是当我回到editProfile页面时,它没有显示哪个字段有错误。我的gsp表单有render错误标记,每个字段也有.Error。
def update (){
def myProfile = new Profile()
dataBind(params, myProfile, include:[.....])
if (myProfile.hasErrors()){
respond myUser.errors, view: 'editProfile'
return
}
else{
redirect(action:"viewProfile")
}
}
我的gsp是这样的,我现在无法访问我的家用电脑发布确切的gsp页面:
<g:hasErrors bean="${myUser}" field="name">
<g:message error="${myUser.errors.getFieldErrors("name")}" />
</g:hasErrors>
答案 0 :(得分:2)
不是尝试传回错误,而是将整个对象传递给gsp。假设你的gsp页面中的bean是'myUser',那么这就是render语句应该是:
render view: 'editProfile', model: [myUser: myProfile]
答案 1 :(得分:0)
使用Application.Helper.Controller
时,将基于类名而不是变量名来调用Bean。
在您的情况下,您需要像respond bean.errors
一样访问gep中的bean:
profile
如果需要使用//variable in controller
def myProfile = new Profile()
//bean in views
<g:hasErrors bean="${profile}" field="name">
<g:message error="${profile.errors.getFieldErrors("name")}" />
</g:hasErrors>
之类的特定名称调用bean,只需在模型中传递它即可:
myUser