On Thu, Dec 9, 2010 at 3:16 PM, Jeremy Thomerson
<[email protected]>wrote:
> > Example:
> >
> > class AutoConfigureBehavior extends AbstractBehavior {
> > @Override
> > public void onComponentTag(Component component, ComponentTag tag)
> {
> > super.onComponentTag(component, tag);
> >
> > if (component instanceof FormComponent<?> &&
> > !Strings.isEmpty(tag.getAttribute("required"))) {
> > ((FormComponent) component).setRequired(true);
> > }
> > }
> > }
> >
> > class AddAutoConfigureBehaviorToAllComponents implements
> > IComponentInstantiationListener {
> > @Override
> > public void onInstantiation(Component component) {
> > component.add(new AutoConfigureBehavior());
> > }
> > }
>
>
> Wasn't thinking this all the way through yesterday. onComponentTag
> won't work since you can't change the hierarchy at that point (IRRC
> this will block additions to validators, and it should block the
> change of the required flag). So, here's another idea.
>
Actually you can use
org.apache.wicket.behavior.Behavior.beforeRender(Component) instead.
All you need is component.getMarkupAttributes() and depending on them to add
some validators to the (form)component.
>
> In the application, you could have a factory method like
> newComponentAutoConfigurer that returns an interface. Default could
> either be returns one that does nothing or one that does the default
> validation (required, min, max, etc). It could use an
> IComponentOnBeforeRenderListener registered to the application to
> invoke itself. It could have a method that allows the "configurers"
> to be plugged in (i.e., in my app, I want designers to be able to
> specify required in markup, but not min, max, etc...)
>
Here is another solution from me:
Add constructor parameter or setter to Form and FormComponent
(#setMarkupConfigurable(true)) and then the user can call that for each
formcomponent she wants or write a visitor that will do that for all
formcomponents in form/page, or component instantiation listener.
Using a constructor parameter will work if we want to configure from markup
in onInitialize(), using a setter will
move this logic to onConfigure()/onBeforeRender() and will require usage of
hasBeenRendered() == false.
> --
> Jeremy Thomerson
> http://wickettraining.com
> Need a CMS for Wicket? Use Brix! http://brixcms.org
>