On Tue, Sep 15, 2009 at 03:10:51PM +0100, Phil Housley wrote:
> 
> A good point, but I don't think it exactly applies in this case.  What
> I really want is for each component to know whether it is required
> within a very small context.  For example, a might have a criteria
> panel (one of the sub-parts of the searcher) set out as follows:
> 
> [x] must be a foo called [_____]
> 
> In which case, the sub panel will be in total charge of whether the
> text field is required.
> 

I like to do this as described here:

http://cwiki.apache.org/WICKET/conditional-validation.html#ConditionalValidation-CheckBox

final CheckBox checkBox = new CheckBox("cb");
form.add(checkBox);

TextField foo = new TextField("foo") {
    public boolean isRequired() {
        return Strings.isTrue(checkBox.getValue());
    }
}
form.add(foo);

> On the other hand, maybe that whole criteria panel is deselected, in
> which case I want to be able to stop the whole thing from doing any
> validation at all, so it won't even look at whether the check box is
> checked.
> 

I assume here that the checkbox/radiobutton is implemented at a higher level in
the hierarchy and you don't want the internals of the panel to know about it.
Is that right?

In this case, I usually implement a method on my panel, then delegate the
isRequired method on my form components to this panel method:

public class MyPanel extends Panel {
    public MyPanel(String panelId, ...) {
        super(panelId);
        new TextField("foo") {
            public boolean isRequired() {
                return MyPanel.this.isRequired();
            }
        }
    }

    /** Override to control whether my child components are required */
    public boolean isRequired() {
        return true;
    }
}

You can then just repeat the pattern above, overriding MyPanel.isRequired() and
checking your checkbox/radiobutton there.

jk


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org

Reply via email to