官术网_书友最值得收藏!

Time for action – validating user input

We can run a TestSet instance only if the user selects one. Follow these steps to add a proper validation:

  1. Implement your initButton method to match this code:
    private void initButton() {
      button.addClickListener(new ClickListener() {
        @Override
        public void buttonClick(ClickEvent event) {
          if(isValid()) {
            runSelectedTest();
          }
        }
      });
    }
  2. Now implement your validate method. Here is how we validate:
    public boolean isValid() {
      combo.setComponentError(null);
      textField.setComponentError(null);
      
      boolean isValid = true;
      
      if(combo.getValue() == null) {
        combo.setComponentError(
            new UserError("Select a test from the list."));
        isValid = false;
      }
      
      if(textField.getValue().toString().isEmpty()) {
        textField.setComponentError(new UserError(
            "You must introduce the number of iterations to execute"));
    
        isValid = false;
      }
      
      return isValid;
    }

What just happened?

For the button listener, it's the same as in previous chapter. We have a button and we want to respond to click events, so we add ClickListener, using an anonymous class.

As you can see, if the isValid method returns true, we proceed to run the selected test. First two lines of the method are for clearing any error we may have added to the components in previous executions of the isValid method itself. Then, we declare a flag that will tell us whether the components have valid values or not. If the selected value in combo is null, we add an error message to the component using the following code line:

combo.setComponentError(new UserError("Select a test from the list."));

setComponentError expects an instance of ErrorMessage. UserError is an implementation of the ErrorMessage interface that allows us to show an error message on the component. Usually, the component will show an error indicator. If the user places the mouse cursor over the component, a tooltip will appear showing the error text:

A similar logic is used to show an error over textField if the users left it empty.

Note

Vaadin comes with three flavors of errors, UserError, SystemError, and CompositeErrorMessage, as follows:

  • UserError is an error that we, programmers, generate as a result of validations
  • SystemError is used where there is a problem in the system (for example, an uncaught exception)
  • CompositeErrorMessage allows you to show multiple error messages

Layout spacing

We have our components ready. We know how they respond to events (clicks and value changes) and how they communicate. We still have to know how the tests are executed and how the results are shown. But first, let's incorporate all those components into the main layout.

主站蜘蛛池模板: 视频| 安丘市| 荔波县| 津市市| 安塞县| 湘潭市| 肥西县| 宿州市| 静安区| 高碑店市| 酒泉市| 江川县| 全州县| 望江县| 特克斯县| 米林县| 平舆县| 卢湾区| 博客| 林州市| 固原市| 丹江口市| 寿阳县| 台山市| 鹿泉市| 益阳市| 长白| 田阳县| 阜康市| 孟津县| 江孜县| 康平县| 竹溪县| 剑川县| 武清区| 柯坪县| 扎赉特旗| 衡阳市| 无棣县| 莱西市| 鲁山县|