package com.itmill.toolkit.data;
+import com.itmill.toolkit.data.Validator.InvalidValueException;
import com.itmill.toolkit.terminal.ErrorMessage;
import com.itmill.toolkit.terminal.PaintException;
import com.itmill.toolkit.terminal.PaintTarget;
* @throws SourceException
* if the operation fails because of an exception is thrown by
* the data source. The cause is included in the exception.
+ * @throws InvalidValueException
+ * if the operation fails because validation is enabled and the
+ * values do not validate
*/
- public void commit() throws SourceException;
+ public void commit() throws SourceException, InvalidValueException;
/**
* Discards all changes since last commit. The object updates its value from
* @throws SourceException
* If the operation fails because of an exception is thrown by
* the data source.
+ * @throws InvalidValueException
+ * If the implicit commit operation fails because of a
+ * validation error.
*
*/
- public void setWriteThrough(boolean writeThrough) throws SourceException;
+ public void setWriteThrough(boolean writeThrough) throws SourceException,
+ InvalidValueException;
/**
* Tests if the object is in read-through mode. If the object is in
--- /dev/null
+package com.itmill.toolkit.tests.components.abstractfield;
+
+import com.itmill.toolkit.data.util.ObjectProperty;
+import com.itmill.toolkit.data.validator.StringLengthValidator;
+import com.itmill.toolkit.tests.components.TestBase;
+import com.itmill.toolkit.ui.Button;
+import com.itmill.toolkit.ui.TextField;
+import com.itmill.toolkit.ui.Button.ClickEvent;
+import com.itmill.toolkit.ui.Button.ClickListener;
+import com.itmill.toolkit.ui.Window.Notification;
+
+public class AbstractFieldCommitWithInvalidValues extends TestBase {
+
+ private TextField tf;
+
+ @Override
+ protected String getDescription() {
+ return "Commiting a field with invalid values should throw an exception";
+ }
+
+ @Override
+ protected Integer getTicketNumber() {
+ return 2532;
+ }
+
+ @Override
+ protected void setup() {
+ tf = new TextField("A field, must contain 1-2 chars",
+ new ObjectProperty("a"));
+ tf
+ .addValidator(new StringLengthValidator("Invalid length", 1, 2,
+ false));
+ tf.setWriteThrough(false);
+ tf.setRequired(true);
+
+ Button b = new Button("Commit", new ClickListener() {
+
+ public void buttonClick(ClickEvent event) {
+ try {
+ tf.commit();
+ if (tf.isValid()) {
+ getMainWindow().showNotification(
+ "OK! Form validated and no error was thrown",
+ Notification.TYPE_HUMANIZED_MESSAGE);
+ } else {
+ getMainWindow().showNotification(
+ "Form is invalid but no exception was thrown",
+ Notification.TYPE_ERROR_MESSAGE);
+ }
+ } catch (Exception e) {
+ if (tf.isValid()) {
+ getMainWindow().showNotification(
+ "Form is valid but an exception was thrown",
+ Notification.TYPE_ERROR_MESSAGE);
+ } else {
+ getMainWindow().showNotification(
+ "OK! Error was thrown for an invalid input",
+ Notification.TYPE_HUMANIZED_MESSAGE);
+
+ }
+ }
+ }
+
+ });
+
+ addComponent(tf);
+ addComponent(b);
+ }
+}
--- /dev/null
+package com.itmill.toolkit.tests.components.form;
+
+import com.itmill.toolkit.data.validator.StringLengthValidator;
+import com.itmill.toolkit.tests.components.TestBase;
+import com.itmill.toolkit.ui.Button;
+import com.itmill.toolkit.ui.Form;
+import com.itmill.toolkit.ui.TextField;
+import com.itmill.toolkit.ui.Button.ClickEvent;
+import com.itmill.toolkit.ui.Button.ClickListener;
+import com.itmill.toolkit.ui.Window.Notification;
+
+public class FormCommitWithInvalidValues extends TestBase {
+
+ private Form form;
+
+ @Override
+ protected String getDescription() {
+ return "Commiting a form with invalid values should throw an exception";
+ }
+
+ @Override
+ protected Integer getTicketNumber() {
+ return 2466;
+ }
+
+ @Override
+ protected void setup() {
+ form = new Form();
+ TextField tf = new TextField("A field, must contain 1-2 chars");
+ tf
+ .addValidator(new StringLengthValidator("Invalid length", 1, 2,
+ false));
+ tf.setRequired(true);
+
+ form.addField("a", tf);
+
+ Button b = new Button("Commit", new ClickListener() {
+
+ public void buttonClick(ClickEvent event) {
+ try {
+ form.commit();
+ if (form.isValid()) {
+ getMainWindow().showNotification(
+ "OK! Form validated and no error was thrown",
+ Notification.TYPE_HUMANIZED_MESSAGE);
+ } else {
+ getMainWindow().showNotification(
+ "Form is invalid but no exception was thrown",
+ Notification.TYPE_ERROR_MESSAGE);
+ }
+ } catch (Exception e) {
+ if (form.isValid()) {
+ getMainWindow().showNotification(
+ "Form is valid but an exception was thrown",
+ Notification.TYPE_ERROR_MESSAGE);
+ } else {
+ getMainWindow().showNotification(
+ "OK! Error was thrown for an invalid input",
+ Notification.TYPE_HUMANIZED_MESSAGE);
+
+ }
+ }
+ }
+
+ });
+
+ addComponent(form);
+ addComponent(b);
+ }
+
+}
import com.itmill.toolkit.data.Property;
import com.itmill.toolkit.data.Validatable;
import com.itmill.toolkit.data.Validator;
+import com.itmill.toolkit.data.Validator.InvalidValueException;
import com.itmill.toolkit.terminal.CompositeErrorMessage;
import com.itmill.toolkit.terminal.ErrorMessage;
import com.itmill.toolkit.terminal.PaintException;
* Saves the current value to the data source Don't add a JavaDoc comment
* here, we use the default documentation from the implemented interface.
*/
- public void commit() throws Buffered.SourceException {
- if (dataSource != null && (isInvalidCommitted() || isValid())
- && !dataSource.isReadOnly()) {
- final Object newValue = getValue();
- try {
+ public void commit() throws Buffered.SourceException, InvalidValueException {
+ if (dataSource != null && !dataSource.isReadOnly()) {
+ if ((isInvalidCommitted() || isValid())) {
+ final Object newValue = getValue();
+ try {
- // Commits the value to datasource.
- dataSource.setValue(newValue);
+ // Commits the value to datasource.
+ dataSource.setValue(newValue);
- } catch (final Throwable e) {
+ } catch (final Throwable e) {
- // Sets the buffering state.
- currentBufferedSourceException = new Buffered.SourceException(
- this, e);
- requestRepaint();
+ // Sets the buffering state.
+ currentBufferedSourceException = new Buffered.SourceException(
+ this, e);
+ requestRepaint();
- // Throws the source exception.
- throw currentBufferedSourceException;
+ // Throws the source exception.
+ throw currentBufferedSourceException;
+ }
+ } else {
+ /* An invalid value and we don't allow them, throw the exception */
+ validate();
}
}
* implemented interface.
*/
public void setWriteThrough(boolean writeTrough)
- throws Buffered.SourceException {
+ throws Buffered.SourceException, InvalidValueException {
if (writeTroughMode == writeTrough) {
return;
}
// Only commit on valid state if so requested
if (!isInvalidCommitted() && !isValid()) {
+ /*
+ * The values are not ok and we are told not to commit invalid
+ * values
+ */
if (validationVisibleOnCommit) {
setValidationVisible(true);
}
- return;
+
+ // Find the first invalid value and throw the exception
+ validate();
}
// Try to commit all
* JavaDoc comment here, we use the default one from the interface.
*/
@Override
- public void setWriteThrough(boolean writeThrough) {
+ public void setWriteThrough(boolean writeThrough) throws SourceException,
+ InvalidValueException {
if (writeThrough != this.writeThrough) {
this.writeThrough = writeThrough;
for (final Iterator i = propertyIds.iterator(); i.hasNext();) {