Announcement Announcement Module
No announcement yet.
Lost with Validation Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Lost with Validation

    Hi, I'm new to spring. I consider using parts of spring for a eclipse RCP based application I'm currently developing. I'm especially interested in a validation solution for my business obects. Straight forward I read the documentation and found:
    But sorry, where's the point? this document does not discuss the validation in depth. After reading I have no idea about how validation with spring can look like. This chapter discusses more or less only some facts about JavaBeans.

    So can please someone clarify on:

    - What's the relationship of Spring's validation capabilities and commons validator?
    - Is spring's standard validation (not Rich Client) even suitable for a Rich Client app or only related to web apps?
    - What is Spring's validation made for? Validating data based on beans or....?


  • #2
    ...But sorry, where's the point?
    This validation is type validation only. It is used for binding strings (e.g. from web requests or poperties file) to complex types.


    • #3
      What's the relationship of Spring's validation capabilities and commons validator?
      Not much. There is integration for Commons Validator at Spring Modules.

      Is spring's standard validation (not Rich Client) even suitable for a Rich Client app or only related to web apps?
      The type validation is mainly for web apps, as the conversion from string to complex objects is required.

      What is Spring's validation made for? Validating data based on beans or....?
      As explained above, type validation is for converting from String to complex types. Spring allows for business validation via a Validator interface.


      • #4
        Hi katentim, thanks for clarification on this topic!

        What I'm now interested in is: Is there any best practice to validate my domain objects with Springs functionality?
        I'm not so much into the functionality of spring that I can figure out a way.
        All I can say: My domain objects are commands and thus beans as I'm using the command pattern. My domain validation could somehow look like that:
        The commands implements some kind of validatable interface and I can throw this object to a validator which carries out validation. The validator is capable of some standard validations and cusom validation classes.
        I know this is a hard question as it aks for a complete design and is not specific but maybe has a pattern for such a scenario?!

        Thanks, Thomas


        • #5
          Hi Thomas

          We use Spring's stock-standard Validator interface in both the web and DAO tiers of our application. Our web tier uses the Validator as per standard Spring MVC. Before create, update and delete operations, our DAO superclass delegates to a ValidationManager which guarantees to find any Validator applicable to the domain object and validate it. This ensures no invalid objects ever get to the database. It's important to do this in the DAO tier as your services layer might have inadvertantly made a business rule mistake and attempted to persist it. If your services layers do anything at all, there's a good chance of this happening. Also, relying on RDBMS constraints is pretty limited in many cases, especially if you're mapping subclasses the recommended table-per-class-hierarchy way and as such the common NOT-NULL constraint is unavailable to you.

          The standard Spring Validator interface is actually very flexible, but it's a bit bare-bones to use for AOP type validation out-of-the-box. I feel like I'm marketing here, but Acegi Security CVS has a "domain" subproject that includes a validation package which includes a lot of goodies that show how useful the Validator really can become. This includes navigating into child objects (eg validating an Invoice causes its LineItem to also go to a Validator because there's a Hibernate association).

          We use it with good success in our current application. I personally don't know what the big deal is with Commons Validator (as Spring's Validator is quite adequate and multi-purpose with a little planning). I just use fValidate for MVC applications on the client side and it works well.


          • #6
            Hello Ben and thanks for your reply!

            That sounds interesting. I might apply this tactic for my business layer. I cannot do so in the dao layer as I use a mix of Spring DAOs for reading, Websphere's SDOs and plain CMP EJBs and I don't want to introduce validation there.

            For communication and execution of business logic I use the command pattern and I want to apply validation on those command beans. I think your solution might be adaptable here.

            I had a look at the spring validator interface ( but sorry I cannot figure out any way, how to use it. I understand that the first parameter is the bean to validate and then?!

            So does your solution involve AOP?!

            I think of an solution where I can devine via an XML file or programatically which fields od my bean have to be validated with which validator. AND some standard validators like string with length XY would be nice. Is this how you do it or...?

            I'll have a look at Acegi Security CVS.

            You talk about a Spring validator - well I cannot see one, do you mean one in modules?!

            Thanks, Thomas


            • #7
              Spring just offers the Validator interface, with its two simple methods.

              Acegi Security's Domain subproject (in CVS) offers a net.sf.acegisecurity.domain.validation package that has some useful classes, like one which accepts an Object you wish to be validated, finds the closest supporting Validator registered in the application context, and then uses it for validation. You can call this ValidationManager whenever you need to (ie from a DAO, before an update or save method - traditional OO) or use AOP to automate it (we offer an advisor and pointcut in the package as well).

              Basically you just write a Validator to process the properties of your domain objects, appending to the passed errors object if you find a problem. The ValidationManager does the rest. Fortunately, the same Validator can be used in Spring MVC controllers as well.