4 Replies Latest reply on Jul 22, 2009 4:36 PM by coralfe

    validating that at least one of n fields are not null on submission of form

    gonorrhea

      So I have a functional requirement as follows:


      There are four fields in a HtmlForm in my xhtml page.  One is always required.  Of the other three, one is always required as well (doesn't matter which one).  So basically two fields total must be non-null upon submission of the form.


      I started writing a custom Seam validator class that implements javax.faces.validator.Validator interface but I'm not sure that's the best approach.


      I'm also considering simply evaluating the context variables in my SFSB in the action method for the form submission and adding a message to the facesMessages instance if all of the three fields are null.


      Any tips on this?  thx.

        • 1. Re: validating that at least one of n fields are not null on submission of form
          lvdberg

          Arbi,


          This kind of cross-property validation is best done in a action or Home component where you add theses business rules., using a valueChangeListenetr or including ajax-support tags if you're using the standard elements.



          Leo


          • 2. Re: validating that at least one of n fields are not null on submission of form
            gonorrhea

            Thx for the reply.  I have used <a4j:support event="onblur"> in the past with Seam validators.


            In this case, it's not a dynamic (live) validation, so I did the following for the search action method in my SFSB:


            public void search(){
                      //first check to see if item number, recovery status, equipment type are null; requirement is at least one of them to be non-null upon submission...
                      if ( (itemNumber == null || itemNumber.equals("")) && selectedListValueForRecoveryStatus == null && selectedListValueForEquipmentType == null)
                           facesMessages.add(FacesMessage.SEVERITY_ERROR, "You must input at least one of the following fields: item number, recovery status, equipment type", null);
                      else {
                           //continue processing
                           getData();
                      }
                 }

            • 3. Re: validating that at least one of n fields are not null on submission of form
              lvdberg

              This is what I meant.
              We are trying to solve a similar problem with uniqueness of beans. As oon as we have something re-usable, we will send it to this forum.


              Leo


              • 4. Re: validating that at least one of n fields are not null on submission of form
                coralfe

                As per the el solution here you could probably
                use a similar el expression using or.


                Something like


                <h:inputText value="#{userCRUD.selectedRow.username}" size="30" required="#{!empty param['user_addEditForm:field1'] or !empty param['user_addEditForm:field1\2']}"  id="user"/>