Re: [newbie] cayenne & tapestry: how to validate form?

From: Andrus Adamchik (andru..bjectstyle.org)
Date: Mon May 19 2003 - 22:54:02 EDT

  • Next message: Mario.Link..fi-lsa.de: "IBM DB2 Adapter"

    >> I guess the validation logic should be in or near my
    >> CayenneDataObjects. Should I implement a validate-method or should the
    >> set's throw an validationexception?
    >
    > I don't think this is a good idea. You definitely want validation to
    > happen on a defined point in time, not arbitrarily when someone sets
    > values on your DataObject.
    >
    > -dirk

    To add to what Dirk just said, my own best practice is to call a special
    custom validation method from a Tapestry *action* method explicitly. I
    have always been skeptical about "generic" validation approach. I will try
    to explain why below.

    Validation method itself is normally implemented by the business
    (persistent) object subclass, but has to have some kind of awareness of
    the control layer to make conclusions. E.g. you can check that a
    "password" and "confirmed password" fields match, even though "confirmed
    password" is not a persistent property, and may even be stored outside of
    a business object, as a page variable. So sometimes you may opt to move
    validation from the model to the control layer.

    Another important thing to me is to be able to build a detailed error
    message as a result of validation. You can include a dictionary of
    validation messages into the thrown exception, and then catch it in the
    control layer, retrieve the dictionary, and use it to initialize the error
    message(s) on the page. I normally just return such dictionary from the
    validation method without throwing an exception. This is a matter of taste
    of course.

    To make it really fancy, validation errors dictionary can contain the keys
    to messages stored by page in an external (localized) dictionary. This
    would allow to reuse the validation procedure, but show different messages
    depending on the context of a page.

    Andrus



    This archive was generated by hypermail 2.0.0 : Mon May 19 2003 - 22:54:02 EDT