1

I have a form where I need to add/remove validators dynamically. Based on a dropdown selection, other form fields may have different validation rules.

For other kinds of inputs, I've used replace(methodThatCreatesTheInput()) to get rid of a previously added validator. (Not knowing of a better way. Specifically, there doesn't seem to be any way to directly remove a validator from a component...)

With Select, from wicket-extensions, this approach fails with something like:

WicketMessage: submitted http post value [[Ljava.lang.String;@5b4bf56d] 
for SelectOption component  [8:myForm:targetInput] contains an 
illegal relative path element [targetConsortiums:1:option] which does not
point to an SelectOption component. Due to this the Select component cannot 
resolve the selected SelectOption component pointed to by the illegal value.
A possible reason is that component hierarchy changed between rendering and 
form submission.

The method that creates the Select:

private FormComponent<?> targetSelection() {
    Map<Class<? extends Target>, List<Target>> targets = targetService.getAllAsMap();

    SelectOptions<Target> propertyOptions = new SelectOptions<Target>("targetConsortiums",
            targets.get(Consortium.class), new TargetRenderer());
    SelectOptions<Target> consortiumOptions = new SelectOptions<Target>("targetProperties",
            targets.get(Property.class), new TargetRenderer());

    Select select = new Select(ID_TARGET, new PropertyModel<Target>(model, "target"));
    select.add(propertyOptions);
    select.add(consortiumOptions);
    select.setRequired(true);
    select.setMarkupId(ID_TARGET);

    return select;
}

(Why use a Select instead of normal DropDownChoice? We want the two types of choices to be clearly separated, as documented in this question.)

Any ideas how to solve this? What I'm trying to achieve is, of course, very simple. Unfortunately Wicket disagrees, or I'm using it wrong.

Wicket 1.4.

Community
  • 1
  • 1
Jonik
  • 80,077
  • 70
  • 264
  • 372
  • I came up with a relatively easy workaround: use **form validator** that considers the values of both inputs (in my case "loan type" and "loan target" selections) and never needs to be removed from the form. I won't spend more time on this, but feel free to answer the original question if you can! – Jonik Feb 17 '12 at 12:38
  • 1
    I think form validators are not just a workaround, they're the correct answer. They are specifically there to deal with validation involving more than one input value. – biziclop Feb 17 '12 at 16:04
  • @biziclop, thanks! If you want to add that as an answer, maybe elaborated slightly, I'd accept it. – Jonik Apr 03 '12 at 13:46

1 Answers1

0

I don't know how to do this on Wicket 1.4, but on Wicket 1.5 there is a remove method for validators on FormComponent (see javadoc)

jordeu
  • 6,711
  • 1
  • 19
  • 19
  • Yeah, [1.4 doesn't have](http://wicket.apache.org/apidocs/1.4/org/apache/wicket/markup/html/form/FormComponent.html) such method; that was the problem. – Jonik Apr 03 '12 at 13:42