ContactNumberValidator refactor (#1491)

* ContactNumberValidator refactor

* Refactor controllers
This commit is contained in:
Grzegorz Piwowarek 2017-03-25 11:47:14 +01:00 committed by GitHub
parent 21f9df6330
commit 9b48f77c8d
3 changed files with 11 additions and 14 deletions

View File

@ -11,10 +11,7 @@ public class ContactNumberValidator implements ConstraintValidator<ContactNumber
@Override
public boolean isValid(String contactField, ConstraintValidatorContext cxt) {
if (contactField == null) {
return false;
}
return contactField.matches("[0-9]+") && (contactField.length() > 8) && (contactField.length() < 14);
return contactField != null && contactField.matches("[0-9]+") && (contactField.length() > 8) && (contactField.length() < 14);
}
}

View File

@ -3,15 +3,16 @@ package com.baeldung.web.controller;
import com.baeldung.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/")
public class UserController {
@RequestMapping(value = "/", method = RequestMethod.GET)
@GetMapping("/")
public String showForm(final Model model) {
final User user = new User();
user.setFirstname("John");
@ -21,7 +22,7 @@ public class UserController {
return "index";
}
@RequestMapping(value = "/processForm", method = RequestMethod.POST)
@PostMapping("/processForm")
public String processForm(@ModelAttribute(value = "user") final User user, final Model model) {
// Insert User into DB
model.addAttribute("name", user.getFirstname() + " " + user.getLastname());

View File

@ -1,27 +1,26 @@
package com.baeldung.web.controller;
import javax.validation.Valid;
import com.baeldung.model.ValidatedPhone;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import com.baeldung.model.ValidatedPhone;
import javax.validation.Valid;
@Controller
@EnableWebMvc
public class ValidatedPhoneController {
@RequestMapping(value = "/validatePhone", method = RequestMethod.GET)
@GetMapping("/validatePhone")
public String loadFormPage(Model m) {
m.addAttribute("validatedPhone", new ValidatedPhone());
return "phoneHome";
}
@RequestMapping(value = "/addValidatePhone", method = RequestMethod.POST)
@PostMapping("/addValidatePhone")
public String submitForm(@Valid ValidatedPhone validatedPhone, BindingResult result, Model m) {
if (result.hasErrors()) {
return "phoneHome";