-->
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.  [ 4 posts ] 
Author Message
 Post subject: @Email doesn't work as what it shall be
PostPosted: Fri Sep 17, 2010 6:48 pm 
Regular
Regular

Joined: Tue Feb 17, 2009 3:36 pm
Posts: 78
I have a simple web form bean as the following:
Code:
public class UsernameRetrievalForm {
   
   @NotEmpty
   @Email
   String email;

   /**
    * @return the email
    */
   public String getEmail() {
      return email;
   }

   /**
    * @param email the email to set
    */
   public void setEmail(String email) {
      this.email = email;
   }
   
}


If I leave the field blank, I will get an error message about the empty field. But, if I enter an invalid string, I get an exception as the following:
Quote:
java.lang.IllegalArgumentException: can't parse argument number value
java.text.MessageFormat.makeFormat(MessageFormat.java:1339)
java.text.MessageFormat.applyPattern(MessageFormat.java:458)
java.text.MessageFormat.<init>(MessageFormat.java:368)
org.springframework.context.support.MessageSourceSupport.createMessageFormat(MessageSourceSupport.java:115)
org.springframework.context.support.MessageSourceSupport.formatMessage(MessageSourceSupport.java:99)
org.springframework.context.support.AbstractMessageSource.renderDefaultMessage(AbstractMessageSource.java:275)
org.springframework.context.support.AbstractMessageSource.getMessage(AbstractMessageSource.java:152)
org.springframework.context.support.AbstractApplicationContext.getMessage(AbstractApplicationContext.java:1125)
org.springframework.web.servlet.support.RequestContext.getMessage(RequestContext.java:559)
org.springframework.web.servlet.support.BindStatus.initErrorMessages(BindStatus.java:177)
org.springframework.web.servlet.support.BindStatus.getErrorMessages(BindStatus.java:273)
org.springframework.web.servlet.tags.form.ErrorsTag.exposeAttributes(ErrorsTag.java:172)
org.springframework.web.servlet.tags.form.AbstractHtmlElementBodyTag.writeTagContent(AbstractHtmlElementBodyTag.java:48)
org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:91)
org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:78)


Is a Validator problem or a Spring Problem?


Top
 Profile  
 
 Post subject: Re: @Email doesn't work as what it shall be
PostPosted: Sat Sep 18, 2010 6:58 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Looking at the stack trace Validator does not even seem to be called yet. Not sure what's going on there. Can you describe more your framework stack? Which value do you try to validate? How do you enable Validator, etc

--Hardy


Top
 Profile  
 
 Post subject: Re: @Email doesn't work as what it shall be
PostPosted: Sat Sep 18, 2010 1:15 pm 
Regular
Regular

Joined: Tue Feb 17, 2009 3:36 pm
Posts: 78
hardy.ferentschik wrote:
Looking at the stack trace Validator does not even seem to be called yet. Not sure what's going on there. Can you describe more your framework stack? Which value do you try to validate? How do you enable Validator, etc

--Hardy


Hi Hardy,

Thanks very much your quick rely.

The stack trace is copied from the top of exception trace. You are right. I am fully aware that the Hibernate Validator was not even called yet. When I leave the field blank, I get "may not be empty" (twice). I guess the message is a native validator message. I don't know why it shows up twice, however.

We use the Spring MVC framework 3.0. It enables the Validator with an annotation @Valid. In this case,
Code:
public String submit(
         @Valid @ModelAttribute("usernameRetrieval") UsernameRetrievalForm usernameRetrieval,
         BindingResult result, SessionStatus status, Model m) {

      if (result.hasErrors()) {
         logger.debug("validation error");
         result.rejectValue("email", "INVALID_EMAIL", result.getFieldErrors(
               "email").get(0).getDefaultMessage());
         return "profile.usernameRetrievalForm";
      }
...
}


That is how Spring MVC handles validation with the Hibernate Validator. If I have a whitespace or a random string such as "svbe", I will get the exception.

Can I ask you a related question? Messages from Hibernate Validator can be quite verbose. That is good for providing an insightful information. That is not too good for UI though, too much text beside a UI component. Is a way to customize a validator error message in an i18n environment (that is a text on Java code is a message key in a message file)?

________________________________________________________________________
Having your web bookmarks anywhere you go - http://homepage.kgbinternet.com


Top
 Profile  
 
 Post subject: Re: @Email doesn't work as what it shall be
PostPosted: Tue Sep 21, 2010 5:51 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
wei725 wrote:
[
Can I ask you a related question? Messages from Hibernate Validator can be quite verbose. That is good for providing an insightful information. That is not too good for UI though, too much text beside a UI component. Is a way to customize a validator error message in an i18n environment (that is a text on Java code is a message key in a message file)?

Sure. You can provide your own resource bundle. Create a file called ValidationMessages.properties (and any translations of it) and put it into the root of the classpath. You can overrride existing messages or define new ones (each Bean Validation constraint takes a message parameter where you can define a message key to be looked up in your resource bundle).
Have a look at the online manual if you like to know more.

--Hardy


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.