ContactNumberValidator refactor (#1491)
* ContactNumberValidator refactor * Refactor controllers
This commit is contained in:
parent
21f9df6330
commit
9b48f77c8d
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
|
|
|
@ -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";
|
||||
|
|
Loading…
Reference in New Issue