How to use Hibernate validator with Primefaces site validation (CSV)
I have followed Primefaces 5.1 tutorial and I can make client site validation (CSV) work with Bean validation (JSR 349). I have
<context-param>
<param-name>primefaces.TRANSFORM_METADATA</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>primefaces.CLIENT_SIDE_VALIDATION</param-name>
<param-value>true</param-value>
</context-param>
This setting is standard @Size
or @Pattern
works without going back to the server. However, annotations like those @Email
from Hibernate Validator don't work at all. The User's Guide in the User's Guide describes third-party annotations as
When using third party constraints like Hibernate Validator custom annotations, use the BeanValidationMetadataMapper to define the ClientValidationConstraint for them.
BeanValidationMetadataMapper.registerConstraintMapping(Class<? extends Annotation>
constraint, ClientValidationConstraint clientValidationConstraint);
BeanValidationMetadataMapper.removeConstraintMapping(Class<? extends Annotation>
constraint);
This sounds very mysterious to me. Does anyone know what the setup should look like? Thank.
source to share
For @NotBlank, for example:
package com.algaworks.pedidovenda.util;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import org.hibernate.validator.constraints.NotBlank;
import org.primefaces.validate.bean.BeanValidationMetadataMapper;
import com.algaworks.pedidovenda.validation.NotBlankClientValidationConstraint;
@WebListener
public class AppContextListener implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent event) {
}
@Override
public void contextInitialized(ServletContextEvent event) {
System.setProperty("org.apache.el.parser.COERCE_TO_ZERO", "false");
BeanValidationMetadataMapper.registerConstraintMapping(NotBlank.class,
new NotBlankClientValidationConstraint());
}
}
And the ClientValidationConstraint itself:
package com.algaworks.pedidovenda.validation;
import java.util.HashMap;
import java.util.Map;
import javax.validation.metadata.ConstraintDescriptor;
import org.hibernate.validator.constraints.NotBlank;
import org.primefaces.validate.bean.ClientValidationConstraint;
public class NotBlankClientValidationConstraint implements ClientValidationConstraint {
public static final String MESSAGE_ID = "{org.hibernate.validator.constraints.NotBlank.message}";
@SuppressWarnings("rawtypes")
@Override
public Map<String, Object> getMetadata(ConstraintDescriptor constraintDescriptor) {
Map<String, Object> metadata = new HashMap<String, Object>();
Map attrs = constraintDescriptor.getAttributes();
Object message = attrs.get("message");
if (!message.equals(MESSAGE_ID)) {
metadata.put("data-msg-notblank", message);
}
return metadata;
}
@Override
public String getValidatorId() {
return NotBlank.class.getSimpleName();
}
}
Assuming the code works because it was "borrowed" from here:
And don't forget about:
PrimeFaces.validator.NotBlank = {
MESSAGE_ID : 'org.hibernate.validator.constraints.NotBlank.message',
validate : function(element, value) {
if (value === null || value === undefined || value.trim() === '') {
var msg = element.data('msg-notblank');
var label = element.data('p-label');
var context = PrimeFaces.util.ValidationContext;
var msgObj;
if (!msg) {
msgObj = context.getMessage(this.MESSAGE_ID, label);
} else {
var msgObj = {
summary : msg,
detail : msg
}
}
throw msgObj;
}
}
};
source to share