7 Replies Latest reply on Jul 3, 2008 9:53 AM by yetidesalpes

    validator for two components at once

    marx3

      i would like to validate to dates - first cannot be before the second. As I see there is no possibility to write validator with additional parameters?

        • 1. Re: validator for two components at once
          marx3

          two dates

          For example first field 2007-01-01 must be before the second one 2007-10-10


          Is it possible to write validator, which gets values from two different components?

          • 2. Re: validator for two components at once
            mail.micke

            Tomahawk seems to have a validator that does this, perhaps it can be used or at least used as a reference when implementing your own.


            Take a look here SVN validateEqual


            • 3. Re: validator for two components at once
              tom_goring

              You could for your entities:



              @AssertTrue(message="Message Text")
              @Transient
              public boolean isValid() {
              // .... 
              }


              • 4. Re: validator for two components at once
                yetidesalpes

                I did something like that :



                @Name("ratePeriodValidator") 
                @org.jboss.seam.annotations.faces.Validator
                public class RatePeriodValidator implements Serializable, Validator
                {
                        @In(create = true)
                        RatePeriodHome ratePeriodHome;
                        
                        @In("#{messages['date.to.before.from']}") 
                        private String dateToBeforeFrom;
                        
                  public void validate(FacesContext arg0, UIComponent arg1, Object value) throws ValidatorException
                  {
                                if (!((Date) value).after(ratePeriodHome.getInstance().getBegin()))
                                                throw new ValidatorException(new FacesMessage(dateToBeforeFrom));               
                  }
                }




                and in the view :


                <rich:calendar 
                    id="end" 
                    validator="ratePeriodValidator" 
                    value="#{ratePeriodHome.instance.end}">
                  <s:validate/>
                </rich:calendar>



                Hope this helps !

                • 5. Re: validator for two components at once
                  stephen

                  Huh? How can that work?
                  At the time your validator runs, the values have not yet been saved to the entity, so ratePeriodHome.getInstance().getBegin() won't return the current (local) value to which you need to compare against.
                  Or maybe I am missing something?

                  • 6. Re: validator for two components at once
                    marx3

                    I did it in different way: i've set label property in richcalendar component (i don't see it used anywhere) like ">toDate". In validator i read this property. First sign means which date should be bigger. The rest is used to lookup second component from facesContext, then fire converter from this object (converter.getAsObject) to read second value. Now I have two values and I can compare them.


                    It's hack, but works. I've tried with


                    <f:param ... value="toDate">



                    inside <f:validator> but I couldn't read it in validator. Maybe there is better way to pass name of second field to validator...?

                    • 7. Re: validator for two components at once
                      yetidesalpes

                      Oops, I forgot that , in my case, the getBegin() date is computed and not editable...


                      Sorry !