Grails 表单错误处理:绑定 1:1 对象映射
我有一个注册表单,其中包含与两个域对象相关的字段;用户和个人资料。该关系是 User 域类拥有的 1:1 映射。
用户控制器上的“注册”操作会整理表单值,并且如果没有验证错误,则会保留用户对象并在提交表单时重定向到应用程序根目录。否则,控制器将重定向回注册表,显示带有失败值的预填充字段。
但是,实际上,当验证失败时,失败的值不会显示在视图中。下面是注册操作的代码:
def registration = {
}
def register = {
def user = new User()
bindData(user, params)
if (user.save()) {
flash.message = 'Successfully Registered User'
redirect(uri: '/')
}else {
flash.message = 'Registration Failed!'
redirect(action: registration, params: [ user: user ])
}
}
下面是从显示用户和个人资料相关字段的视图中摘录的示例 html:
<div class="row">
<label for="city"> City, State: </label>
<g:textField id="city" name="profile.city"
value="${user?.profile?.city}" size="28" />
<span class="red">*</span>
</div>
<hr />
<div class="row">
<label for="email"> E-mail address: </label>
<g:textField id="email" name="userId" value="${user?.userId}" size="28" />
<span class="red">*</span>
</div>
从语法上看,一切看起来都不错;我使用适当的命名约定和 grail 的插值来访问值,所以我对为什么这没有按预期运行感到束手无策。
任何意见或建议将不胜感激。
谢谢, -汤姆
I have a registration form that contains fields related to two domain objects; User and Profile. The relationship is a 1:1 mapping owned by the User domain class.
A 'register' action on the User controller marshals the form values, and provided there are no validation errors, persists the user object and redirects to the applications root when the form is submitted. Otherwise, the controller will redirect back to the registration form showing pre-populated fields with failed values.
However, in practice, when validation fails, the failed values aren't displayed in the view. Below is the code for the register action:
def registration = {
}
def register = {
def user = new User()
bindData(user, params)
if (user.save()) {
flash.message = 'Successfully Registered User'
redirect(uri: '/')
}else {
flash.message = 'Registration Failed!'
redirect(action: registration, params: [ user: user ])
}
}
Below is an example html excerpt from the view showing User and Profile related fields:
<div class="row">
<label for="city"> City, State: </label>
<g:textField id="city" name="profile.city"
value="${user?.profile?.city}" size="28" />
<span class="red">*</span>
</div>
<hr />
<div class="row">
<label for="email"> E-mail address: </label>
<g:textField id="email" name="userId" value="${user?.userId}" size="28" />
<span class="red">*</span>
</div>
Syntactically, everthing looks okay; I'm using appropriate naming conventions and grail's interpolation for acessing values, so I'm at wits end as to why this isn't behaving as expected.
Any comments or suggestions would be appreciated.
Thanks,
-Tom
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果我没记错的话,我认为这是这样的:
If i remember correctly i thought it was something in the lines of:
您需要以某种方式将提交的值从
register
操作中的user
传递到registration
操作中的user
。像这样:You need to somehow pass the submitted values from
user
inregister
action touser
inregistration
action. Like this:尝试显式调用错误?
我一直在使用这种模式重定向回相同的表单。
我通常在网络流中使用命令对象,所以我的正常模式如下:
Try explicitly calling the erorr?
Ive been using this pattern to redirect back to the same form.
I normally use command objects in webflows, so my normal pattern looks like: