Those are basically the tradeoffs you need to make, though - if you allow an
empty string, it is up to your app to define a meaning for it, since the empty
string does not represent a valid number.
On Mar 7, 2011, at 12:55 PM, anton dos santos wrote:
> Hi Chris
>
> you are right, this works:
>
> @Override
> public boolean isValid(String text) {
> return (text.length() == 0) || super.isValid(text);
> }
>
> I was referencing a wrong validator in my bxml file :(
> but now when my input is empty, it can't be converted into a number when
> store() is invoked:
>
> java.lang.NumberFormatException: For input string: ""
> at
> java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
> at java.lang.Long.parseLong(Long.java:431)
> at java.lang.Long.parseLong(Long.java:468)
> at org.apache.pivot.beans.BeanAdapter.coerce(BeanAdapter.java:897)
> at org.apache.pivot.beans.BeanAdapter.put(BeanAdapter.java:286)
> at org.apache.pivot.json.JSON.put(JSON.java:152)
> at org.apache.pivot.wtk.TextInput.store(TextInput.java:841)
> at org.apache.pivot.wtk.Container.store(Container.java:613)
> at
> org.apache.pivot.wtk.content.TableViewRowEditor.endEdit(TableViewRowEditor.java:254)
>
> I solved this issue with a custom BindMapping, but now this gets a little bit
> complex ...
>
> Regards
> Anton
>
> On 06/03/2011 20:26, Chris Bartlett wrote:
>>
>> On 6 March 2011 16:11, anton dos santos <[email protected]> wrote:
>> I also tried StrictValidation="true" and an intValidator that accepts empty
>> input by overriding isValid():
>> public boolean isValid(String text) {
>> final ParsePosition pos = new ParsePosition(0);
>> Object obj = format.parseObject(text, pos);
>> if( obj == null || text.length() == 0) {
>> return true;
>> }
>>
>> // the text is only valid is we successfully parsed ALL of it. Don't
>> want trailing bits of
>> // not-valid text.
>> return obj != null && pos.getErrorIndex() == -1 && pos.getIndex() ==
>> text.length();
>> }
>> this works fine until user erases everything in the TextInput, from then on
>> use can enter any character, he is no longer limited to digits.
>>
>> What else could I try ?
>>
>>
>> This seems to work for me.
>>
>>
>> public class XXXIntRangeValidator extends IntRangeValidator {
>> // Constructors...
>>
>> @Override
>> public boolean isValid(String text) {
>> return (text.length() == 0) || super.isValid(text);
>> }
>> }
>