简体   繁体   中英

Seam/Hibernate validator listener

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

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.

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;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM