BAEL-112 - custom validator - format fixes
This commit is contained in:
parent
f8bd663f7d
commit
42f1ef0bf3
@ -11,12 +11,14 @@ import javax.validation.Payload;
|
|||||||
|
|
||||||
@Documented
|
@Documented
|
||||||
@Constraint(validatedBy = ContactNumberValidator.class)
|
@Constraint(validatedBy = ContactNumberValidator.class)
|
||||||
@Target( { ElementType.METHOD, ElementType.FIELD })
|
@Target({ElementType.METHOD, ElementType.FIELD})
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
public @interface ContactNumberConstraint {
|
public @interface ContactNumberConstraint {
|
||||||
|
|
||||||
String message() default "Invalid phone number";
|
String message() default "Invalid phone number";
|
||||||
|
|
||||||
Class<?>[] groups() default {};
|
Class<?>[] groups() default {};
|
||||||
|
|
||||||
Class<? extends Payload>[] payload() default {};
|
Class<? extends Payload>[] payload() default {};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -6,14 +6,15 @@ import javax.validation.ConstraintValidatorContext;
|
|||||||
public class ContactNumberValidator implements ConstraintValidator<ContactNumberConstraint, String> {
|
public class ContactNumberValidator implements ConstraintValidator<ContactNumberConstraint, String> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void initialize(ContactNumberConstraint contactNumber) {}
|
public void initialize(ContactNumberConstraint contactNumber) {
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isValid(String contactField, ConstraintValidatorContext cxt) {
|
public boolean isValid(String contactField, ConstraintValidatorContext cxt) {
|
||||||
if(contactField == null) {
|
if (contactField == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return contactField.matches("[0-9]+") && (contactField.length() > 8) && (contactField.length() < 14);
|
return contactField.matches("[0-9]+") && (contactField.length() > 8) && (contactField.length() < 14);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@ package com.baeldung.model;
|
|||||||
import com.baeldung.customvalidator.ContactNumberConstraint;
|
import com.baeldung.customvalidator.ContactNumberConstraint;
|
||||||
|
|
||||||
public class ValidatedPhone {
|
public class ValidatedPhone {
|
||||||
|
|
||||||
@ContactNumberConstraint
|
@ContactNumberConstraint
|
||||||
private String phone;
|
private String phone;
|
||||||
|
|
||||||
@ -15,4 +15,8 @@ public class ValidatedPhone {
|
|||||||
this.phone = phone;
|
this.phone = phone;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return phone;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,21 +15,21 @@ import com.baeldung.model.ValidatedPhone;
|
|||||||
@EnableWebMvc
|
@EnableWebMvc
|
||||||
public class ValidatedPhoneController {
|
public class ValidatedPhoneController {
|
||||||
|
|
||||||
@RequestMapping(value="/validatePhone", method=RequestMethod.GET)
|
@RequestMapping(value = "/validatePhone", method = RequestMethod.GET)
|
||||||
public String loadFormPage(Model m) {
|
public String loadFormPage(Model m) {
|
||||||
m.addAttribute("validatedPhone", new ValidatedPhone());
|
m.addAttribute("validatedPhone", new ValidatedPhone());
|
||||||
return "phoneHome";
|
return "phoneHome";
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(value="/addValidatePhone", method=RequestMethod.POST)
|
@RequestMapping(value = "/addValidatePhone", method = RequestMethod.POST)
|
||||||
public String submitForm(@Valid ValidatedPhone validatedPhone, BindingResult result, Model m) {
|
public String submitForm(@Valid ValidatedPhone validatedPhone, BindingResult result, Model m) {
|
||||||
if(result.hasErrors()) {
|
if (result.hasErrors()) {
|
||||||
return "phoneHome";
|
|
||||||
}
|
|
||||||
|
|
||||||
m.addAttribute("message", "Successfully saved phone: " + validatedPhone.toString());
|
|
||||||
return "phoneHome";
|
return "phoneHome";
|
||||||
|
}
|
||||||
|
|
||||||
|
m.addAttribute("message", "Successfully saved phone: " + validatedPhone.toString());
|
||||||
|
return "phoneHome";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user