You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@click.apache.org by Bob Schellink <sa...@gmail.com> on 2010/10/15 13:35:27 UTC

Improved Field validation

 The explicit binding feature in 2.2.0 is quite useful in building highly scalable dynamic pages,
however our validation story isn't as complete as I'd like it to be. Today I've helped a colleague
building a fairly sophisticated dynamic form, and one issue we ran into was that Click Fields bind
and validate their values *even* for dynamically added fields which did not partake in the current
post request. In other words the newly added fields does not have a request parameter. We ended up
conditionally toggling form validation, but the default value we assigned in the onInit gets
overridden by the binding, forcing us to set default value in the onRender. This process wasn't
smooth as I've hoped, so I looked at the Field source code for ways to improve the behavior, and
think I've found a way.

Field#onProcess() looks like this:

        bindRequestValue();
        if (getValidate()) {
            validate();
        }
        dispatchActionEvent();


So whether the field has an incoming request parameter or not, its value is bound to it's request
parameter. And validation will occur unless explicitly set to false. So the idea is to wrap the
above code like this:


  if (getContext().hasParameter(getName()) {
    bindRequestValue();
    if (getValidate()) {
      validate();
    }
  dispatchActionEvent();
}

This nicely sidesteps the issue we ran into today. Only submitted fields will be validated, and
Field's default values set in onInit is left as is.

Maybe there is a reason for the current behavior that I'm missing?

Bob