From: Artur Signell Date: Fri, 12 Mar 2010 13:41:16 +0000 (+0000) Subject: Test case and fix for X-Git-Tag: 6.7.0.beta1~1938 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=b924ba2d32d57098433dbb07b111a642b2c37e19;p=vaadin-framework.git Test case and fix for * #4263 DateField - returning valid Date in handleUnparsableDateString method works only at the first attempt and * #4304 Resetting the value in DateField.handleUnparsableDate does not update the client side text svn changeset:11820/svn branch:6.3 --- diff --git a/src/com/vaadin/ui/DateField.java b/src/com/vaadin/ui/DateField.java index 0800537fae..3c21630f0e 100644 --- a/src/com/vaadin/ui/DateField.java +++ b/src/com/vaadin/ui/DateField.java @@ -120,12 +120,6 @@ public class DateField extends AbstractField implements */ private String dateFormat; - /** - * Read-only content of an VTextualDate field - null for other types of date - * fields. - */ - private String dateString; - private boolean lenient = false; /* Constructors */ @@ -280,15 +274,13 @@ public class DateField extends AbstractField implements // Old and new dates final Date oldDate = (Date) getValue(); - final String oldDateString = dateString; Date newDate = null; // this enables analyzing invalid input on the server Object o = variables.get("dateString"); + String dateString = null; if (o != null) { dateString = o.toString(); - } else { - dateString = null; } // Gets the new date in parts @@ -348,9 +340,22 @@ public class DateField extends AbstractField implements newDate = cal.getTime(); } - if (newDate == null && dateString != null && !"".equals(dateString) - && !dateString.equals(oldDateString)) { - setValue(handleUnparsableDateString(dateString)); + if (newDate == null && dateString != null && !"".equals(dateString)) { + try { + setValue(handleUnparsableDateString(dateString)); + /* + * Ensure the value is sent to the client if the value is + * set to the same as the previous (#4304). Does not repaint + * if handleUnparsableDateString throws an exception. In + * this case the invalid text remains in the DateField. + */ + requestRepaint(); + } catch (ConversionException e) { + // FIXME: Should not throw the exception but set an error + // message for the field. And should retain the entered + // value. + throw e; + } } else if (newDate != oldDate && (newDate == null || !newDate.equals(oldDate))) { setValue(newDate, true); // Don't require a repaint, client diff --git a/tests/src/com/vaadin/tests/components/datefield/DateFieldUnparsableDate.java b/tests/src/com/vaadin/tests/components/datefield/DateFieldUnparsableDate.java new file mode 100644 index 0000000000..b0c06acb7a --- /dev/null +++ b/tests/src/com/vaadin/tests/components/datefield/DateFieldUnparsableDate.java @@ -0,0 +1,92 @@ +package com.vaadin.tests.components.datefield; + +import java.util.Date; + +import com.vaadin.tests.components.TestBase; +import com.vaadin.ui.DateField; + +public class DateFieldUnparsableDate extends TestBase { + + public class MyDateField extends DateField { + public MyDateField(String caption) { + super(caption); + } + + @Override + protected Date handleUnparsableDateString(String dateString) + throws ConversionException { + return (Date) getValue(); + } + } + + public class MyDateField2 extends DateField { + public MyDateField2(String caption) { + super(caption); + } + + @Override + protected Date handleUnparsableDateString(String dateString) + throws ConversionException { + return null; + } + } + + public class MyDateField3 extends DateField { + public MyDateField3(String caption) { + super(caption); + } + + @Override + protected Date handleUnparsableDateString(String dateString) + throws ConversionException { + throw new ConversionException("You should not enter invalid dates!"); + } + } + + public class MyDateField4 extends DateField { + public MyDateField4(String caption) { + super(caption); + } + + @Override + protected Date handleUnparsableDateString(String dateString) + throws ConversionException { + if (dateString != null && dateString.equals("today")) { + return new Date(); + } + throw new ConversionException("You should not enter invalid dates!"); + } + } + + @Override + protected void setup() { + MyDateField df = new MyDateField( + "Returns the old value for invalid dates"); + df.setImmediate(true); + addComponent(df); + + MyDateField2 df2 = new MyDateField2("Returns empty for invalid dates"); + df2.setImmediate(true); + addComponent(df2); + + MyDateField3 df3 = new MyDateField3( + "Throws an exception for invalid dates"); + df3.setImmediate(true); + addComponent(df3); + + MyDateField4 df4 = new MyDateField4("Can convert 'today'"); + df4.setImmediate(true); + addComponent(df4); + + } + + @Override + protected String getDescription() { + return "DateFields in various configurations (according to caption). All handle unparsable dates differently"; + } + + @Override + protected Integer getTicketNumber() { + return 4236; + } +}