Here below the snapshot of my code, it doesn't show any error message to into the controller while I am inputting incorrect Values
While executing this code, it can be accessed by all of the inputted data, Like username as null or one character, email is not showing the proper types of email.
How can I solve my issue for data validation?
Controller class
@Controller
@RequestMapping("/registration")
public class UserRegistrationController {
@GetMapping
public String showRegistrationForm(@ModelAttribute("userRegistrationDto")UserRegistrationDto userRegistrationDto) {
return "registration";
}
@PostMapping
public String registerUserAccount(@Valid @ModelAttribute("userRegistrationDto") UserRegistrationDto userRegistrationDto, BindingResult result, Model model) {
System.out.println(userRegistrationDto);
model.addAttribute("data", userRegistrationDto);
if(result.hasErrors()){
System.out.println("error found");
return "registration";
}
UserRegistraionDto Class
public class UserRegistrationDto {
@NotBlank
private String name;
@Size(min = 3, max = 15)
private String username;
@Email(message = "Email is not Valid")
private String email;
@Size(min = 9, max = 11, message = "phone number must be 11 digit")
private String phone;
private String address;
private String password;
public UserRegistrationDto(String name, String username, String email, String phone, String address, String password) {
this.name = name;
this.username = username;
this.email = email;
this.phone = phone;
this.address = address;
this.password = password;
}
}
registration.html page
<div class="container regPage">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<!-- success message -->
<div th:if="${param.success}">
<div class="alert alert-info">You've successfully registered
to our awesome app!
</div>
</div>
<div class="text-center sign">
<h1>Registration Form</h1>
</div>
<form th:action="@{/registration}" method="post" th:object="${userRegistrationDto}">
<div class="form-group">
<label class="control-label" for="name"> Name </label>
<input id="name" name="name" class="form-control" th:field="*{name}"
required autofocus="autofocus"/>
</div>
<div class="form-group">
<label class="control-label" for="username"> User Name </label> <input
id="username" name="username" class="form-control" th:field="*{username}"
required autofocus="autofocus"/>
</div>
<div class="form-group">
<label class="control-label" for="email"> Email </label> <input
id="email" name="email" class="form-control" th:field="*{email}" required
autofocus="autofocus"/>
</div>
<div class="form-group">
<label class="control-label" for="phone"> Phone </label> <input
id="phone" name="phone" class="form-control" th:field="*{phone}"
required autofocus="autofocus"/>
</div>
<div class="form-group">
<label class="control-label" for="address"> Address </label> <input
id="address" name="address" class="form-control" th:field="*{address}"
required autofocus="autofocus"/>
</div>
<div class="form-group">
<label class="control-label" for="password"> Password </label> <input
id="password" name="password" class="form-control" type="password"
th:field="*{password}" required autofocus="autofocus"/>
</div>
<div class="form-group text-center">
<button type="submit" class="btn btn-success">Register</button>
</div>
</form>
<div class="text-center">
<span>Already registered? <a href="/" th:href="@{/login}"> Login here</a></span>
</div>
</div>
</div>
</div>
pom.xml file
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>