Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

TestValidators.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package com.vaadin.tests.validation;
  2. import com.vaadin.legacy.data.Validator;
  3. import com.vaadin.legacy.data.validator.LegacyAbstractStringValidator;
  4. import com.vaadin.legacy.data.validator.LegacyCompositeValidator;
  5. import com.vaadin.legacy.data.validator.LegacyCompositeValidator.CombinationMode;
  6. import com.vaadin.legacy.data.validator.LegacyDoubleValidator;
  7. import com.vaadin.legacy.data.validator.LegacyEmailValidator;
  8. import com.vaadin.legacy.data.validator.LegacyIntegerValidator;
  9. import com.vaadin.legacy.data.validator.LegacyRegexpValidator;
  10. import com.vaadin.legacy.data.validator.LegacyStringLengthValidator;
  11. import com.vaadin.tests.components.TestBase;
  12. import com.vaadin.ui.Button;
  13. import com.vaadin.ui.Button.ClickEvent;
  14. import com.vaadin.ui.Button.ClickListener;
  15. import com.vaadin.ui.Form;
  16. import com.vaadin.ui.Notification;
  17. import com.vaadin.ui.TextField;
  18. import com.vaadin.ui.VerticalLayout;
  19. public class TestValidators extends TestBase {
  20. @Override
  21. protected Integer getTicketNumber() {
  22. return 680;
  23. }
  24. @Override
  25. protected String getDescription() {
  26. return "This test verifies that various validators work correctly";
  27. }
  28. @Override
  29. public void setup() {
  30. final Form form = new Form(new VerticalLayout());
  31. // simple validators
  32. TextField tf = new TextField("A field, must contain 1-2 chars");
  33. tf.addValidator(
  34. new LegacyStringLengthValidator("Invalid length", 1, 2, false));
  35. tf.setRequired(true);
  36. tf.setValue("ab");
  37. form.addField("a", tf);
  38. tf = new TextField("A field, must contain an integer");
  39. tf.addValidator(new LegacyIntegerValidator("Invalid integer {0}"));
  40. tf.setRequired(true);
  41. tf.setValue("123");
  42. form.addField("b", tf);
  43. tf = new TextField("A field, must contain an integer or be empty");
  44. tf.addValidator(new LegacyIntegerValidator("Invalid integer {0}"));
  45. tf.setValue("-321");
  46. form.addField("c", tf);
  47. tf = new TextField(
  48. "A field, must contain a floating point number or be empty");
  49. tf.addValidator(new LegacyDoubleValidator("Invalid double {0}"));
  50. tf.setValue("-123.45e6");
  51. form.addField("d", tf);
  52. tf = new TextField(
  53. "A field, must contain an e-mail address or be empty");
  54. tf.addValidator(new LegacyEmailValidator("Invalid e-mail address {0}"));
  55. tf.setValue("a.b@example.com");
  56. form.addField("e", tf);
  57. // regular expressions
  58. tf = new TextField(
  59. "A field, must match the regular expression a.*b.*c");
  60. tf.addValidator(new LegacyRegexpValidator("a.*b.*c",
  61. "{0} does not match the regular expression"));
  62. tf.setValue("aagsabeqgc");
  63. form.addField("f", tf);
  64. tf = new TextField(
  65. "A field, must contain the regular expression a.*b.*c");
  66. tf.addValidator(new LegacyRegexpValidator("a.*b.*c", false,
  67. "{0} does not contain the regular expression"));
  68. tf.setValue("aagsabeqgc");
  69. form.addField("g", tf);
  70. tf = new TextField(
  71. "A field, must match the regular expression ^a.*b.*c$");
  72. tf.addValidator(new LegacyRegexpValidator("^a.*b.*c$", false,
  73. "{0} does not match the regular expression with ^ and $"));
  74. tf.setValue("aagsabeqgc");
  75. form.addField("h", tf);
  76. tf = new TextField(
  77. "A field, must contain the regular expression ^a.*b.*c$");
  78. tf.addValidator(new LegacyRegexpValidator("^a.*b.*c$", false,
  79. "{0} does not contain the regular expression with ^ and $"));
  80. tf.setValue("aagsabeqgc");
  81. form.addField("i", tf);
  82. // TODO CompositeValidator
  83. tf = new TextField(
  84. "A field, must be a floating point number with 4-5 chars");
  85. LegacyCompositeValidator cv = new LegacyCompositeValidator(
  86. CombinationMode.AND,
  87. "The field must contain a floating point number with 4-5 characters");
  88. cv.addValidator(new LegacyStringLengthValidator(
  89. "String length of '{0}' should be 4-5 characters", 4, 5,
  90. false));
  91. cv.addValidator(new LegacyDoubleValidator(
  92. "{0} must be a floating point number"));
  93. tf.addValidator(cv);
  94. tf.setValue("12.34");
  95. form.addField("j", tf);
  96. tf = new TextField(
  97. "A field, must be a floating point number or 4-5 chars");
  98. cv = new LegacyCompositeValidator(CombinationMode.OR,
  99. "The field must contain a floating point or with 4-5 characters");
  100. cv.addValidator(new LegacyStringLengthValidator(
  101. "String length of '{0}' should be 4-5 characters", 4, 5,
  102. false));
  103. cv.addValidator(new LegacyDoubleValidator(
  104. "{0} must be a floating point number"));
  105. tf.addValidator(cv);
  106. tf.setValue("12.34g");
  107. form.addField("jb", tf);
  108. // Postal code that must be 5 digits (10000-99999).
  109. tf = new TextField("Postal Code");
  110. tf.setColumns(5);
  111. // Create the validator - this would be even easier with RegexpValidator
  112. Validator postalCodeValidator = new LegacyAbstractStringValidator(
  113. "Postal code must be a number 10000-99999.") {
  114. @Override
  115. protected boolean isValidValue(String value) {
  116. return value.matches("[1-9][0-9]{4}");
  117. }
  118. };
  119. tf.addValidator(postalCodeValidator);
  120. tf.setValue("12345");
  121. form.addField("k", tf);
  122. Button b = new Button("Commit", new ClickListener() {
  123. @Override
  124. public void buttonClick(ClickEvent event) {
  125. try {
  126. form.commit();
  127. if (form.isValid()) {
  128. getMainWindow().showNotification(
  129. "OK! Form validated and no error was thrown",
  130. Notification.TYPE_HUMANIZED_MESSAGE);
  131. } else {
  132. getMainWindow().showNotification(
  133. "Form is invalid but no exception was thrown",
  134. Notification.TYPE_ERROR_MESSAGE);
  135. }
  136. } catch (Exception e) {
  137. if (form.isValid()) {
  138. getMainWindow().showNotification(
  139. "Form is valid but an exception was thrown",
  140. Notification.TYPE_ERROR_MESSAGE);
  141. } else {
  142. getMainWindow().showNotification(
  143. "OK! Error was thrown for an invalid input",
  144. Notification.TYPE_HUMANIZED_MESSAGE);
  145. }
  146. }
  147. }
  148. });
  149. addComponent(form);
  150. addComponent(b);
  151. }
  152. }