import java.util.Map;
import com.vaadin.data.Property;
+import com.vaadin.data.Validator.InvalidValueException;
import com.vaadin.event.FieldEvents;
import com.vaadin.event.FieldEvents.BlurEvent;
import com.vaadin.event.FieldEvents.BlurListener;
import com.vaadin.event.FieldEvents.FocusListener;
import com.vaadin.terminal.PaintException;
import com.vaadin.terminal.PaintTarget;
+import com.vaadin.terminal.UserError;
import com.vaadin.terminal.gwt.client.ui.VDateField;
import com.vaadin.terminal.gwt.client.ui.VPopupCalendar;
private boolean lenient = false;
+ private String dateString = null;
+
+ /**
+ * Was the last entered string parsable?
+ */
+ private boolean parsingSucceeded = true;
+
/**
* Determines if week numbers are shown in the date selector.
*/
* Constructs an empty <code>DateField</code> with no caption.
*/
public DateField() {
+ setInvalidAllowed(false);
}
/**
*/
public DateField(String caption) {
setCaption(caption);
+ setInvalidAllowed(false);
}
/**
* the Property to be edited with this editor.
*/
public DateField(Property dataSource) throws IllegalArgumentException {
+ setInvalidAllowed(false);
if (!Date.class.isAssignableFrom(dataSource.getType())) {
throw new IllegalArgumentException("Can't use "
+ dataSource.getType().getName()
* the Date value.
*/
public DateField(String caption, Date value) {
+ setInvalidAllowed(false);
setValue(value);
setCaption(caption);
}
target.addAttribute("type", type);
target.addAttribute(VDateField.WEEK_NUMBERS, isShowISOWeekNumbers());
+ target.addAttribute("parsable", parsingSucceeded);
// Gets the calendar
final Calendar calendar = getCalendar();
@Override
public void changeVariables(Object source, Map variables) {
super.changeVariables(source, variables);
+ setComponentError(null);
if (!isReadOnly()
&& (variables.containsKey("year")
// this enables analyzing invalid input on the server
Object o = variables.get("dateString");
- String dateString = null;
+ dateString = null;
if (o != null) {
dateString = o.toString();
}
if (newDate == null && dateString != null && !"".equals(dateString)) {
try {
- setValue(handleUnparsableDateString(dateString));
+ Date parsedDate = handleUnparsableDateString(dateString);
+ parsingSucceeded = true;
+ setValue(parsedDate, true);
+
/*
* Ensure the value is sent to the client if the value is
* set to the same as the previous (#4304). Does not repaint
*/
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;
+ /*
+ * Sets the component error to the Conversion Exceptions
+ * message. This can be overriden in
+ * handleUnparsableDateString.
+ */
+ setComponentError(new UserError(e.getLocalizedMessage()));
+
+ /*
+ * The value of the DateField should be null if an invalid
+ * value has been given. Not using setValue() since we do
+ * not want to cause the client side value to change.
+ */
+ parsingSucceeded = false;
+ setInternalValue(null);
+ fireValueChange(true);
}
} else if (newDate != oldDate
&& (newDate == null || !newDate.equals(oldDate))) {
+ parsingSucceeded = true;
setValue(newDate, true); // Don't require a repaint, client
// updates itself
}
*/
protected Date handleUnparsableDateString(String dateString)
throws Property.ConversionException {
- throw new Property.ConversionException();
+ throw new Property.ConversionException("Date format not recognized");
}
/* Property features */
setValue(newValue, false);
}
+ /*
+ * (non-Javadoc)
+ *
+ * @see com.vaadin.ui.AbstractField#setValue(java.lang.Object, boolean)
+ */
@Override
public void setValue(Object newValue, boolean repaintIsNotNeeded)
throws Property.ReadOnlyException, Property.ConversionException {
// Allows setting dates directly
if (newValue == null || newValue instanceof Date) {
- super.setValue(newValue, repaintIsNotNeeded);
+ try {
+ super.setValue(newValue, repaintIsNotNeeded);
+ parsingSucceeded = true;
+ } catch (InvalidValueException ive) {
+ // Thrown if validator fails
+ parsingSucceeded = false;
+ throw ive;
+ }
} else {
// Try to parse as string
final SimpleDateFormat parser = new SimpleDateFormat();
final Date val = parser.parse(newValue.toString());
super.setValue(val, repaintIsNotNeeded);
+ parsingSucceeded = true;
} catch (final ParseException e) {
- throw new Property.ConversionException(e.getMessage());
+ parsingSucceeded = false;
+ throw new Property.ConversionException(
+ "Date format not recognized");
}
}
}
requestRepaint();
}
+ /*
+ * (non-Javadoc)
+ *
+ * @see com.vaadin.ui.AbstractField#isEmpty()
+ */
+ @Override
+ protected boolean isEmpty() {
+ /*
+ * Logically isEmpty() should return false also in the case that the
+ * entered value is invalid.
+ */
+ return dateString == null || dateString.equals("");
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see com.vaadin.ui.AbstractField#isValid()
+ */
+ @Override
+ public boolean isValid() {
+ /*
+ * For the DateField to be valid it has to be parsable also
+ */
+ boolean parsable = isEmpty() || (!isEmpty() && getValue() != null);
+ return parsable && super.isValid();
+ }
}
--- /dev/null
+package com.vaadin.tests.components.datefield;
+
+import com.vaadin.data.Property;
+import com.vaadin.data.Property.ValueChangeEvent;
+import com.vaadin.data.validator.NullValidator;
+import com.vaadin.tests.components.TestBase;
+import com.vaadin.ui.DateField;
+
+@SuppressWarnings("serial")
+public class DefaultHandleUnparsableDateField extends TestBase {
+
+ @Override
+ protected void setup() {
+ final DateField date = new DateField("Default DateField");
+ date.setImmediate(true);
+ addComponent(date);
+ date.addListener(new Property.ValueChangeListener() {
+ public void valueChange(ValueChangeEvent event) {
+ if (date.isValid()) {
+ getMainWindow().showNotification(date.toString());
+ }
+
+ }
+ });
+
+ final DateField validated = new DateField("Validated Default DateField");
+ validated.setImmediate(true);
+ validated.addValidator(new NullValidator("Validator: Date is NULL",
+ false));
+ addComponent(validated);
+ }
+
+ @Override
+ protected String getDescription() {
+ return "By default the DateField should handle an unparsable date field without throwing an exception";
+ }
+
+ @Override
+ protected Integer getTicketNumber() {
+ return 4311;
+ }
+
+}