6 Replies Latest reply on Feb 11, 2007 3:05 PM by William E. Sollers III

    Complex validation

    Vladimir Kovalyuk Apprentice

      May I propose some extensions for registration functionality for the Booking example?

      1. Implement Hibernate custom validator for checking password and re-entered password equality.
      Actually reference manual refers Hibernate custom validators, but there is no example.

      2. Perform custom validation for the Username property (user already exists) and fill in message related to property, so it should appear next to input. I saw web2 apps that perform verification asynchronously - it's great.
      Chapter 9. JSF form validation in Seam doesn't cover complex validation topic.

        • 1. Re: Complex validation
          Jason Long Master

          I have a lot of complex validations in my project.

          I just put the logic in my action method and add the appropriate messages to a map as feed it to the function below.

          If messages were added I redirect to the same page. This seems like an old fashioned what to do things, but I have not found a more JSF way to do this.

          If anyone has a better solution, please let me know.

          My validations are more complex than just inspecting the object to be persisted. I need to query the EntityManager to see if these changes are acceptable, not simply compare a few properties on an object.

          I also do not like to have to put required="true" in order to validate null fields.


          Is there any way around this?
          Could Seam be made to override the default JSF implementation behavior in this case?


           public int addMessages(Map<String, String> m)
           {
           for(String key : m.keySet())
           {
           if(key.startsWith("globalMessage"))
           {
           FacesMessages.instance().add(m.get(key));
           }
           else
           {
           FacesMessages.instance().add(key, m.get(key));
           }
           }
           return m.size();
           }
          


          • 2. Re: Complex validation
            Vladimir Kovalyuk Apprentice

            It seems that simple validation (related to entity itself) is completely covered by Seam, except custom hibernate validators.
            AFAIK hibernate validator is supposed to work without hibernate itself, for instance it should work with Kodo.

            Complex validation (how entity correlates to the other entities, rules and policies) are not covered at all.
            Since Seam already employs JBoss Rules for authorization I believe it's possible to perform complex validation using Rules as well. The only thing I'm concerned about is performance.

            • 3. Re: Complex validation
              Gavin King Master

               

              "vladimir.kovalyuk" wrote:
              It seems that simple validation (related to entity itself) is completely covered by Seam, except custom hibernate validators.
              AFAIK hibernate validator is supposed to work without hibernate itself, for instance it should work with Kodo.

              Complex validation (how entity correlates to the other entities, rules and policies) are not covered at all.
              Since Seam already employs JBoss Rules for authorization I believe it's possible to perform complex validation using Rules as well. The only thing I'm concerned about is performance.


              +1

              • 4. Re: Complex validation
                Ronald van Kuijk Master

                Some less complex intra entity validations would be a nice first step. Things like : date A should be before date B. Element B is required if element A is not null. Has anyone written some 'generic' validators for these less complex validations

                • 5. Re: Complex validation
                  Pete Muir Master

                  [ur]http://jroller.com/page/jgilbert01?entry=extending_the_hibernate_validation_framework[/url]

                  This seems one approach - I would prefer to see EL used rather than JEXL and I'm not sure how it would tie into the JSF lifecycle (as the datamodel hasn't been updated in the process validations phase).

                  It strikes me that the big problem isn't actually *doing* complex validations but tying it into JSF.

                  • 6. Re: Complex validation
                    William E. Sollers III Newbie

                    Could you:

                    @ValidatorClass ( GreaterThanValidator.class )
                    @Retention ( RetentionPolicy.RUNTIME )
                    @Target ( { ElementType.METHOD } )
                    public @interface GreaterThan {
                    
                     String elExpr = null;
                     String mesg = "#{someMessageKey}";
                    
                    }
                    


                    And then in the validator class get a reference to the Seam interpolator and invoke the el-binding cast it to Comparable and compare it to the annotated data element.

                    Could do the same for less than, either or, just a thought for the simpler things. To do entity exists checks I use a custom interceptor that has access to the entity manager to do the queries.

                    I am curious about this as well as I have lot's of these kind of things floating around and would really like to standardize them and will research it further.