Form validation with inter-dependant fields

Hi there,

I try to create a form with two date fields (from and to). I want to validate the from date is before the end date. But I’m stuck.

The problem is that the error messages appear ok, but then I cannot change the values of the fields anymore. Whatever I enter, the value of the date field jumps back to the value I entered initially.

I’m using FormFieldFactory, here is the code that creates the 2 date fields:

if (propertyId.equals ("validFrom")) {
   DateField validfrom = new DateField ("Valid From");
   validfrom.setDateFormat ("yyyy-MM-dd");
   GregorianCalendar now = new GregorianCalendar ();
   validfrom.setResolution (PopupDateField.RESOLUTION_DAY);
   validfrom.addValidator (validator);
   // save this field
   ReasonDate.this.validfrom = validfrom;
   f = validfrom;
else if (propertyId.equals ("validTo")) {
   DateField validTo = new DateField ("Valid To");
   validTo.setDateFormat ("yyyy-MM-dd");
   validTo.setResolution (PopupDateField.RESOLUTION_DAY);
   ReasonDate.this.validto = validTo;
   validto.addValidator (validator);
   f = validTo;

Since the validation of one field depends on the values of another field, I access the data bound to the fields in the isValid() method of the validator:

		public boolean isValid (Object value)
			if (validto == null || validfrom == null)
				return true;

			ReasonValidity rv = (ReasonValidity) ReasonDate.this.getPropertyDataSource ().getValue ();
			// ReasonValidity rv = (ReasonValidity) value;
			// Date dateValidTo = (Date) validto.getValue ();
			// Date dateValidFrom = (Date) validfrom.getValue ();
			Date dateValidTo = rv.getValidTo ();
			Date dateValidFrom = rv.getValidFrom ();

			// if either is null there is nothing to check
			if (dateValidTo == null || dateValidFrom == null)
				return true;

			if (dateValidFrom.compareTo (dateValidTo) > 0)
				return false;

			return true;

Here is the code that creates and configures the form:

		businessReasonAndValidity = new ReasonDate ();		
		businessReasonAndValidity.setPropertyDataSource ( ... some ObjectProperty ... );
		//businessReasonAndValidity.setWriteThrough (true);
		businessReasonAndValidity.setImmediate (true);

So what happens if validto is before validfrom is that I get the exclamation mark icon next to the form field but if I select a correct validto date after that (see attached screenshot) then the date jumps back to 2011-05-18. Now, there is no way to get valid input into the form. I cannot get past this step.

What am I doing wrong?


Same problem here:

Has anyone an idea, how this could work?

Best regards, André


I’ve found a solution that works for me… see here:

Best regards, André