Grails 表单错误处理:绑定 1:1 对象映射

发布于 2024-11-04 02:07:31 字数 1196 浏览 6 评论 0原文

我有一个注册表单,其中包含与两个域对象相关的字段;用户和个人资料。该关系是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

错々过的事 2024-11-11 02:07:31

如果我没记错的话,我认为这是这样的:

def user = new User()
user.properties = params

If i remember correctly i thought it was something in the lines of:

def user = new User()
user.properties = params
人事已非 2024-11-11 02:07:31

您需要以某种方式将提交的值从 register 操作中的 user 传递到 registration 操作中的 user。像这样:

if (params.user) {
  user.properties = params.user.properties
}

You need to somehow pass the submitted values from user in register action to user in registration action. Like this:

if (params.user) {
  user.properties = params.user.properties
}
稀香 2024-11-11 02:07:31

尝试显式调用错误?

我一直在使用这种模式重定向回相同的表单。

if (user.save()) {
...
} else {
    return error()
}

我通常在网络流中使用命令对象,所以我的正常模式如下:

def registerFlow = {
    registerPage = {
        on("submit") { FormDataCommand cmd ->
            cmd.validate()
            if (cmd.hasErrors()) {
               flow.cmd = cmd
               return error()
            } else {
               ...
            }
        }
    }
}

class FormDataCommand implements Serializable {
    User u
    Profile p

    static constraints = {
        u(validator: { it.validate() })
        p(validator: { it.validate() })
    }  

}

Try explicitly calling the erorr?

Ive been using this pattern to redirect back to the same form.

if (user.save()) {
...
} else {
    return error()
}

I normally use command objects in webflows, so my normal pattern looks like:

def registerFlow = {
    registerPage = {
        on("submit") { FormDataCommand cmd ->
            cmd.validate()
            if (cmd.hasErrors()) {
               flow.cmd = cmd
               return error()
            } else {
               ...
            }
        }
    }
}

class FormDataCommand implements Serializable {
    User u
    Profile p

    static constraints = {
        u(validator: { it.validate() })
        p(validator: { it.validate() })
    }  

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文