Reputation: 720
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.
Upvotes: 3
Views: 827
Reputation: 2906
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.
Upvotes: 1