-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: [Validator]: Messages containing detailed validation errors
PostPosted: Mon Oct 20, 2008 5:59 am 
Newbie

Joined: Tue Feb 05, 2008 11:01 am
Posts: 2
Hi all,

I try to generate error messages that give a detailed explanation of the cause of the validation failure. Specifically, I have created a CronExpressionValidator and I want to display more than "CRON expression is not valid" (but instead e.g. "Month field contains invalid month") i.e. an error message that describes why validation failed.

Is there any way to implement this behaviour using Hibernate Validator or perhaps any workaround? All suggestions are welcome.

I also thought I once found a topic about this here in the forum, but I was unable to find it. Thanks already if someone could just point me to it.

Best regards,
Pierre


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 21, 2008 9:07 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

I don't think that is possible right now. Once Bean Validation (JSR 303) is out it will be possible tough.

--Hardy


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 22, 2008 3:38 am 
Newbie

Joined: Tue Feb 05, 2008 11:01 am
Posts: 2
Thanks Hardy for your response!

Meanwhile, I solved the problem the following way:

1. I defined an interface DetailedValidation:

Code:
public interface DetailedValidation{
   public String getDetails();
}


2. I created a new message interpolator:

Code:
public class DetailedMessageInterpolator implements MessageInterpolator {
    @Override
    @SuppressWarnings("unchecked")
    public String interpolate(String message, Validator validator, MessageInterpolator defaultInterpolator) {
        if (validator instanceof DetailedValidation) {
            return ((DetailedValidation) validator).getDetails();
        }
        return defaultInterpolator.interpolate(message, validator, defaultInterpolator);
    }
}


3. My validators that implement the DetailedValidation interface look like this (in fact they extend an AbstractDetailedValidation class containing a setter for details):

Code:
    @Override
    public boolean isValid(Object obj) {
        try {
            checkValid(obj);
            setDetails(null);
            return true;
        } catch (ValidationException ve) {
            setDetails(ve.getMessage());
            return false;
        }
    }


I'm aware that this only works as long as you create your own ClassValidator instance:

Code:
ClassValidator<T> dataBeanValidator = new ClassValidator<T>(dataClass, new DetailedMessageInterpolator());


It works nicely for our use cases though, so anyone, feel free to post comments or possible side-effects / hiccups. Thanks in advance!

Best regards,
Pierre


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.