Skip to content

Commit 9b48f77

Browse files
authored
ContactNumberValidator refactor (eugenp#1491)
* ContactNumberValidator refactor * Refactor controllers
1 parent 21f9df6 commit 9b48f77

3 files changed

Lines changed: 11 additions & 14 deletions

File tree

‎spring-mvc-java/src/main/java/com/baeldung/customvalidator/ContactNumberValidator.java‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ public void initialize(ContactNumberConstraint contactNumber) {
1111

1212
@Override
1313
public boolean isValid(String contactField, ConstraintValidatorContext cxt) {
14-
if (contactField == null) {
15-
return false;
16-
}
17-
return contactField.matches("[0-9]+") && (contactField.length() > 8) && (contactField.length() < 14);
14+
return contactField != null && contactField.matches("[0-9]+") && (contactField.length() > 8) && (contactField.length() < 14);
1815
}
1916

2017
}

‎spring-mvc-java/src/main/java/com/baeldung/web/controller/UserController.java‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
import com.baeldung.model.User;
44
import org.springframework.stereotype.Controller;
55
import org.springframework.ui.Model;
6+
import org.springframework.web.bind.annotation.GetMapping;
67
import org.springframework.web.bind.annotation.ModelAttribute;
8+
import org.springframework.web.bind.annotation.PostMapping;
79
import org.springframework.web.bind.annotation.RequestMapping;
8-
import org.springframework.web.bind.annotation.RequestMethod;
910

1011
@Controller
1112
@RequestMapping("/")
1213
public class UserController {
1314

14-
@RequestMapping(value = "/", method = RequestMethod.GET)
15+
@GetMapping("/")
1516
public String showForm(final Model model) {
1617
final User user = new User();
1718
user.setFirstname("John");
@@ -21,7 +22,7 @@ public String showForm(final Model model) {
2122
return "index";
2223
}
2324

24-
@RequestMapping(value = "/processForm", method = RequestMethod.POST)
25+
@PostMapping("/processForm")
2526
public String processForm(@ModelAttribute(value = "user") final User user, final Model model) {
2627
// Insert User into DB
2728
model.addAttribute("name", user.getFirstname() + " " + user.getLastname());

‎spring-mvc-java/src/main/java/com/baeldung/web/controller/ValidatedPhoneController.java‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
11
package com.baeldung.web.controller;
22

3-
import javax.validation.Valid;
4-
3+
import com.baeldung.model.ValidatedPhone;
54
import org.springframework.stereotype.Controller;
65
import org.springframework.ui.Model;
76
import org.springframework.validation.BindingResult;
8-
import org.springframework.web.bind.annotation.RequestMapping;
9-
import org.springframework.web.bind.annotation.RequestMethod;
7+
import org.springframework.web.bind.annotation.GetMapping;
8+
import org.springframework.web.bind.annotation.PostMapping;
109
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
1110

12-
import com.baeldung.model.ValidatedPhone;
11+
import javax.validation.Valid;
1312

1413
@Controller
1514
@EnableWebMvc
1615
public class ValidatedPhoneController {
1716

18-
@RequestMapping(value = "/validatePhone", method = RequestMethod.GET)
17+
@GetMapping("/validatePhone")
1918
public String loadFormPage(Model m) {
2019
m.addAttribute("validatedPhone", new ValidatedPhone());
2120
return "phoneHome";
2221
}
2322

24-
@RequestMapping(value = "/addValidatePhone", method = RequestMethod.POST)
23+
@PostMapping("/addValidatePhone")
2524
public String submitForm(@Valid ValidatedPhone validatedPhone, BindingResult result, Model m) {
2625
if (result.hasErrors()) {
2726
return "phoneHome";

0 commit comments

Comments
 (0)