[英]How do I validate an attribute of type BigDecimal using Bean Validation annotation?
I'm just wanting to do a simple validation, if the html fields below are sent "null or empty" the user can't persist the data.我只是想做一个简单的验证,如果下面的 html 字段被发送“空或空”,则用户无法保存数据。 How can I solve this problem?我怎么解决这个问题?
below my code在我的代码下面
HTML: HTML:
<div th:each="msgErrors : ${erros}" class="alert alert-danger alert-dismissible fade show" role="alert">
<span th:text="${msgErrors}"></span>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form th:action="cadastrar" method="post" th:object="${fatura}">
<div class="row">
<div class="col-lg-4 col-sm-12">
<label for="cofins">COFINS:</label>
<input type="text" class="form-control" autocomplete="off" id="cofins" th:field="*{cofins}" placeholder="0,00 %">
</div>
<div class="col-lg-4 col-sm-12">
<label for="icms">ICMS:</label>
<input type="text" class="form-control" autocomplete="off" id="icms" th:field="*{icms}" placeholder="0,00 %">
</div>
</div>
<div class="row mt-4">
<button type="submit" class="btn btn-success mr-3"><i class="far fa-save"></i> SALVAR</button>
</div>
</form>
My Model Class: @Entity我的 Model Class:@Entity
public class Fatura {
@NumberFormat(pattern = "#,###.##")
@Column(name = "cofins")
private BigDecimal cofins;
@NumberFormat(pattern = "#,###.##")
@Column(name = "icms")
private BigDecimal icms;
}
My Controller:我的 Controller:
public ModelAndView cadastro(@Valid Fatura fatura, BindingResult br){
if(br.hasErrors()){
ModelAndView mv = new ModelAndView("cadastro/cadastro");
mv.addObject("fatura", fatura);
mv.addObject("listLojas", Lojas.values());
List<String> msg = new ArrayList<String>();
for(ObjectError objError : br.getAllErrors()){
msg.add(objError.getDefaultMessage());
}
mv.addObject("erros", msg);
return mv;
}
I need the user to not be allowed to send the fields without typing anything我需要不允许用户在不输入任何内容的情况下发送字段
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.