f: validateLongRange. Validation of huge numbers

advertisements

I am using JSF 2 and primefaces 3.5. I have an inputText which must be a number between Long.MIN_VALUE and Long.MAX_VALUE.

<p:inputText id="startRange" value="#{attributeBean.attribute.startRange}">
<f:convertNumber />
<f:validateLongRange minimum="#{attributeBean.minimumValue}"
                     maximum="#{attributeBean.maximumValue}"/>
</p:inputText>

In attributeBean:

public Long getMinimumValue(){
  return Long.MIN_VALUE;
}
public Long getMaximumValue(){
  return Long.MAX_VALUE;
}

When I enter a huge number like 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 validation message doesn't appear. If come back to this form in an inputText field is 9,223,372,036,854,775,807 value. Can I get a validation message?


Well this is do to the f:converter. The converter tries to convert 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
To a long. The max of a long = 9,223,372,036,854,775,807 so the converter works well :-)

But this behavior is not what you want i guess. So you can validate it yourself on this way. Add a actionListener to your command button: actionListener="#{attributeBean.validateLong}" and remove this:

<f:validateLongRange minimum="#{attributeBean.minimumValue}"
                     maximum="#{attributeBean.maximumValue}"/>

and add a method like this in your bean:

public void validateLong()
{
 if(startRange.compareTo(Long.MIN_VALUE) > 0 && startRange.compareTo(Long.MAX_VALUE) < 0)
 {
    //Do bussiness logic
 }
 else
 {
     //Throw message
      FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Not good!"));
 }
}

Keep in mind that your range with above check is between: -9223372036854775807 and 9223372036854775806. But i don't know if thats a problem.