DropDownChoice - 未选择任何内容,值 -1?
我的表单上有一个 DropDownChoice 组件,当提交表单并且未从 DropDownChoice 中选择任何内容时,返回的默认值为“-1”。有办法改变这种行为吗?
I have a DropDownChoice component on my Form and when the form is submitted and nothing is selected from the DropDownChoice, the default value that's returned is "-1". Is there are a way to change this behavior?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此行为由
AbstractSingleSelectChoice
中的常量字段控制,该常量字段是
DropDownChoice
的超类。您无法在子类中更改此值,因此为了更改使用的值,您必须找到此常量的使用并重写使用它的方法以使用其他默认值。
这样做会有风险,尽管这是可能的。我知道您必须至少重写
getDefaultChoice(final Object selected)
和getModelValue()
。你为什么要这样做?就其目的而言,这是一个合理的值。
也许有更好的方法来满足您的潜在需求。
This behavior is controlled by a constant field
in
AbstractSingleSelectChoice
, which is a superclass ofDropDownChoice
.You can't change this value in a subclass, so in order to change the value used, you would have to locate uses of this constant and override the methods that use it to use some other default.
Doing that would be risky, though it's likely possible. I know you'd have to override at least
getDefaultChoice(final Object selected)
andgetModelValue()
.Why do you wish to do this? It's a sensible value for its purpose.
Perhaps there's a better approach to accomplishing your underlying need.
挂钩 beforeSubmit,然后更改您想要的内容。
我认为有这样的值是很好的,因为第一个元素是0,notting意味着-1。
Hook to the beforeSubmit, and change what you want.
I think it is good to have such a value, Since first element is 0, notting means -1.