[Seaside] Form field validation and polite error reporting

Avi Bryant avi at beta4.com
Mon Jul 7 16:10:42 CEST 2003


On 7 Jul 2003, Ken Causey wrote:

> I'm really just getting started with Seaside and I'm struggling to
> figure out how to handle "nice" field validation error reporting.  My
> definition of nice is to return the form to the user just as she filled
> it out with one or more error messages and possibly the bad fields
> marked.  I figured I could throw Errors or some sort of Exception from
> the appropriate setter method.  However I can't figure out how to catch
> the error and provide the appropriate response.

This is something we've talked about before.  Part of the problem is that
you don't want to actually change any of your model data until you're sure
it all validates - throwing an exception on the setter is pretty ugly,
because, first, you lose the user's data, and second, any changes you've
made before the validation error get stored to the model, so it's quite
likely inconsistent.

You will find in Seaside a class called WAModelProxy, which is what I use
in such situations.  It wraps around another object, and uses DNU to
proxy any reads (ie, #personName) through to the original object, but
cache any writes (ie, #personName:) until you explicitly ask it to
#commit.

You use it something like this:

model: aRealModelObject
  proxy := WAModelProxy on: aRealModelObject.

renderContentOn: html
  html form: [
     "normal stuff, but using proxy as your model"

     html submitButtonWithAction: [self validate] text: 'Save'.
  ]

validate
  (self validate: proxy)
    ifTrue: [proxy commit]
    ifFalse: ["... save the validation errors somewhere to be displayed
next time"]


It would actually be quite easy to subclass ModelProxy with a version that
you can hang validation rules on, so that it did the checking for you, and
could answer #isValid or #validationErrors or whatever - this is something
Lukas and I played with a while back.

Cheers,
Avi



More information about the Seaside mailing list