简体   繁体   English

如何使用 Bean Validation 注解验证 BigDecimal 类型的属性?

[英]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">&times;</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我需要不允许用户在不输入任何内容的情况下发送字段

There are two methods you can use,您可以使用两种方法,

  • NotNull bean validation NotNull bean 验证
  • Constraint in your database for the fields as not nullable在数据库中将字段约束为不可为空

For more info about bean validation check this .有关 bean 验证的更多信息,请查看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SpringBoot Bean Validation注解 - SpringBoot Bean Validation annotation 如何在DAO中同时使用Spring验证注释(Bean验证规范) - How to use Spring Validation Annotation in DAO concurrently(Bean Validation Specification) 如何配置Spring以使用@RequestBody批注将JSON反序列化为BigDecimal - How to configure Spring to deserialize JSON into a BigDecimal using a @RequestBody annotation @Autowire 如何在不使用 @Bean 注释的情况下获取 spring bean - How @Autowire gets the spring bean without using @Bean annotation Bean验证-验证可选字段 - Bean validation - validate optional fields Java 8 个流。 如何将每个枚举类型的 BigDecimal 总计聚合到自定义 bean - Java 8 streams. How to aggregate BigDecimal totals per enumerated type into a custom bean 获得 UnsatisfiedDependency 异常(未找到符合类型的 bean)即使定义了 bean 并使用了 Service 注释 - Getting UnsatisfiedDependency exception(No qualifying bean of type found) even though bean is defined and using Service annotation 如何在 Spring 启动应用程序中使用 Hibernate 验证进行 Bean 验证? - How to do Bean Validation with Hibernate Validation in Spring Boot app? 如何禁用在 Spring 中使用 @Component 注释创建 bean? - How can I disable creating bean with @Component annotation in Spring? 在 `@Bean` 方法上使用 `@ConfigurationProperties` 注释 - Using `@ConfigurationProperties` annotation on `@Bean` Method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM