FloatRangeValidator question

I am using smartgwt v2.3 and the latest firefox.

I am trying to get the FloatRangeValidator to not select the value when validation fails. It is set to validate on change. The problem is every time I push a key, it validates the field and selects the text. In order to type in a valid value, I have to continually unselect the value and then type another key. So if my validation requires the number to be higher than 600, for every key stroke until I get above 600 it selects the text. Very annoying!

I see some posts talking about a selectOnFocus setting but I don’t see that as an option. A validate on exit function would also work but I don’t see such an item either.

Also, the floatRangeValidator accepts letters. It should only be allowing numbers.

I can think of a couple work arounds such as adding an exit event handler and extracting the value from the field and then run the validation. I am also thinking I might need to just make a custom validator to make sure no letters are accepted as input into the field. Finally, is the selection of the current text on validation error built into all validators. If I write a custom validator will it also do it? The last wordaround would be just to write a method to look at the value when the user tries to save the form and make sure it is an actual number but I would rather user the nicer functionality built into the package.

If you’re going to set validateOnChange, set changeOnKeypress to false.

Ok, I will try that. I also noticed there is a IsFloatValidator validator. I assume I just need to set both validators onto the field.

The setChangeOnKeypress function is only available on form items. I am trying to do this with a listgrid. If I implement a custom validator does it also do the same function concerning the onkeypress validation?

I loaded the IsFloatValidtor on the ListGridField and it is still accepting letters. On another note setting setValidateByCell to true and not setting setValidateOnChange works for the field problem.

So I was setting both validators on the listgridfield using the setvalidators method which takes an array of validators by listing them. However, it looks like it only really takes the last one since when I passed in only the isfloat validator and it now works.

So two things:

First is this a bug? Why isn’t it using both validators.

Second, shouldn’t a float range validator implicitly check it is a float…how else is it going to check it is within range?

Configure the FormItems used by a grid for editing via ListGridField/DataSourceField.setEditorType().

If you declare the DataSourceField as type “float”, the isFloat validator is implicit.

Declaring a field as type float does not, by default, prevent entry of non-numeric characters. Use Text Masking for this (see samples in the Showcase) if you want it.

Thanks for the response…It would be nice to stop them from even inputting letters if that’s what the mask function does. I will take a look at it.

On another note, a work around for the multiple loading of validators..

I was using a static function to return new instances of the validators I use in the application. So I was doing something like:

listGridField.setValidators(AppValidators.getNewXRangeValidator(), new IsFloatValidator());

You cannot do this. It now works when loading multiple validators when I create and use them within the local space. I am thinking the static function might be the overarching problem. I am going to try and create them in a singleton maybe and see if that works. I would rather have the validators i use in multiple places defined in one place rather than in every java class that needs them.

It’s not a problem with a static function, it’s a problem of passing the exact same validator object to multiple fields. Validators are stateful and can contain custom attributes. Go ahead and use a static function, but have it return fresh, distinct objects each time.

The static function does return a new instance of a validator every time…below is an example.

public static Validator getBandwidthValidator(){
FloatRangeValidator bandwidthValidator = new FloatRangeValidator();
bandwidthValidator.setMin( 3.0f );
bandwidthValidator.setMax( 50000.0f );
return bandwidthValidator;
}

Then try to create a complete, standalone test case demonstrating an issue.

That’s crazy…the stand alone test works just fine…all I did was cut out the code around it but otherwise its the same…