[英]How can I check if a number input is not empty?
I have this input field in a form which I validate using spring boot validator:我在一个表单中有这个输入字段,我使用 spring 引导验证器进行验证:
<input class="form-control" th:field="*{numericField}"
type="number" min="0" id="numeric_field_input">
The valid range is all positive numbers.有效范围均为正数。 But even if I do not fill in any number, the field validates.
但即使我不填写任何数字,该字段也会生效。 I believe the default value is zero.
我相信默认值为零。 Adding something like
添加类似的东西
th:default="-1"
did not solve the problem.没有解决问题。 How can I check on serverside that a user input any value?
如何在服务器端检查用户是否输入了任何值?
These are my current annotations of the field:这些是我目前对该领域的注释:
@Positive(message = "{validation.numericField.positive}")
@ColumnDefault("0")
private Integer numericField;
You can use these 2 validations您可以使用这两个验证
@NotNull("message": "numericField: positive number value is required")
@Min(value=0, message="numericField: positive number, min 0 is required")
You can use min
constraint to limit values be only positive numbers您可以使用
min
约束来限制值只能是正数
//import javax.validation.constraints.Min;
@Min(value = 0, message = "numericField not be negative")
private Integer numericField;
Migrating OP's ultimate solution from the question to an answer:将 OP 的最终解决方案从问题迁移到答案:
This is the final way how [sic] I fixed it:
这是 [原文如此] 我修复它的最终方法:
@NotNull(message = "{validation.numericField.positive}") @Positive(message = "{validation.numericField.positive}") private Integer numericField;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.