added email valication helper
This commit is contained in:
@ -30,6 +30,13 @@
|
||||
<artifactId>hibernate-core</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.primefaces</groupId>
|
||||
<artifactId>primefaces</artifactId>
|
||||
<version>6.0</version>
|
||||
<type>jar</type>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax</groupId>
|
||||
<artifactId>javaee-api</artifactId>
|
||||
|
||||
@ -0,0 +1,31 @@
|
||||
package de.muehlencord.shared.jeeutil.validator;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
|
||||
import static java.lang.annotation.ElementType.FIELD;
|
||||
import static java.lang.annotation.ElementType.METHOD;
|
||||
import java.lang.annotation.Retention;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
import java.lang.annotation.Target;
|
||||
import javax.validation.Constraint;
|
||||
import javax.validation.Payload;
|
||||
import org.primefaces.validate.bean.ClientConstraint;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Joern Muehlencord <joern at muehlencord.de>
|
||||
*/
|
||||
@Target({METHOD, FIELD, ANNOTATION_TYPE})
|
||||
@Retention(RUNTIME)
|
||||
@Constraint(validatedBy = EmailConstraintValidator.class)
|
||||
@ClientConstraint(resolvedBy = EmailClientValidationConstraint.class)
|
||||
@Documented
|
||||
public @interface Email {
|
||||
|
||||
String message() default "{de.muehlencord.shared.jeeutil.validator.Email}";
|
||||
|
||||
Class<?>[] groups() default {};
|
||||
|
||||
Class<? extends Payload>[] payload() default {};
|
||||
|
||||
}
|
||||
@ -0,0 +1,33 @@
|
||||
package de.muehlencord.shared.jeeutil.validator;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import javax.validation.metadata.ConstraintDescriptor;
|
||||
import org.primefaces.validate.bean.ClientValidationConstraint;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Joern Muehlencord <joern at muehlencord.de>
|
||||
*/
|
||||
public class EmailClientValidationConstraint implements ClientValidationConstraint {
|
||||
|
||||
public static final String MESSAGE_METADATA = "data-p-email-msg";
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getMetadata(ConstraintDescriptor constraintDescriptor) {
|
||||
Map<String, Object> metadata = new HashMap<>();
|
||||
Map attrs = constraintDescriptor.getAttributes();
|
||||
Object message = attrs.get("message");
|
||||
if (message != null) {
|
||||
metadata.put(MESSAGE_METADATA, message);
|
||||
}
|
||||
|
||||
return metadata;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getValidatorId() {
|
||||
return Email.class.getSimpleName();
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,33 @@
|
||||
package de.muehlencord.shared.jeeutil.validator;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
import javax.validation.ConstraintValidator;
|
||||
import javax.validation.ConstraintValidatorContext;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Joern Muehlencord <joern at muehlencord.de>
|
||||
*/
|
||||
public class EmailConstraintValidator implements ConstraintValidator<Email, String> {
|
||||
|
||||
private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\."
|
||||
+ "[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*"
|
||||
+ "(\\.[A-Za-z]{2,})$";
|
||||
|
||||
private Pattern pattern;
|
||||
|
||||
@Override
|
||||
public void initialize(Email a) {
|
||||
pattern = Pattern.compile(EMAIL_PATTERN);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid(String value, ConstraintValidatorContext cvc) {
|
||||
if (value == null) {
|
||||
return true;
|
||||
} else {
|
||||
return pattern.matcher(value).matches();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright 2017 Joern Muehlencord <joern at muehlencord.de>.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package de.muehlencord.shared.jeeutil.validator;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import javax.faces.application.FacesMessage;
|
||||
import javax.faces.component.UIComponent;
|
||||
import javax.faces.context.FacesContext;
|
||||
import javax.faces.validator.FacesValidator;
|
||||
import javax.faces.validator.Validator;
|
||||
import javax.faces.validator.ValidatorException;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Joern Muehlencord <joern at muehlencord.de>
|
||||
*/
|
||||
@FacesValidator("de.muehlencord.shared.jeeutil.validator.EmailValidator")
|
||||
public class EmailValidator implements Validator {
|
||||
|
||||
private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\."
|
||||
+ "[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*"
|
||||
+ "(\\.[A-Za-z]{2,})$";
|
||||
|
||||
private final Pattern pattern;
|
||||
private Matcher matcher;
|
||||
|
||||
public EmailValidator() {
|
||||
pattern = Pattern.compile(EMAIL_PATTERN);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
|
||||
matcher = pattern.matcher(value.toString());
|
||||
if (!matcher.matches()) {
|
||||
// String summary = context.getApplication().evaluateExpressionGet(context, "#{msgs.email_validation_failed}", String.class);
|
||||
// String detail = context.getApplication().evaluateExpressionGet(context, "#{msgs.email_validation_failed_detail}", String.class);
|
||||
// FacesMessage msg = new FacesMessage(summary, detail);
|
||||
|
||||
FacesMessage msg = new FacesMessage("E-mail validation failed.","Invalid E-mail format.");
|
||||
|
||||
msg.setSeverity(FacesMessage.SEVERITY_ERROR);
|
||||
throw new ValidatorException(msg);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user