0

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?

San Kap
  • 11
  • 6

2 Answers2

0

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.

Tankhenk
  • 634
  • 5
  • 20
  • I don't understand how a long variable startRange will keep a value bigger than Long.MAX_VALUE (startRange.compareTo(Long.MAX_VALUE) ???). And I try to use a listener, but it has worked only if a value smaller than Long.MAX_VALUE. – San Kap Sep 13 '13 at 14:14
  • My solution checks if the long isn't bigger the Max - 1 do that you get your validation message. And your original input stays preserved – Tankhenk Sep 14 '13 at 11:33
0

This problem is solved with help <f:converter converterId="javax.faces.Long"/>

San Kap
  • 11
  • 6