如何检查数字输入是否为空?

问题描述

我使用 spring boot 验证器验证的表单中有这个输入字段:

<input class="form-control" th:field="*{numericField}"
    type="number" min="0" id="numeric_field_input">

有效范围均为正数。但即使我不填写任何数字,该字段也会生效。我相信认值为零。添加类似

th:default="-1"

没有解决问题。如何在服务器端检查用户输入任何值?

编辑:这些是我当前对该字段的注释:

   @Positive(message = "{validation.numericField.positive}")
   @ColumnDefault("0")
   private Integer numericField;

编辑2: 这是我修复它的最终方法

    @NotNull(message = "{validation.numericField.positive}")
    @Positive(message = "{validation.numericField.positive}")
    private Integer numericField;

解决方法

您可以使用这两个验证

@NotNull("message": "numericField: positive number value is required")

@Min(value=0,message="numericField: positive number,min 0 is required")
,

您可以使用 min 约束来限制值只能是正数

//import javax.validation.constraints.Min;

@Min(value = 0,message = "numericField not be negative")
 private Integer numericField;

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...