Spring MVC Custom Validator
This commit is contained in:
+22
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.customvalidator;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import javax.validation.Constraint;
|
||||
import javax.validation.Payload;
|
||||
|
||||
@Documented
|
||||
@Constraint(validatedBy = ContactNumberValidator.class)
|
||||
@Target( { ElementType.METHOD, ElementType.FIELD })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface ContactNumberConstraint {
|
||||
|
||||
String message() default "Invalid phone number";
|
||||
Class<?>[] groups() default {};
|
||||
Class<? extends Payload>[] payload() default {};
|
||||
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.customvalidator;
|
||||
|
||||
import javax.validation.ConstraintValidator;
|
||||
import javax.validation.ConstraintValidatorContext;
|
||||
|
||||
public class ContactNumberValidator implements ConstraintValidator<ContactNumberConstraint, String> {
|
||||
|
||||
@Override
|
||||
public void initialize(ContactNumberConstraint 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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.model;
|
||||
|
||||
import com.baeldung.customvalidator.ContactNumberConstraint;
|
||||
|
||||
public class ValidatedPhone {
|
||||
|
||||
@ContactNumberConstraint
|
||||
private String phone;
|
||||
|
||||
public String getPhone() {
|
||||
return phone;
|
||||
}
|
||||
|
||||
public void setPhone(String phone) {
|
||||
this.phone = phone;
|
||||
}
|
||||
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
package com.baeldung.web.controller;
|
||||
|
||||
import javax.validation.Valid;
|
||||
|
||||
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.servlet.config.annotation.EnableWebMvc;
|
||||
|
||||
import com.baeldung.model.ValidatedPhone;
|
||||
|
||||
@Controller
|
||||
@EnableWebMvc
|
||||
public class ValidatedPhoneController {
|
||||
|
||||
@RequestMapping(value="/validatePhone", method=RequestMethod.GET)
|
||||
public String loadFormPage(Model m) {
|
||||
m.addAttribute("validatedPhone", new ValidatedPhone());
|
||||
return "phoneHome";
|
||||
}
|
||||
|
||||
@RequestMapping(value="/addValidatePhone", method=RequestMethod.POST)
|
||||
public String submitForm(@Valid ValidatedPhone validatedPhone, BindingResult result, Model m) {
|
||||
if(result.hasErrors()) {
|
||||
return "phoneHome";
|
||||
}
|
||||
|
||||
m.addAttribute("message", "Successfully saved phone: " + validatedPhone.toString());
|
||||
return "phoneHome";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user