Grails 对 int 列的约束抛出 java.lang.IllegalArgumentException

发布于 2025-01-05 08:59:53 字数 840 浏览 2 评论 0原文

我对两个整数 Score 和 numOfQuestions 有以下约束。

score blank:false, min:1, nullable:false, notEqual:null
numOfQuestions blank:false, min:1, nullable:false, notEqual:null

可以看出,我希望这些字段大于 0 并且不应该为空或空白,但无论我做什么,而不是显示一条友好的消息,如“属性不能为空”,我的屏幕上会充满长异常。 .. 还有其他方法可以验证非空白输入的整数吗?

Failed to convert property value of type java.lang.String to required type int for property numOfQuestions; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [int] for property numOfQuestions: PropertyEditor [org.springframework.beans.propertyeditors.CustomNumberEditor] returned inappropriate value of type [null]

我看过这个 http://jira.grails.org/browse/GRAILS-4603但没有多大帮助

I have following constraints on two integers, score and numOfQuestions.

score blank:false, min:1, nullable:false, notEqual:null
numOfQuestions blank:false, min:1, nullable:false, notEqual:null

As it can be seen I want these fields to be greater then 0 and should not be null or blank but regardless of what I do, rather then showing a friendly message like "property can not be blank" my screen gets filled with long exception...
Is there any other way to validate integers for non blank inputs?

Failed to convert property value of type java.lang.String to required type int for property numOfQuestions; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [int] for property numOfQuestions: PropertyEditor [org.springframework.beans.propertyeditors.CustomNumberEditor] returned inappropriate value of type [null]

I have seen this http://jira.grails.org/browse/GRAILS-4603 but didn't help much

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

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

发布评论

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

评论(1

心碎的声音 2025-01-12 08:59:53

我认为您可以将上面的约束替换为:

score min:1
numOfQuestions min:1

因为 min:1 意味着 not-null。约束 blank: false 只能应用于 String 属性,因此您将此约束与数字属性一起使用可能会导致您看到的异常。

I think you can replace the constraints above with just:

score min:1
numOfQuestions min:1

Because min:1 implies not-null. The constraint blank: false can only be applied to String properties, so you're use of this constraint with a numeric property is probably responsible for the exception you're seeing.

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