JSF 2.0, Custom Validations Defined outside the Application (Inside a Jboss Module) - Jboss AS 7/EAP 6.1
mhlulani May 18, 2018 6:14 PMI'm all out of ideas as to what I'm doing wrong. I have my custom validation in a jar called my-validation.jar, the reason I've packaged this as a separete jar is that. we need to use the same validations rules across all out applications (about 15 different apps)
package za.company.constraint;
...
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Retention(RetentionPolicy.RUNTIME)
@Target({
ElementType.FIELD,
ElementType.METHOD,
ElementType.PARAMETER})
@Documented
@Constraint(validatedBy = {Email.EmailValidator.class})
public @interface Email {
String message() default "You have entered an invalid email address";
Class[] groups() default {};
Class[] payload() default {};
class EmailValidator implements ConstraintValidator<email, string=""> {
Logger LOG = LoggerFactory.getLogger(EmailValidator.class);
@Override
public void initialize(Email constraintAnnotation) {
LOG.debug("INIT");
}
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
LOG.debug("VALIDATION: {}, CONTEXT: {}", value, context);
if (value == null) {
return false;
}
return value.matches("^[\\w.]+@[\\w]+([.]{1}[a-zA-Z]+){1}([.][a-zA-Z]+)?+$");
}
}
}
I created a Jboss 7 Module with this jar included:
/opt/jboss/7.1.1/modules/za/fnb/common/main/my-validation.jar (my jar)/opt/jboss/7.1.1/modules/za/fnb/common/main/module.xml (This module is referenced by the test-web.war in the META-INF/MANIFEST.MF or WEB-INF/jboss-deployment-structure.xml)
module.xml
I created a Web Application called test-web.war, consisting of a CID managed bean (Normal JSF Managed bean has the same issue) and a .xhtml page
User.java (CDI managed bean)
@Named
@RequestScoped
public class User {
private String firstName;
private String surname;
@Accepted //This is not being validated
private boolean accept;
@Email //This is not being validated
private String email;
@Pattern (regexp = "(MALE|FEMALE|M|F)", message = "Invalid gender supplied (Correct values include MALE, FEMALE, F, M)") //Validated ok
private String gender;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
//...
}
test.xhtml
... First Name Surname Gender Email I accept that all information is correct ...
The problem is that the @Email annotated field is NOT being validated, The only way I could make this work is to have the is to have the @Email definition in the same Source code (Inside the war) as the web application
test-web/src/main/java/za/company/constraint/Email.java test-web/src/main/java/za/company/bean/User.java test-web/src/main/webapp/test.xhtml test-web/src/main/webapp/WEB-INF/faces-config.xml test-web/src/main/webapp/WEB-INF/beans.xml test-web/src/main/webapp/WEB-INF/web.xml test-web/pom.xml
I've tried including this my-validation.jar inside the test-web.war/WEB-INF/lib/my-validation.jar with NO Luck.
I thought maybe I'm missing the implementation of the validation, so I packaged the hibernate-validate jars with my project, No Luck test-web.war/WEB-INF/lib/hibernate-validator-4.2.0.Final.jar test-web.war/WEB-INF/lib/validation-api-1.0.0.GA.jar
I've also tried packaging the faces-config.xml inside the my-validation.jar/META-INF/faces-config.xml
The funny thing is that this WORKS on Glassfish 3 and even JBoss 6 but NOT Jboss AS 7 and itsjboss-EAP 6.1.0 Final big brother.
My money is on the suspicion that with all this module concepts I might be missing a certain dependancy.
Thank you in advance
Message was edited by: Hlulani Mhlongo