views:

1190

answers:

2

Hi all,

The problem I'm having is that my Bean Validation isn't working as I would expect.

I have a Session Scoped Managed Bean with a name field that is bound to an h:inputText. The name must be entered, and have a minimum length of 1 character, and a maximum length of 5 characters. I expect that when I enter the name into the textbox, it will be validated accordingly by the backing bean, and if it fails, then it would display the corresponding error messages.

However, this is not the case. The validations are always failing, even if I enter a valid case in the inputText (e.g. "abc"). On debugging the application, it seems that the getName() accessor is always called, and the setter is never reached. Am I doing something wrong? I assume the validator uses the accessor to validate, but the problem is that the setter never gets a chance to update the value of the name... I must be missing something.

Below is the Managed Bean:

@ManagedBean
@SessionScoped
public class James implements Serializable {

  public James() {
    super();
   }

  private String name;

  @NotNull
  @Min(value = 1)
  @Max(value = 5)
  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
}

Below is the fragment of the JSF xhtml. I tried a few different variations with separate forms, but the first option is the one I would assume to work (I think it's equivalent to the third option, but tried just in case :) )

<h:messages/>
<h:form>
  <h:inputText id="jamesName" value="#{james.name}"/>
  <h:message for="jamesName"/>
  <h:commandButton value="submit"/>
</h:form>
<h:form>
  <h:inputText id="jamesName" value="#{james.name}" immediate="true"/>
  <h:message for="jamesName"/>
  <h:commandButton value="submit"/>
</h:form>
<h:form>
  <h:inputText id="jamesName" value="#{james.name}">
    <f:validateBean />
  </h:inputText>
  <h:message for="jamesName"/>
  <h:commandButton value="submit"/>
</h:form>

I'm using JSF 2.0.2-FCS with Hibernate Entity Manager 3.3.2.GA and Hibernate Validator 4.0.2.GA (below are the relevant parts from my Maven POM), running in Tomcat 6.0.20 on Windows XP Pro Service Pack 3 (32-bit).

<dependency>
  <groupId>javax.faces</groupId>
  <artifactId>jsf-api</artifactId>
  <version>2.0.2-FCS</version>
</dependency>
<dependency>
  <groupId>javax.faces</groupId>
  <artifactId>jsf-impl</artifactId>
  <version>2.0.2-FCS</version>
</dependency>
<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>jstl</artifactId>
  <version>1.2</version>
</dependency>
<dependency>
  <groupId>javax.servlet.jsp</groupId>
  <artifactId>jsp-api</artifactId>
  <version>2.1</version>
  <scope>provided</scope>
</dependency>
<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-entitymanager</artifactId>
  <version>3.3.2.GA</version>
</dependency>
<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-validator</artifactId>
  <version>4.0.2.GA</version>
</dependency>
A: 

As for your additional question - see this matrix - no, they are not compatible.

About the JSF question - check both tomcat logs and the firefox javascript console.

Bozho
I don't think it has anything to do with Javascript though, the post reaches the server successfully... any problems you've encountered regarding this?It's not an exception, so it wouldn't show in the Tomcat logs I'd think, but point noted. I don't remember anything unusual in the logs, but I'll check them again when I get back to this.Thanks for the link, I've been there before but never actually scrolled down far enough, assuming that the latest releases would be compatible :B Though, I did try this with 3.1.0 GA as well with no luck if I remember correctly.
jamiebarrow
also reduce the logging threshold of your logger.
Bozho
But what if I want the logging for debugging, that's what logging is for :D reducing it to WARN does help, but it's still very slow. I'm not too concerned about it for now, since if an issue comes up I'll just attach a debugger anyways, or increase it to TRACE and use Tomcat outside of any IDE (bit of a pain, but hey)
jamiebarrow
reduce treshold = make the log level lower, i.e. DEBUG or TRACE.
Bozho
Ah, sorry I thought you meant the other way around :) it's already at TRACE, and extremely slow with Glassfish - related to a comment on another stackoverflow question I have - sorry for the confusion. I'll get back to this tomorrow probably.
jamiebarrow
+1  A: 

Your validation is working correctly. The problem is with your @Min and @Max annotations:

@NotNull
@Min(value = 1)
@Max(value = 5)
public String getName() {
    return name;
}

@Min and @Max are meant for numeric comparison. You wrote:

The name must be entered, and have a minimum length of 1 character, and a maximum length of 5 characters

You should use @Size annotation instead, as in:

@NotNull
@Size(min = 1, max = 5)
public String getName() {
    return name;
}

The bean fields are not updated because the validation fails. Only after the validation succeeds will the bean get updated.

Tuukka Mustonen
Thanks Tuukka, that makes sense. Still haven't gone back to this yet, gone on to different projects, hopefully will try it out in my spare time.
jamiebarrow
Seems this is a popular question. I'll credit you to an answer, although haven't tried the solution as yet
jamiebarrow