gpt4 book ai didi

grails - View 未检测到传入模型对象中的错误

转载 作者:行者123 更新时间:2023-12-02 15:59:21 25 4
gpt4 key购买 nike

Controller 中的代码如下:

for(o in options){

if(o){

if(!o.isInteger()){

don.errors.reject("The value was not integer")

render(view: "editdonation", model: [id:id, donation:don])
return

}

don.addToDenominations(o.toInteger())

}

}

我有意传入无效输入,以便执行don.errors.reject()代码。

editdonation.gsp具有以下代码以在捐赠对象中打印错误。
<ul class="inline-errors" role="alert">

<g:hasErrors bean="${donation}">

<div class="errors">

ok it has errors

</div>

</g:hasErrors>

</ul>

为什么 View 不显示错误?感谢您的帮助!

最佳答案

Errors.reject()方法需要错误代码而不是错误描述。

创建错误代码

要创建自己的错误代码:

  • 编辑文件grails-app / i18n / message.properties
  • 在错误代码中添加新行。例如: option.donation.notinteger =选项值{0}不是整数
  • 保存文件grails-app / i18n / message.properties

  • 使用错误代码

    要使用错误代码,请这样调用 Errors.reject():
    don.errors.reject('option.donation.notinteger', [o] as Object[], 'The value was not integer')

    上面的代码传递了拒绝选项,以便可以将值插入错误消息中。并根据需要提供后备错误消息。

    关于grails - View 未检测到传入模型对象中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32521223/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com