Revision history [back]

click to hide/show revision 1
initial version

answered 2012-11-29 20:29:37 +0800

rdgrimes gravatar image rdgrimes

I have something similar with a "from" and "to" date wherein, when the from date is changed, I want to change the value and constraint of the "to" date box so that the "to" date must be after the from date and within a month thereof.

ZUL:

<row>
    <cell>
        <label value="Date From:" sclass="strongLabel" ></label>
    </cell>
    <cell colspan="4">
        <datebox id="fromDate" constraint="no future" value="@bind(vm.fromDate)" ></datebox>
        <space width="10px" ></space>                   
        <label value="to:" sclass="strongLabel" ></label>
        <space width="10px" ></space>                   
        <datebox id="toDate" constraint="@bind(vm.toDateConstraint)" value="@bind(vm.toDate)" ></datebox>
        <space width="5px" ></space>                    
        <button id="submit" dir="reverse" 
                image="assets/images/arrow-right.png"
                label="go" onClick="@command('showCreditMemoSummary')" ></button>
    </cell>
</row>

ViewModel:

    public SimpleDateConstraint getToDateConstraint()
    {
        Calendar futureDate = new GregorianCalendar();
        futureDate.setTime(fromDate);
        futureDate.add(Calendar.MONTH, 1);

        toDateConstraint = 
                new SimpleDateConstraint("between " + CONSTRAINT_DATE.format(fromDate) + " and " + CONSTRAINT_DATE.format(futureDate.getTime()));

        return toDateConstraint;
    }

    public void setToDateConstraint(SimpleDateConstraint constraint)
    {
        toDateConstraint = constraint;
    }

    public Date getFromDate()
    {
        // First time, set to component preference definition.      
        if (fromDate == null)
        {
            Calendar now = getNow();            
            fromDate = now.getTime();

            if (componentPreference != null)
            {
                if (componentPreference.getFromValueAgo().compareTo(0L) == 0)
                {
                    fromDate.setTime(now.getTimeInMillis() - (2 * MS_PER_DAY));
                } else
                {
                    long prefMS = componentPreference.getFromValueAgo() * ((long) MS_PER_DAY);
                    fromDate.setTime(now.getTimeInMillis() - prefMS);
                }
            } else
            {
                fromDate.setTime(now.getTimeInMillis() - (2 * MS_PER_DAY));
            }
        }

        return fromDate;
    }

    public void setFromDate(Date fromDate)
    {
        this.fromDate = fromDate;

        // Adjust to date based on from date.
        Calendar futureDate = new GregorianCalendar();
        futureDate.setTime(getToDateConstraint().getBeginDate());
        futureDate.add(Calendar.DATE, 2);
        toDate = futureDate.getTime();

    }

    public Date getToDate()
    {
        // First time, set to component preference definition.
        if (toDate == null)
        {
            Calendar now = getNow();            
            toDate = now.getTime();

            if (componentPreference != null)
            {
                if (componentPreference.getToValueAgo().compareTo(0L) == 0)
                {
                    toDate.setTime(now.getTimeInMillis());
                } else
                {
                    long prefMS = componentPreference.getToValueAgo() * ((long) MS_PER_DAY);
                    toDate.setTime(now.getTimeInMillis() - prefMS);
                }
            }
        }

        return toDate;
    }

    public void setToDate(Date toDate)
    {
        this.toDate = toDate;
    }

The key here is that, in the setFromDate() method, I change the toDateConstraint and force the toDate itself to a value that is after the fromDate. This triggers the BindComposer to update the ZUL toDate box with the appropriate value and constraint.

Note that, if you do it this way, you completely avoid the ability of the user to select an incorrect date range.

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More