Vaadin中OK按钮点击无响应问题求助及多邮箱输入咨询
Hey there! Let's tackle your two problems one by one—first the unresponsive OK button, then handling multiple semicolon-separated emails.
1. Why Your OK Button Isn't Working
Looking at your code, two key issues are blocking the OK button's functionality:
a. The isValid() Method Always Returns False
Your override of isValid() has a hardcoded return false;—this means every time you click OK, the confirmed() method will hit if (isValid()) and fail, so it never triggers the listener.dialogConfirmed(email) or closes properly.
b. Your Binder Isn't Properly Set Up
You added an email validator to the Binder, but you never:
- Bound the text field to your
Personclass'semailproperty - Triggered validation checks when needed
Fixed Code Snippets
First, update your doFieldValidations() to properly bind the field and add validation:
@Override protected void doFieldValidations() { // Bind the text field to the Person's email property binder.forField(efield) .withValidator(new EmailValidator(ENTER_VALID_EMAIL_ADDRESS)) .bind(Person::getEmail, Person::setEmail); }
Then fix the isValid() method to use the Binder's validation:
@Override protected boolean isValid() { // Trigger Binder validation and return the result try { binder.writeBean(email); return true; } catch (ValidationException e) { LOGGER.error("Validation failed for email input", e); return false; } }
Also, in your setEmail() method, make sure to load the data into the Binder:
public void setEmail(Person email) { LOGGER.info("Loading Email " + email.getPersonId() + " into email dialog window"); this.email = email; // Load the person's email into the binder/field binder.readBean(email); }
With these changes, the OK button will properly validate the input and trigger the listener when valid.
2. Handling Multiple Semicolon-Separated Emails
To allow multiple emails separated by semicolons, you'll need a custom validator that splits the input and checks each email individually. Here's how to adjust your code:
Update the Validator in doFieldValidations()
@Override protected void doFieldValidations() { binder.forField(efield) .withValidator(input -> { if (StringUtils.isEmpty(input)) { return true; // Allow empty if needed, adjust based on your requirements } // Split input by semicolons, trim whitespace from each part String[] emails = input.split(";"); for (String email : emails) { String trimmedEmail = email.trim(); if (!trimmedEmail.isEmpty() && !EmailValidator.isValidEmail(trimmedEmail)) { return false; } } return true; }, "Please enter valid semicolon-separated email addresses") .bind(Person::getEmail, Person::setEmail); }
Storing Multiple Emails (Optional)
If your Person class should store a list of emails instead of a single string, you can modify the confirmed() method to split the input and set the list:
@Override protected boolean confirmed() { if (!isEmpty()) { if (isValid()) { if (email == null) { email = new Person(); } // Split the input into a list of trimmed emails String[] emailArray = efield.getValue().split(";"); List<String> emailList = Arrays.stream(emailArray) .map(String::trim) .filter(s -> !s.isEmpty()) .collect(Collectors.toList()); // Assuming Person has a setEmails(List<String>) method email.setEmails(emailList); listener.dialogConfirmed(email); } else { return false; } } return true; }
内容的提问来源于stack exchange,提问作者AlfaBet

