Why javax validation not working in DTO class?

Kiran Subedi :

I am trying to learn the spring boot. And, I am stuck on the form validation process. I have followed the process as instructed here.

Here is my controller class

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class TalentCategoryController {

    @GetMapping("talent-category")
    public ModelAndView create(CreateTalentCategoryRequest talentCategory) {
        ModelAndView model = new ModelAndView();
        model.setViewName("talent-category/create");
        model.addObject("talentCategory", talentCategory);
        return model ; 
    }

    @Autowired
    TalentCategoryService talentCategoryService ; 
     
    @RequestMapping(path="talent-category", method = RequestMethod.POST, consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
    public ModelAndView store(@Valid @ModelAttribute CreateTalentCategoryRequest talentCategory, BindingResult result) {
        // result.hasErrors is false
        if(result.hasErrors()) { 
            System.out.println("Validation working");
            ModelAndView model = new ModelAndView();            
            model.setViewName("talent-category/create");
            return model; 
        }
        System.out.println("Validation not working");
        talentCategoryService.store();
        return null ; 
    }
     
}

DTO class :

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import lombok.Data;

@Data
public class CreateTalentCategoryRequest {
    
    @NotBlank(message="Cannot be empty")
    @Size(min=10, max=30)
    private String name ; 
    
    @NotBlank(message="Cannot be empty")
    private String status  ; 

    @NotBlank(message="Cannot be empty")
    private String approved ; 
    
    @NotBlank(message="Cannot be empty")
    private String sort_order ;

}

View :

<form th:object="${talentCategory}" name="create-talent-category" method="POST" th:action="@{/talent-category}">
    <div class="row">
        <div class="col-4">
            <div class="form-group">
                <label for="name">Name</label>
                <input th:field="*{name}" type="text"  class="form-control form-control-sm" id="name" placeholder="Category Name" />
                <p class="alert alert-danger" th:if="${#fields.hasErrors('name')}" th:errors="*{name}"></p>
                
             </div>
        </div>
        
        <div class="col-2">
            <div class="form-group">
                <label for="sort_order">Sort Order</label>
                <input type="text" class="form-control form-control-sm" id="sort_order" placeholder="Eg : 1" />
             </div>
        </div>
        
        
        <div class="col-2">
            <div class="form-group">
                <label for="status">Status</label>
                 <select name="status" id="status" class="form-control form-control-sm">
                    <option selected>Choose...</option>
                    <option value="1">Active</option>
                    <option value="0">Passive</option>
                 </select>
             </div>
        </div>
        
        <div class="col-2">
            <div class="form-group">
                <label for="approved">Approved</label>
                 <select name="approved" id="approved" class="form-control form-control-sm">
                    <option selected>Choose...</option>
                    <option value="1">Yes</option>
                    <option value="0">No</option>
                 </select>
             </div>
        </div>
    </div>
    
    <div class="row">
        <div class="col-12">
            <button name="create" class="btn btn-sm btn-primary">Create</button>
        </div>
    </div>
 </form>

When a form is submitted with all the fields empty, the request is not redirect to the form(prints validation not working in console).

Achalveer :

if you are using spring version 2.3+ , make sure you have following dependency

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
</dependency>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Javax validation on nested objects - not working

javax validation api not working for pojo validation

Spring Boot Validation is not working with javax.validation

javax bean validation not working on method parameters

import javax.validation.constraints.NotEmpty; not working

import javax.validation.constraints.NotEmpty; not working

Javax Bean Validation : @Max and @Min is not working

Annotations from javax.validation.constraints not working

Javax validation not working for Hashmap with value as an Array or a List

Javax validation not working on boot rest controller parameter

Why is validation not working as intended?

Nest creating dto with class validator is not working

Why javascript validation not working properly?

Why is the pattern validation not working here?

javax.validation 2.0.1 List<Object> not working in spring boot

Annotations from javax.validation.constraints not working (ignored)

Why is the checking for Class not Working?

Why is this :class="{}" not working? vueJs

Validation a DTO with two entity

SpringBoot DTO Validation

Dto custom validation

ng-class validation is not working in AngularJS

Jquery validation working with class but not with rules and messages

Why is this form validation with dynamic input values not working?

why Vaadin 7 PasswordField validation not working

Why Javascript validation is not working in html form?

Why my regex not working for char validation

Validation is not working in Metronic UI. Why?

Why is data validation not working with entire list of times?