views:

660

answers:

2

Hi,

We use a standard SEAM setup here ... complete with the validation system that uses hibernate.

Basically what happens is a user enters a value into an html input and seam validates the value they entered using the hibernate validation.

Works fine for the most part except here's my problem: We need to record the results of validation on each field and I can't figure out a good way to do it ... ideally it would be done through communicating with the seam/hibernate validation system and just recording the validation results but as far as I can tell there isn't a way to do this?

Has anyone done anything like this in the past? There are a couple nasty work arounds but I'd prefer to do it cleanly.

Just a quick overview of the process that we have happening right now for context:

1) user enters field value 2) onblur value is set with ajax (a4j:support) at this point the validators fire and the div is re-rendered, if any validation errors occured they're now visible on the page

What I'd like to have happen at step2 is a 'ValidationListener' or something similar is called which would allow us to record the results of the validation.

Thanks if anyone is able to help :o

+1  A: 

You should be able to do it by creating a Bean that has a method observing the org.jboss.seam.validationFailed event. That method can then do whatever logging you want.

@Name("validationObserver")
public class ValidationObserver() {

    @Observer("org.jboss.seam.validationFailed")
    public void validationFailed() {
        //Do stuff
    }
}

The validationFailed event doesn't pass any parameters so you'll have to interrogate the FacesMessages or possibly the Hibernate Validation framework itself if you want to record what the error was.

Damo
A: 

I you are only using Hibernate for validation, you can use the Hibernate ClassValidator in the validationFailed() method, as recommended by Damo.
Example:

    public <T> InvalidValue[] validateWithHibernate(T object) {
     ClassValidator<T> validator = new ClassValidator(object.getClass());
     InvalidValue[] invalidValues = validator.getInvalidValues(object);
     return invalidValues;
    }
Zsolt Török