I have this input field in a form which I validate using spring boot validator:
<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
//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:
This is the final way how [sic] I fixed it:
@NotNull(message = "{validation.numericField.positive}") @Positive(message = "{validation.numericField.positive}") private Integer numericField;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.