3

I am using a Form component to edit values in an Object . I have mapped an object using a LoadableDetachableModel to the form so that the value in the object is displayed on the form and updates are applied automatically.

final Form<Withdrawal> form = new Form<Withdrawal>("form",
                new CompoundPropertyModel<Withdrawal>(ldm)) {
.... }

However, I have a problem with adding validators to form component such as StringValidator to text fields. I have a "comment" field ( one of the fields in the Withdrawal Object).

Ideally I want to add it as :

TextField<Withdrawal> tf_comments = new TextField<Withdrawal>("comment");
tf_comments.add( new StringValidator.MaximumLengthValidator(255));
form.add( tf_comments);

But since I cannot add StringValidators to it because StringValidator for TextField<Withdrawal> is not defined. So I am using the below and getting value of the field and setting it to object manually.

    TextField<String> tf_comments = new TextField<String>("comment");
    tf_comments.add( new StringValidator.MaximumLengthValidator(255));
    form.add(tf_comments);

Is there a way to add a Validator directly on to TextField<Withdrawal>?

Thank you.

Jay
  • 690
  • 1
  • 10
  • 27

1 Answers1

1

First, your understanding of TextField<Withdrawal> with CompoundPropertyModel ist wrong: The CompoundPropertyModel is responsible for binding a property specified by the name via PropertyModel to the TextField. So you do not need generic StringValidator objects for the TextField.

In Wicket 6.7: TextField<Withdrawal> tf_comments = new TextField<Withdrawal>("comment"); tf_comments.add(StringValidator.maximumLength(255));

For Wicket 1.5 your code should work, at least I'm not getting any syntax errors. You should have getter and setter for "comment" in the Withdrawal class.

mrak
  • 2,826
  • 21
  • 21
  • I am using Wicket 1.4. I am getting following error **no suitable method found for add(StringValidator) method FormComponent.add(IValidator) is not applicable (actual argument StringValidator cannot be converted to IValidator by method invocation conversion)** – Jay Jun 12 '13 at 13:07
  • Can't say what is wrong with your setup: I just quickly tested the example on jetty with wicket 1.5 and it was working. Can you paste more of your code (e.x. Withdrawal class) pastebin? – mrak Jun 12 '13 at 13:49
  • 1
    You specified Wicket 1.5 in the tag, so I tested for this version. For Wicket 1.4 use just TextField tf_comments = new TextField("comment"). This should do the trick. Btw: Is there a particular reason that you are using an outdated wicket version? – mrak Jun 12 '13 at 13:56