I'm trying out some bean validation and I'm stumbling upon 'strange' behavior. I'm working with Glassfish and Primefaces as a front-end (if it makes any difference). Elsewhere in my project I use the Hibernate-validator, I'm not sure if it is validating JSF (else it is the default in Glassfish). I have a managed bean:
@javax.faces.bean.ManagedBean
@javax.faces.bean.ViewScoped
public class TestBean
{
@Size(min=8)
private String _testString;
public String getTestString()
{
return _testString;
}
public String setTestString(String testString)
{
_testString = testString;
}
public void doSomethind()
{
// Do something with the test string
}
}
And a JSF page containing this:
<h:form id="testForm">
<h:outputLabel for="testInput" value="Input:"/>
<p:inputText id="testInput" value="#{testBean.testString}"/>
<p:message id="testInputMsg" for="testInput"/>
<p:commandButton value="Aanmaken" action="#{testBean.doSomething}" update="@form"/>
</h:form>
The _testString
does not get validated this way. However, bean validation does work when I change the field to:
@Size(min=8)
private String testString;
or when I annotate the getter in stead of the field:
private String _testString;
@Size(min=8)
public String getTestString()
{
return _testString;
}
Following our coding guidelines, we must prefix private fields with an underscore. Which leaves me with one option; annotate the getter.
Could someone care to explain why it is behaving like this?