0

I have recently upgraded from JSF 1.2 to JSF 2.1. Here is my code:

Product Code: <h:inputText id="productCode" value="#{dataItem.productCode}" maxlength="25" disabled="#{dataItem.disabled}">
                        <f:validateLength minimum="2" maximum="25" />
                    </h:inputText>
                    <h:message for="productCode" errorClass="error" showSummary="false" />

The behavior of the validateLength in JSF 1.2 was if we enter the value "1" in the field Product Code which is less than the mininum allowable '2', I will get the error message "Value is less than allowable minimum of '2'". But if I dont enter any value for the field Product Code then I dont see any error message.

This behavior has changed part of JSF 2.1. If I dont give any value for the field Product Code then I get the error message "Value is less than allowable minimum of '2'".

Did the behavior really change part of JSF 2.1?

Any thoughts or help on this is much appreciated.

Sri
  • 309
  • 1
  • 9
  • 24

2 Answers2

0

That was actually a bug in JSF 1.2. An empty non-required field should not be validated like that.

If you want to validate the requireness, use required="true".

<h:inputText ... required="true">

If you want to control the required message, set the requiredMessage attribute (new since JSF2):

<h:inputText ... required="true" requiredMessage="Please enter value">

You can by the way control the generic validation message by validatorMessage as well (also new since JSF2).

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I'm seeing this same behavior. I guess I'm confused. Which part is the bug? I don't have a required attribute, but the empty values still fire the minimum validation error. This is not what I want I only want that to fire **if** there is a value or if it is required. I have a question posted [here](http://stackoverflow.com/questions/12678784/jsf-2-1-x-validation-behaves-differently-on-glassfish-3-1-2-vs-tomcat-6-7). – Mickelback Oct 26 '12 at 17:31
  • The bug is that the `` is also fired on an empty non-required field. – BalusC Oct 26 '12 at 17:37
  • to be clear, is the bug in 1.2 or 2.1? I'm using Glassfish 3.1 using Jsf 2.1.12 and I'm getting behavior that the field is **not** required, yet I'm still getting the validteLength to fire. Also, just to add, we've been using 2.1 for quite some time on Tomcat with no problems...only started seeing this behavior when we started using Glassfish...any thoughts? – Mickelback Oct 26 '12 at 17:49
0

You can configure this behaviour by adding the following context param:

<context-param>
    <param-name>javax.faces.VALIDATE_EMPTY_FIELDS</param-name>
    <param-value>false</param-value>
</context-param>