1

I am developing an application using Tomcat 7, JSF 2.2.6 (Mojarra), Hibernate Validator 4.3.1 and have come across the well documented bug shown here: https://java.net/jira/browse/JAVASERVERFACES-3183.

I have patched this temporarily using the fix given in this answer on another question: https://stackoverflow.com/a/21700488/1089995, until such a time as 2.2.7 is released publicly.

However when I attempt to use Annotations on fields, such as @NotNull, @Size, etc, these are simply ignored - there is no relevant stack trace for this, no error occurs, the annotations are simply ignored during the Validation phase.

page.xhtml

<input type="text" placeholder="e.g. Joe Bloggs" jsf:id="txtAdminName" jsf:validator="#{bean.validateAdminName}" jsf:value="#{bean.model.adminName}"/>

Model.java

//model bean with bean validation, which is not applied to the field.
@Size(min = 3, max = 50, message = "Please enter between 3 and 50 characters.")
private String adminName;

Bean.java

//the model bean with Bean Validation, as shown below.
private Model model;

//JSF Validator method, works fine
public void validateAdminName(FacesContext context, UIComponent component, Object convertedValue) {
  String adminName = convertedValue.toString();

  if(adminName.matches(".*[0-9].*")) {
    //not valid
    throw new ValidatorException(new FacesMessage("Please enter a valid name."));
  }
}

Is this an isolated problem, or does the temporary fix simply not allow the use of @Annotations? Any ideas would be much appreciated.

Thanks.

Edit: added sample working validator method source code, added info regarding lack of relevant stack trace.

Community
  • 1
  • 1
Chris Matthews
  • 382
  • 4
  • 19
  • "This fix has allowed me to use Validator methods in backing beans, however when I attempt to use Annotations on fields, such as `@NotNull, `@Size, etc, these are simply ignored." - Can you explain this in more detail? How can you use Validator methods, but no constraint annotations? Can you provide some code and a stacktrace if you have one? – Hardy Apr 23 '14 at 08:51
  • @Hardy, I have added sample working source code. There is no relevant stack trace as no error is thrown - the validation phase simply ignores any annotations on the Model bean. – Chris Matthews Apr 23 '14 at 10:51
  • I don't understand your code. Where in your bean do you use the Bean Validation API? All I see is just a custom regular expression check which even throws a non Bean Validation exception. I assume that Bean Validation annotations are not working, because you are just not wiring Bean Validation up at all. – Hardy Apr 23 '14 at 11:18
  • Bean Validation is being used in the model beans, such as the **Model.java** in the code above. I only included the JSF validation methods to show these were working as expected. I have updated the question to make it clearer, apologies for the original confusion. – Chris Matthews Apr 23 '14 at 11:28
  • @Hardy is that clearer now? – Chris Matthews Apr 25 '14 at 16:26
  • the model beans only contain the constraint annotations. Just adding them to the model classes will not have any effect, unless Bean Validation is hooked in somewhere. Somewhere a Validator instance needs to actually validate the model classes and to do this a ValidatorFactory needs to be build somewhere. That said, if JSF find Bean Valdiation on the classpath, it should be used transparently. So maybe you are running into a duplicate of this issue - http://stackoverflow.com/questions/8508984/why-fvalidatebean-wont-work. – Hardy Apr 25 '14 at 18:38
  • @Hardy, as you say, the validation should kick in transparently, and this was the case in a few older (JSF 2.1) projects I worked on. I can only assume I have implemented the "fix" incorrectly. Is there a specific place the FacesContextBootstrap class should be located in the project? At the moment I have it in the default package to keep it away from project specific code. – Chris Matthews Apr 27 '14 at 13:47
  • Having tested an old JSF2.2 version (2.2.2), I can confirm that the "fix" from the other thread is not working with my project. Bean Validation works with the older library. – Chris Matthews Apr 27 '14 at 14:47

0 Answers0