You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractFieldCommitWithInvalidValues.java 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package com.vaadin.tests.components.abstractfield;
  2. import com.vaadin.tests.components.TestBase;
  3. import com.vaadin.ui.Button;
  4. import com.vaadin.ui.Notification;
  5. import com.vaadin.v7.data.util.ObjectProperty;
  6. import com.vaadin.v7.data.validator.StringLengthValidator;
  7. import com.vaadin.v7.ui.TextField;
  8. public class AbstractFieldCommitWithInvalidValues extends TestBase {
  9. private TextField tf;
  10. @Override
  11. protected String getDescription() {
  12. return "Commiting a field with invalid values should throw an exception";
  13. }
  14. @Override
  15. protected Integer getTicketNumber() {
  16. return 2532;
  17. }
  18. @Override
  19. protected void setup() {
  20. tf = new TextField("A field, must contain 1-2 chars",
  21. new ObjectProperty<>("a"));
  22. tf.addValidator(
  23. new StringLengthValidator("Invalid length", 1, 2, false));
  24. tf.setBuffered(true);
  25. tf.setRequired(true);
  26. Button b = new Button("Commit", event -> {
  27. try {
  28. tf.commit();
  29. if (tf.isValid()) {
  30. getMainWindow().showNotification(
  31. "OK! Form validated and no error was thrown",
  32. Notification.TYPE_HUMANIZED_MESSAGE);
  33. } else {
  34. getMainWindow().showNotification(
  35. "Form is invalid but no exception was thrown",
  36. Notification.TYPE_ERROR_MESSAGE);
  37. }
  38. } catch (Exception e) {
  39. if (tf.isValid()) {
  40. getMainWindow().showNotification(
  41. "Form is valid but an exception was thrown",
  42. Notification.TYPE_ERROR_MESSAGE);
  43. } else {
  44. getMainWindow().showNotification(
  45. "OK! Error was thrown for an invalid input",
  46. Notification.TYPE_HUMANIZED_MESSAGE);
  47. }
  48. }
  49. });
  50. addComponent(tf);
  51. addComponent(b);
  52. }
  53. }