3

I want to create custom email validator using annotations. This solution was useful while creating the validator. Here's the annotation :

@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE,           ElementType.CONSTRUCTOR, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {CommonsEmailValidator.class})
@Documented
@ReportAsSingleViolation
public @interface ExEmailValidator {

String message() default "        {org.hibernate.validator.constraints.Email.message}";

Class<?>[] groups() default {};

Class<? extends Payload>[] payload() default {};

@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE,          ElementType.CONSTRUCTOR, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface List {
ExEmailValidator[] value();
}
}

And here's the class CommonsEmailValidator :

public class CommonsEmailValidator implements         ConstraintValidator<ExEmailValidator, String> {
private static final boolean ALLOW_LOCAL = false;
private EmailValidator realValidator =        EmailValidator.getInstance(ALLOW_LOCAL);

@Override
public void initialize(ExEmailValidator email) {
// TODO Auto-generated method stub  
}
@Override
public boolean isValid(String email, ConstraintValidatorContext         constraintValidatorContext) {
 if( email == null ) return true;
 return realValidator.isValid(email);
 }

 }

I run the project, but, when I click submit on the registration form, while the email format is not valid I have the following exception :

Request processing failed; nested exception is javax.validation.ConstraintViolationException: Validation failed for classes [...] during persist time for groups [javax.validation.groups.Default, ]

error 500

the exception was as excpected, But instead of showing a 500 error, I would like to see an error message explaning what's wrong. So, I think I have to use ConstraintValidatorContext to define custom error messages. But, I don't know how to do it. Any ideas please ?

Community
  • 1
  • 1
Najoua
  • 391
  • 1
  • 7
  • 15
  • And your problem is? There is an exception because it is invalid, as you would expect, so I don't see the problem you have. One thing is you have a lot of leading spaces in the `message` value on your annotation. – M. Deinum May 09 '16 at 11:00
  • Yes, the exception was as excpected, But instead of showing a 500 error, I would like to see an error message explaning what's wrong. and that doesn't allow the user to click submit until he enter a well formed email – Najoua May 09 '16 at 11:11
  • Validation is done on the server, not on the client, so you will have to press submit to trigger the validation. – M. Deinum May 09 '16 at 11:17
  • Ah okay. and what about the cutom error message? please check the post I edited it. – Najoua May 09 '16 at 11:26
  • Assuming you are using Spring MVC, add `@Valid` to the method argument you want to validate. – M. Deinum May 09 '16 at 11:34
  • Do you mean, in the controller ? I should add @Valid to a certain email argument there ? – Najoua May 09 '16 at 12:34
  • No to your model object ... I assume you are using binding to an object and not passing in regular argument.s – M. Deinum May 09 '16 at 12:44
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/111432/discussion-between-najoua-and-m-deinum). – Najoua May 09 '16 at 13:10

1 Answers1

1

You should use @ControllerAdvice and handle validation exceptions there with @ExceptionHandler(MethodArgumentNotValidException.class) for example.

Jack Pettinger
  • 2,715
  • 1
  • 23
  • 37
Ivan
  • 11
  • 1