1 Reply Latest reply on Aug 15, 2007 7:26 AM by ellenzhao

    Handling context-based validation

      I have a "Claim" object. Whether or not it is valid depends on what the user is trying to do.

      My person object will require payee information to be valid for the purpose of payment, but not otherwise.

      Ideally, I'd do something like this:

       public void makePayment(Party payee) {
       Payee validator = new PayeeValidator();
       if (validator.isValid(payee) {
       ...
       }
       }
      


      At the UI level, I want to show

      Valid person: yes/no
      Valid payee: yes/no
      Valid employee: yes/no

      where the yes/no value is determined by calling the relevant validator with the same person object.

      This seems to be a limitation of the annotation approach. i.e. you can only specify a set of rules that apply in all contexts.

      Anyone else feel the same way? Am I missing something?

      Richard

        • 1. Re: Handling context-based validation
          ellenzhao

          You may want to try to make your validator object be aware of the current context/conversation/use case (pick a word which is appropriate for your situation). My own project does not have any validator object yet (all the constraint logic is either in entity beans or in the entity manager beans), but I imagine it is possible to put a field

          AppContext currentContext;
          


          or
          AppConversation currentConversation;


          or
          AppUseCase currentUseCase;


          in your validator. And also setter and getter for this field. Your other objects like Person object can call the setter to let the validator know the current context/conversation/use case before calling the validation method. So that the current current context/conversation/usecase information is available in your validator object. Conceptually it should work.

          You can put the names of all the app-specific context/conversation/use case in a enum to eliminate String typing errors in your java class. An example can be seen here:

          http://www.jboss.com/index.html?module=bb&op=viewtopic&t=115871