How to validate number format TextField in wicket?

victorio

In my maven-wicket (6.10) application I have a TextField, which has an Integer type property model. I want to set a maximum length for numberts to type into this TextField. (for example the user should write maximum 2 characters to the "age" text field)

I have tried this code:

add(new TextField<>("age",new PropertyModel<(personModel,"age"))
    .add(StringValidator.maximumLength(2)));
//age is an Integer value from a Person class, personModel is "IModel<Person>" type

but I got this exception:

java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.String
    at org.apache.wicket.validation.validator.StringValidator.getValue(StringValidator.java:87)
    at org.apache.wicket.validation.validator.StringValidator.getValue(StringValidator.java:59)
    at org.apache.wicket.validation.validator.AbstractRangeValidator.validate(AbstractRangeValidator.java:107)
    at org.apache.wicket.markup.html.form.FormComponent.validateValidators(FormComponent.java:1523)

So if the property model is not string type, I cannot use StringValidator. I have found examples, which use NumberValidator (validators), but I cannot resolve NumberValidator. I have only these validators in the source: validation source How could I use number validator? Or am I missing something, maybe form the pom.xml's dependencies for wicket?

divanov

The problem you are facing is related to the way Wicket works. First it converts input text into a model object and then it performs validation.

Thus, you have to use RangeValidator instead of StringValidator

IModel<Integer> model =
    new PropertyModel<Integer>(personModel, "age");
Component ageField = new TextField<Integer>("age", model);
add(ageField).add(RangeValidator.<Integer>range(0, 99));

Note I've changes Long to Integer as I believe saving age as Long is not practical.

Also note that link to NumberValidator is for Wicket 1.4, while you are using Wicket 6. Wicket 6 is a large API change comparing to previous versions.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to validate number format TextField in wicket?

From Dev

How to validate a phone number in this format

From Dev

How to Validate Phone Number format

From Dev

How to Validate Phone Number format

From Dev

How to validate a specific format phone number?

From Dev

How to validate wicket form sequencly

From Dev

How to validate a form in a bootstrap modal window in wicket?

From Dev

How do you validate an Australian Business Number (ABN) format in Java?

From Dev

How to validate a string entry to ensure it is in a format of 'Number.Number.Number' (1.2.3)

From Dev

How to validate time format?

From Dev

How to validate the format of a string?

From Dev

How to validate phone number

From Dev

Adding a listener on a Wicket TextField

From Dev

Apache Wicket textfield onUpdate

From Dev

How to validate Integers from a TextField in a table with a wrapper

From Dev

Yii - How to validate a textfield for text only

From Dev

Yii - How to validate a textfield for text only

From Dev

How to validate the username textfield in a form in php

From Dev

Regex to validate phone number which is in the US Format

From Dev

how to validate UTC format date

From Dev

how to validate UTC format date

From Dev

How to pass number to TextField JavaFX?

From Dev

How to validate that an argument is a positive number?

From Dev

How to validate only number in winform?

From Dev

How to validate number of renderings on item

From Dev

How to validate phone number in laravel?

From Dev

How to validate the number of children records?

From Dev

Wicket: add TextField / Panel dynamically

From Dev

Changing the textfield of button on submit in Wicket

Related Related

HotTag

Archive