您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

AbstractFieldCommitWithInvalidValues.java 2.3KB

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