服务方法中的 Grails 验证错误

发布于 2024-11-15 20:40:50 字数 319 浏览 5 评论 0原文

我在服务类中有一个创建对象的方法:

def createContent (fileName, description) {

    def content = new Content(
        fileName:fileName,
        description:description,
    ).save()
}

这些属性都不可为空。如何将验证错误传回并显示?我尝试过 flash.message 和 render,这两者都无法在服务类中工作。我也尝试过 .save(failOnError:true) 它显示了一长串错误。

I have a method in a service class that creates an object:

def createContent (fileName, description) {

    def content = new Content(
        fileName:fileName,
        description:description,
    ).save()
}

Neither of those properties are nullable. How can I pass the validation errors back to be displayed? I've tried flash.message and render, both of which don't work from within service classes. I also tried
.save(failOnError:true)
which displayed a long list of errors.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

我的影子我的梦 2024-11-22 20:40:50

简化一切它应该看起来像这样。

服务方法:

def createContent (fileName, description) {
    //creating an object to save
    def content = new Content(
        fileName:fileName,
        description:description,
    )

    //saving the object
    //if saved then savedContent is saved domain with generated id
    //if not saved then savedContent is null and content has validation information inside
    def savedContent = content.save()

    if (savedContent != null) {
        return savedContent
    } else {
        return content
    }
}

现在在控制器中:

def someAction = {
    ...
    def content = someService.createContent (fileName, description)
    if (content.hasErrors()) {
        //not saved
        //render create page once again and use content object to render errors
        render(view:'someAction', model:[content:content])
    } else {
        //saved
        //redirect to show page or something
        redirect(action:'show', model:[id:content.id])
    }
}

和 someAction.gsp:

<g:hasErrors bean="${content}">
   <g:renderErrors bean="${content}" as="list" />
</g:hasErrors>

一般来说,您应该查看以下内容: Grails 验证文档

Simplifying everything it should look like this.

Service method:

def createContent (fileName, description) {
    //creating an object to save
    def content = new Content(
        fileName:fileName,
        description:description,
    )

    //saving the object
    //if saved then savedContent is saved domain with generated id
    //if not saved then savedContent is null and content has validation information inside
    def savedContent = content.save()

    if (savedContent != null) {
        return savedContent
    } else {
        return content
    }
}

Now in controller:

def someAction = {
    ...
    def content = someService.createContent (fileName, description)
    if (content.hasErrors()) {
        //not saved
        //render create page once again and use content object to render errors
        render(view:'someAction', model:[content:content])
    } else {
        //saved
        //redirect to show page or something
        redirect(action:'show', model:[id:content.id])
    }
}

And someAction.gsp:

<g:hasErrors bean="${content}">
   <g:renderErrors bean="${content}" as="list" />
</g:hasErrors>

And in general you should look through this: Grails validation doc

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