7 Replies Latest reply on Jun 3, 2008 3:12 AM by rhills

    JSF form (application) validation with SEAM

    javacoryd

      We have been using Seam for a while now and are very happy with it, but we have come across a validation use case which I'm stumped on how to implement in the framework.


      We would like to validate the domain in the invoke application phase by calling a set of validations.  These are business validations (rules) which incorporate possibly multiple fields in the domain.  At the same time, if we have a validation error we would like to link the error (message) back to a specific field on the UI form.


      We are using both the JSF and Seam validations in other parts of our application with great success, but they don't fit here since that validation is at the field level.


      Any suggestions on this one?


      Thanks,


      Cory.

        • 1. Re: JSF form (application) validation with SEAM
          christian.bauer

          Just do it in your actions, like this.

          • 2. Re: JSF form (application) validation with SEAM
            keithnaas

            If you want to do any fancy styling of the bad field values, you could even call FacesMessages.addToControl() with your input fields component id.  Note, that the component id will need to be fully qualified.  So for instance if the component id is username and it is inside a form with an id of form, the
            addToControl(form:username) should do the trick.  There is likely an easier way to do this now :)

            • 3. Re: JSF form (application) validation with SEAM
              javacoryd

              Thanks everyone for the reply.  This works great!


              One question on this one.  Will this solution work with fields under a ui:repeat?


              Thanks,


              Cory.

              • 4. Re: JSF form (application) validation with SEAM
                keithnaas

                Are you referring to the fully qualified component id?  If so, then no out of the box it won't work with input fields in any repeating container such as a ui:repeat or h:dataTable.  However, you can attempt to structure the componentId much the same way that the UIData does, namely to prepend the rowindex to the component id.  So if your component is username, it is in a ui:repeat and the row index is 2, use
                addToControl(form:2:username).

                • 5. Re: JSF form (application) validation with SEAM
                  jimk1723

                  Not to derail, but has anyone in forum-land tinkered with using Drools for this kind of non-invariant validation?


                  • 6. Re: JSF form (application) validation with SEAM
                    luke.maurer

                    Well, if you count login checking as form validation, then yes :-D


                    Come to think of it, maybe I'm not kidding - RuleBasedIdentity is a good example of a binary decision made using a quick Drools operation. I haven't done any performance testing, but AFAICT Drools is optimized for this sort of use case, so maybe a case could be made for using Drools for form validation. Certainly it's what I thought of when I read the OP's talk of business rules.

                    • 7. Re: JSF form (application) validation with SEAM
                      rhills

                      I have a similar problem, with a group of related checkboxes that the user must select one or more of.  I've got the validation working in the action, but I'm having trouble getting a validation error message to appear near the checkboxes.


                      The checkboxes are generated from a list via a h:selectManyCheckbox so trying to hook the message to one of those would be kludgy and fragile.  They're inside their own rich:simpleTogglePanel, but the addToControl(..) method seems uninterested in attaching to an element that isn't an input field.


                      Is there some way of defining a named anchor point that I can target with a RichFaces method, or will I have to fudge it by creating a dedicated attribute on my action to hold/display my error message?